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

目录

一、行为型模式

一句话概括行为型模式

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. 当访问的聚合对象的内容无需要暴露其内部实现细节.
相关推荐
七月丶24 分钟前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞31 分钟前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼1 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟18 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder18 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式