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

目录

一、行为型模式

一句话概括行为型模式

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、案例实现

实现上述学生案例.

java 复制代码
/**
 * 学生类
 */
public class Student {

    private String name;
    private int id;

    public Student() {
    }

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

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

    public String getName() {
        return name;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}
java 复制代码
/**
 * 抽象迭代器: 学生迭代器接口
 */
public interface StudentIterator {

    boolean hasNext();

    Student next();

}
java 复制代码
/**
 * 具体迭代器: 学生迭代器
 */
public class StudentIteratorImpl implements StudentIterator{

    private List<Student> list;
    private int position;

    public StudentIteratorImpl(List<Student> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return position < list.size();
    }

    @Override
    public Student next() {
        Student current = list.get(position);
        position++;
        return current;
    }

}
java 复制代码
/**
 * 抽象聚合: 学生聚合接口
 */
public interface StudentAggregation {


    void addStudent(Student student);

    void removeStudent(Student student);

    StudentIterator getStudentIterator();

}
java 复制代码
/**
 * 具体聚合: 学生聚合
 */
public class StudentAggregationImpl implements StudentAggregation{

    private List<Student> list = new ArrayList<>();

    @Override
    public void addStudent(Student student) {
        list.add(student);
    }

    @Override
    public void removeStudent(Student student) {
        list.remove(student);
    }

    @Override
    public StudentIterator getStudentIterator() {
        return new StudentIteratorImpl(list);
    }

}
java 复制代码
public class Client {

    public static void main(String[] args) {
        StudentAggregationImpl aggregation = new StudentAggregationImpl();
        aggregation.addStudent(new Student("曹操", 1));
        aggregation.addStudent(new Student("诸葛亮", 2));
        aggregation.addStudent(new Student("赵云", 3));
        StudentIterator studentIterator = aggregation.getStudentIterator();
        while(studentIterator.hasNext()) {
            Student student = studentIterator.next();
            System.out.println(student);
        }
    }

}

执行结果如下:

1.1.3、优缺点

优点:

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

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

缺点:

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

1.1.4、使用场景

  1. 当需要为聚合对象提供多种遍历方式.
  2. 当需要为遍历不同的聚合结构提供一个统一的接口时.
  3. 当访问的聚合对象的内容无需要暴露其内部实现细节.
相关推荐
geovindu11 小时前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh5 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊6 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua3336 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
wy_hhxx7 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
AAA修煤气灶刘哥7 天前
DSH Super-agent 双层架构:如何兼顾后台执行与持续响应
人工智能·后端·设计模式
用户624293196628 天前
告别 Electron 臃肿与 Wireshark 老化界面:用 Rust + Tauri 2.0 构建多总线网络与现场总线深度分析仪 Omni-Probe
设计模式
是2的10次方啊8 天前
设计模式与生活㉒:暖气热不热,得一间一间摸过去
设计模式
bigbread9 天前
迭代器模式-在区域健康档案导出中的实战应用
设计模式
装不满的克莱因瓶9 天前
设计模式之策略模式核心概念和应用场景
java·后端·设计模式·架构·系统架构·策略模式