Chiriri's blog Chiriri's blog
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)

Iekr

苦逼后端开发
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)
  • JavaSE

  • JavaEE

  • Linux

  • MySQL

  • NoSQL

  • Python

  • Python模块

  • 机器学习

  • 设计模式

    • 设计模式的概念
    • 创建型模式
    • 单例(Singleton)模式
    • 原型(Prototype)模式
    • 工厂(Factory)模式
    • 建造者(Builder)模式
    • 结构型模式(Structural Pattern)
    • 适配器模式(Adapter Pattern)
    • 桥接模式(Bridge Pattern)
    • 装饰器模式(Decorator、Wrapper(包装) Pattern)
    • 代理模式(Proxy Pattern)
    • 外观模式(Facade Pattern)
    • 组合模式(Composite Pattern)
    • 享元模式(Flyweight Pattern)
    • 行为型模式(Behavioral Patterns)
    • 模板方法(Template Method)
    • 策略(Strategy)模式
    • 状态(State)模式
    • 中介者(Mediator)模式
    • 观察者(Observer)模式
    • 备忘录(Memento)模式
    • 解释器(Interpreter)模式
    • 命令(Command)模式
    • 迭代器(Iterator)模式
      • 应用场景
      • 注意事项和细节
    • 访问者(Visitor)模式
    • 职责链(Chain of Responsibility)模式
    • 总结
  • 传智健康

  • 畅购商城

  • 博客项目

  • JVM

  • JUC

  • Golang

  • Kubernetes

  • 硅谷课堂

  • C

  • 源码

  • 神领物流

  • RocketMQ

  • 短链平台

  • 后端
  • 设计模式
Iekr
2023-12-10
目录

迭代器(Iterator)模式

# 迭代器(Iterator)模式

提供 ** 一个对象 (迭代器) 来顺序访问聚合对象 (迭代数据)** 中的一系列数据,而不暴露聚合对象的内部表示。对象行为型模式

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

image-20231210213638909

抽象聚合(Aggregate)角色:定义存储、添加、删除聚合对象以及创建迭代器对象的接口。

public abstract class BeautifulMan {
    // 不方便暴露给外界的集合,只允许外界获取而不可以操作
    private List<String> girlFriends = new ArrayList<>();

    void likeYou(String name) {
        girlFriends.add(name);
    }

    void sayBye(String name) {
        girlFriends.remove(name);
    }

    public Itr getIterator() {
        return new Iterator(girlFriends);
    }


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

具体聚合(ConcreteAggregate)角色:实现抽象聚合类,返回一个具体迭代器的实例。

public class MaYuCheng extends BeautifulMan{

}
1
2
3

抽象迭代器(Iterator)角色:定义访问和遍历聚合元素的接口,通常包含 hasNext ()、first ()、next () 等方法。

/**
 * 抽象迭代器
 */
public interface Itr {
    boolean hasNext();

    String next();

    // 返回第一个
    String firstLove();

    // 返回当前
    String current();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

具体迭代器(Concretelterator)角色:实现抽象迭代器接口中所定义的方法,完成对聚合对象的遍历,记录遍历的当前位置。

/**
 * 具体迭代器
 */
public class Iterator implements Itr {
    private int cursor = 0; // 当前指针

    private List<String> girlFriends;

    public Iterator(List<String> girlFriends) {
        this.girlFriends = girlFriends;
    }

    public boolean hasNext() {
        if (cursor >= girlFriends.size()) {
            return false;
        }
        return true;
    }

    public String next() {
        if (cursor >= girlFriends.size()) {
            return null;
        }
        String s = girlFriends.get(cursor);
        // 指针偏移
        cursor++;
        return s;
    }

    @Override
    public String firstLove() {
        if (girlFriends.size() <= 0) {
            return null;
        }
        return girlFriends.get(0);
    }

    @Override
    public String current() {
        if (girlFriends == null) {
            return null;
        }
        return girlFriends.get(girlFriends.size() - 1);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

测试

public class MainTest {
    public static void main(String[] args) {
        MaYuCheng maYuCheng = new MaYuCheng();
        maYuCheng.likeYou("张三");
        maYuCheng.likeYou("李四");
        maYuCheng.likeYou("王五");

        Itr iterator = maYuCheng.getIterator();
        System.out.println(iterator.firstLove());
        System.out.println(iterator.hasNext());
        System.out.println(iterator.next());
        System.out.println(iterator.current());

        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 应用场景

什么场景用到?

  • jdk 容器接口的 Iterator 定义
  • 现实开发中,我们几乎无需编写迭代器,基本数据结构链表、树、图的迭代器已经都有了。除非要重写迭代逻辑

JDK 的 ArrayList 集合中就使用了迭代器模式

image-20231210222911208

image-20231210222916395

  • 内部类 Itr 充当具体实现迭代器 Iterator 的类, 作为 ArrayList 内部类
  • List 就是充当了聚合接口,含有一个 iterator () 方法,返回一个迭代器对象
  • ArrayList 是实现聚合接口 List 的子类,实现了 iterator ()
  • Iterator 接口系统提供
  • 迭代器模式解决了 不同集合 (ArrayList ,LinkedList) 统一遍历问题

# 注意事项和细节

优点:

  1. 提供一个统一的方法遍历对象,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象了。
  2. 隐藏了聚合的内部结构,客户端要遍历聚合的时候只能取到迭代器,而不会知道聚合的具体组成。
  3. 提供了一种 设计思想,就是一个类应该只有一个引起变化的原因(叫做单一责任原则)。在聚合类中,我们把迭代器分开,就是要把 管理对象集合和 遍历对象集合的责任分开,这样一来集合改变的话,只影响到聚合对象。而如果遍历方式改变的话,只影响到了迭代器。
  4. 当要展示一组相似对象,或者遍历一组相同对象时使用,适合使用迭代器模式

缺点:每个聚合对象都要一个迭代器,会生成多个迭代器不好管理类

编辑 (opens new window)
上次更新: 2023/12/13, 06:06:02
命令(Command)模式
访问者(Visitor)模式

← 命令(Command)模式 访问者(Visitor)模式→

最近更新
01
k8s
06-06
02
进程与线程
03-04
03
计算机操作系统概述
02-26
更多文章>
Theme by Vdoing | Copyright © 2022-2025 Iekr | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式