深入浅出迭代器模式

简介: 深入浅出迭代器模式

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);
        }
    }
}
复制代码

测试结果:

网络异常,图片无法展示
|


4 小总结

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


相关文章
|
1月前
|
设计模式
深入浅出装饰者模式
深入浅出装饰者模式
20 2
|
1月前
|
设计模式 Java
深入浅出职责链模式
深入浅出职责链模式
38 0
|
1月前
|
设计模式 Java
Java设计模式【十七】:迭代器模式
Java设计模式【十七】:迭代器模式
40 0
|
1月前
|
设计模式 算法 Java
【设计模式系列笔记】迭代器模式
迭代器模式是一种行为设计模式,它提供了一种方法来顺序访问一个聚合对象中的各个元素,而不需要暴露该对象的内部表示。该模式通过定义一个迭代器接口,负责定义访问和遍历元素的方法,以及一个可迭代接口,负责返回迭代器的实例。
33 0
|
1月前
|
设计模式 Java
深入浅出迭代器模式
深入浅出迭代器模式
15 0
|
1月前
|
设计模式
深入浅出组合模式
深入浅出组合模式
16 0
|
10月前
|
设计模式 Java 程序员
java实现23种设计模式-引言
java实现23种设计模式-引言
34 0
|
1月前
|
设计模式 算法 数据库
二十三种设计模式全面解析-解密迭代器模式:探索遍历之道
二十三种设计模式全面解析-解密迭代器模式:探索遍历之道
|
设计模式 算法 安全
深入浅出设计模式 - 访问者模式
深入浅出设计模式 - 访问者模式
54 0
深入浅出设计模式 - 访问者模式
|
11月前
|
设计模式 Java 索引
【设计模式】用Java实现迭代器模式
迭代器模式(Iterator Pattern)是一种行为设计模式,它提供了一种顺序访问聚合对象中各个元素的方法,而无需暴露聚合对象的内部表示。
75 0