• 设计模式 - 行为型模式考点篇:迭代器模式(概述 | 案例实现 | 优缺点 | 使用场景)


    目录

    一、行为型模式

    一句话概括行为型模式

    1.1、迭代器模式

    1.1.1、概述 

    1.1.2、案例实现

    1.1.3、优缺点

    1.1.4、使用场景


    一、行为型模式


    一句话概括行为型模式

    行为型模式:类或对象间如何交互、如何划分职责,从而更好的完成任务.

    1.1、迭代器模式

    1.1.1、概述 

    提供一个聚合对象,内部通过迭代器来访问聚合对象中的一系列数据,而不暴露聚合对象的内部实现.

    例如,现在有一个班级的学生(包装在一个 List 容器中的聚合元素),我需要按照学号拿到每一个学生,此时就需要把遍历这个班级的学生(List 容器)交给迭代器完成.

    迭代器模式主要包含以下角色:

    • 抽象迭代器:定义访问和遍历聚合元素的接口,通常包含 hasNext()、next() 等方法.
    • 具体迭代器:实现抽象迭代器接口中定义的方法,完成聚合对象的遍历,记录遍历的当前位置.
    • 抽象聚合:定义存储、添加、删除聚合元素以及创建迭代器对象接口.
    • 具体聚合:实现抽象聚合类,返回一个具体的迭代器实例.

    1.1.2、案例实现

    实现上述学生案例.

    1. /**
    2. * 学生类
    3. */
    4. public class Student {
    5. private String name;
    6. private int id;
    7. public Student() {
    8. }
    9. public Student(String name, int id) {
    10. this.name = name;
    11. this.id = id;
    12. }
    13. @Override
    14. public String toString() {
    15. return "Student{" +
    16. "name='" + name + '\'' +
    17. ", id=" + id +
    18. '}';
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. public void setName(String name) {
    24. this.name = name;
    25. }
    26. public int getId() {
    27. return id;
    28. }
    29. public void setId(int id) {
    30. this.id = id;
    31. }
    32. }
    1. /**
    2. * 抽象迭代器: 学生迭代器接口
    3. */
    4. public interface StudentIterator {
    5. boolean hasNext();
    6. Student next();
    7. }
    1. /**
    2. * 具体迭代器: 学生迭代器
    3. */
    4. public class StudentIteratorImpl implements StudentIterator{
    5. private List list;
    6. private int position;
    7. public StudentIteratorImpl(List list) {
    8. this.list = list;
    9. }
    10. @Override
    11. public boolean hasNext() {
    12. return position < list.size();
    13. }
    14. @Override
    15. public Student next() {
    16. Student current = list.get(position);
    17. position++;
    18. return current;
    19. }
    20. }
    1. /**
    2. * 抽象聚合: 学生聚合接口
    3. */
    4. public interface StudentAggregation {
    5. void addStudent(Student student);
    6. void removeStudent(Student student);
    7. StudentIterator getStudentIterator();
    8. }
    1. /**
    2. * 具体聚合: 学生聚合
    3. */
    4. public class StudentAggregationImpl implements StudentAggregation{
    5. private List list = new ArrayList<>();
    6. @Override
    7. public void addStudent(Student student) {
    8. list.add(student);
    9. }
    10. @Override
    11. public void removeStudent(Student student) {
    12. list.remove(student);
    13. }
    14. @Override
    15. public StudentIterator getStudentIterator() {
    16. return new StudentIteratorImpl(list);
    17. }
    18. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. StudentAggregationImpl aggregation = new StudentAggregationImpl();
    4. aggregation.addStudent(new Student("曹操", 1));
    5. aggregation.addStudent(new Student("诸葛亮", 2));
    6. aggregation.addStudent(new Student("赵云", 3));
    7. StudentIterator studentIterator = aggregation.getStudentIterator();
    8. while(studentIterator.hasNext()) {
    9. Student student = studentIterator.next();
    10. System.out.println(student);
    11. }
    12. }
    13. }

    执行结果如下:

    1.1.3、优缺点

    优点:

    定义多种遍历方式:支持不同方式遍历一个聚合对象,可以在同一个聚合对象上顶一个多种遍历方式.

    满足开闭原则:引入抽象层,增加新的聚合类和迭代器,都无需修改原有代码.

    缺点:

    增加了类的个数,一定程度上增加了系统复杂度.

    1.1.4、使用场景

    1. 当需要为聚合对象提供多种遍历方式.
    2. 当需要为遍历不同的聚合结构提供一个统一的接口时.
    3. 当访问的聚合对象的内容无需要暴露其内部实现细节.

  • 相关阅读:
    【Linux进程间通信】二、pipe管道
    Python学习笔记合集(总结)
    剪映电脑版画中画在哪?
    动态规划——背包问题
    基于C#实现的《勇士返乡》游戏设计
    GP08|财务&估值因子过滤实盘小市值
    HBase海量数据高效入仓解决方案
    Python内置函数/方法详解—元组tuple
    web前端期末大作业 基于HTML+CSS+JavaScript绿色的在线教育平台网站响应式企业网站模板
    Golang 泛型的介绍
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/133741429