深入浅出迭代器模式

1 概念

迭代器模式在设计模式中属于行为型模式,用于顺序访问集合对象的元素,不需要知道集合对象的底层表示。

迭代器模式的结构中包括四种角色:

  • 抽象集合(Abstract Aggregate)
  • 具体集合(Concrete Aggregate)
  • 抽象迭代器(Abstract Iterator)
  • 具体迭代器(Concrete Iterator)

2 案例说明

对Student类实现迭代输出
在这里插入图片描述

3 代码实现

Aggregate.java

/**
 * @desc: 集合容器抽象
 * @author: YanMingXin
 * @create: 2021/8/31-15:53
 **/
public interface Aggregate {
    
    

    public Iterator iterator();

}

Iterator.java

/**
 * @desc: 抽象迭代器
 * @author: YanMingXin
 * @create: 2021/8/31-15:53
 **/
public interface Iterator<E> {
    
    

    public boolean hasNext();

    public E next();

}

Student.java

/**
 * @desc: Student实体类
 * @author: YanMingXin
 * @create: 2021/8/31-15:54
 **/
public class Student  {
    
    

    private Integer id;

    private String name;

    private Integer age;

    public Student() {
    
    

    }

    public Student(Integer id, String name, Integer age) {
    
    
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
    
    
        return id;
    }

    public void setId(Integer id) {
    
    
        this.id = id;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

StudentIterator.java

/**
 * @desc: 具体迭代器
 * @author: YanMingXin
 * @create: 2021/8/31-15:55
 **/
public class StudentIterator implements Iterator {
    
    

    private StudentShelf studentShelf;

    private int index;

    public StudentIterator(StudentShelf studentShelf) {
    
    
        this.studentShelf = studentShelf;
        this.index = 0;
    }

    @Override
    public boolean hasNext() {
    
    
        if (index < studentShelf.getLength()) {
    
    
            return true;
        }
        return false;
    }

    @Override
    public Object next() {
    
    
        Student studentAt = studentShelf.getStudentAt(index);
        index += 1;
        return studentAt;
    }
}

StudentShelf.java

/**
 * @desc: 集合容器具体实现
 * @author: YanMingXin
 * @create: 2021/8/31-16:11
 **/
public class StudentShelf implements Aggregate {
    
    

    private Student[] students;

    private int size = 0;

    private final int DEFAULT_SIZE = 10;

    public StudentShelf() {
    
    
        this.students = new Student[DEFAULT_SIZE];
    }

    public StudentShelf(int maxsize) {
    
    
        this.students = new Student[maxsize];
    }

    public Student getStudentAt(int index) {
    
    
        return students[index];
    }

    public void appendStudent(Student student) {
    
    
        this.students[size] = student;
        size++;
        if (size >= DEFAULT_SIZE) {
    
    
            //扩容
            Student[] stus = new Student[size * 2];
            System.arraycopy(this.students, 0, stus, 0, this.students.length);
            this.students = stus;
        }
    }

    public int getLength() {
    
    
        return size;
    }

    @Override
    public Iterator<Student> iterator() {
    
    
        return new StudentIterator(this);
    }
}

MyIterator.java

/**
 * @desc: 迭代器模式测试
 * @author: YanMingXin
 * @create: 2021/8/31-15:51
 **/
public class MyIterator {
    
    


    public static void main(String[] args) {
    
    
        StudentShelf shelf = new StudentShelf();
        shelf.appendStudent(new Student(1,"zs",23));
        shelf.appendStudent(new Student(2,"ls",24));
        shelf.appendStudent(new Student(3,"ww",25));

        Iterator<Student> iterator = shelf.iterator();
        while (iterator.hasNext()){
    
    
            Student next = iterator.next();
            System.out.println(next);
        }
    }

}

测试结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XBVhXrAH-1631182326243)(深入浅出迭代器模式.assets/image-20210909175029284.png)]

4 小总结

迭代器模式相比其他设计模式感觉实现起来麻烦一些,而且还不如for循环好用,那是因为Java内部已经集成了迭代器模式,这样就不用我们开发人员每次使用都进行迭代,而可以直接使用。

猜你喜欢

转载自blog.csdn.net/Mr_YanMingXin/article/details/120206892
今日推荐