深入浅出迭代器模式

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

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内部已经集成了迭代器模式,这样就不用我们开发人员每次使用都进行迭代,而可以直接使用。


相关文章
|
存储 算法 Python
python中递归调用过深(RecursionError)
【5月更文挑战第3天】
443 1
|
存储 Java API
使用Integer而不是int的场景和优势解析
使用Integer而不是int的场景和优势解析
390 0
|
Oracle 关系型数据库 MySQL
「OceanBase 4.1 体验」|连续多年支撑双11的国产原生分布式数据库
2023年,也就是今年的3 月 25 日,OceanBase 开发者大会·2023 在北京举办,大会上正式发布的 OceanBase 4.1 版本增加了旁路导入、租户级别物理备库、MySQL 8.0 兼容等多项面向开发者的能力。经测试,4.1 的小规格环境 TP 性能 sysnbench 综合读写能力相比 4.0 提升 40%,TPC-H 100G 场景性能比 4.0 提升 17%,TPC-DS 100G 场景性能比 4.0 提升 15%。
546 0
|
存储 前端开发 API
Whisper、React 和 Node 构建语音转文本 Web 应用程序(二)
Whisper、React 和 Node 构建语音转文本 Web 应用程序(二)
378 0
|
前端开发
【JCEF】后端通过技术进行web交互
【JCEF】后端通过技术进行web交互
377 0
|
存储 SQL 分布式计算
Hadoop 3.x各模式部署 - Ubuntu(上)
Hadoop 3.x各模式部署 - Ubuntu
311 0
|
消息中间件 安全 JavaScript
优雅的接口防刷处理方案! 上
优雅的接口防刷处理方案! 上
|
C语言
SAP ABAP——数据类型(六)【预定义基本数据类型详解】【上篇】
本文主要对SAP ABAP中预定义数据类型进行一个详细地介绍,在此前文章中我们已经对预定义数据类型有了一个最基本的了解,本文在此基础上会进行更加详细深入细致地讲解,主要包括预定义数据类型的分类
1238 0
SAP ABAP——数据类型(六)【预定义基本数据类型详解】【上篇】
|
机器学习/深度学习 自然语言处理 并行计算
【PyTorch基础教程2】自动求导机制(学不会来打我啊)
回顾我们在完成一项机器学习任务时的步骤: (1)首先需要对数据进行预处理,其中重要的步骤包括数据格式的统一和必要的数据变换,同时划分训练集和测试集。
177 0
【PyTorch基础教程2】自动求导机制(学不会来打我啊)