行为型模式之迭代器模式

迭代器模式(Iterator Pattern)

迭代器模式(Iterator Pattern)是一种行为设计模式,它提供了一种顺序访问集合对象中各个元素的方法,而无需暴露集合的内部表示。
在迭代器模式中,有三个主要角色:

迭代器(Iterator):定义了访问和遍历集合元素的接口。

具体迭代器(Concrete Iterator):实现迭代器接口,负责具体集合的遍历逻辑。

集合(Collection):定义创建迭代器对象的接口。

具体集合(Concrete Collection):实现集合接口,返回一个特定迭代器的实例。

迭代器模式的核心思想是将遍历集合的责任交给迭代器对象,而不是集合对象本身。

这样可以简化集合的接口,同时也使得各个迭代器可以独立变化而不影响集合对象。
使用迭代器模式的好处包括:

可以提供不同的遍历方式,如顺序遍历、逆序遍历等。

将集合与遍历算法解耦,允许独立修改它们。

可以隐藏集合的内部结构,提供统一的访问接口。

提供demo版代码更容易理解

java 复制代码
/**
 * @author zhou
 *  迭代器接口
 */
interface Iterator<T> {
    boolean hasNext();
    T next();
}

/**
 * @author zhou
 *  具体迭代器的实现
 */
class ConcreteIterator<T> implements Iterator<T> {
    private List<T> collection;
    private int index;

    public ConcreteIterator(List<T> collection) {
        this.collection = collection;
        this.index = 0;
    }

    public boolean hasNext() {
        return index < collection.size();
    }

    public T next() {
        if (hasNext()) {
            T item = collection.get(index);
            index++;
            return item;
        } else {
            throw new IndexOutOfBoundsException("End of iteration");
        }
    }
}

/**
 * @author zhou
 *  集合接口
 */
interface Collection<T> {
    Iterator<T> createIterator();
}

/**
 * @author zhou
 *  具体集合实现
 */
class ConcreteCollection<T> implements Collection<T> {
    private List<T> items;

    public ConcreteCollection(List<T> items) {
        this.items = items;
    }

    public Iterator<T> createIterator() {
        return new ConcreteIterator<>(items);
    }
}

/**
 * @author zhou
 *  客户端代码
 */
public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        Collection<Integer> collection = new ConcreteCollection<>(list);
        Iterator<Integer> iterator = collection.createIterator();

        while (iterator.hasNext()) {
            Integer item = iterator.next();
            System.out.println(item);
        }
    }
}

我们定义了一个迭代器接口 Iterator,具体迭代器实现 ConcreteIterator,集合接口 Collection,和具体集合实现 ConcreteCollection。

使用示例代码中的 Main 类来展示如何使用迭代器模式。
在 ConcreteIterator 中,我们使用一个 List 来存储集合元素,并通过索引 index 来控制迭代过程。

在 hasNext 方法中判断是否还有下一个元素,next 方法返回下一个元素并将索引加一。

在 ConcreteCollection 中,我们以一个 List 作为具体集合的内部数据结构,并实现了 createIterator 方法返回具体迭代器实例。
在 Main 类的 main 方法中,我们创建了一个包含整数的 List 对象,并将其传递给 ConcreteCollection 的实例。

然后通过调用 createIterator 方法获取一个迭代器,并使用 while 循环遍历集合中的元素并输出结果。

该示例展示了如何使用迭代器模式来遍历集合并访问其中的元素,提供了灵活的遍历方式,并将集合与具体的遍历实现解耦。

相关推荐
love530love几秒前
家用或办公 Windows 电脑玩人工智能开源项目配备核显的必要性(含 NPU 及显卡类型补充)
人工智能·windows·python·开源·电脑
周胡杰12 小时前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
几道之旅17 小时前
分别在windows和linux上使用curl,有啥区别?
linux·运维·windows
一直奔跑在路上18 小时前
【Ansible】基于windows主机,采用NTLM+HTTPS 认证部署
windows·https·ansible
郭逍遥18 小时前
[工具]B站缓存工具箱 (By 郭逍遥)
windows·python·缓存·工具
x-cmd19 小时前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
前端·javascript·windows·npm·node.js
IT空门:门主20 小时前
本地的ip实现https访问-OpenSSL安装+ssl正式的生成(Windows 系统)
windows·https·ssl
安装虚拟机的老师傅20 小时前
【2025最新】Windows系统装VSCode搭建C/C++开发环境(附带所有安装包)
c语言·windows·vscode·其他
越甲八千1 天前
windowsC++操作ADB
c++·windows·adb
九班长1 天前
Mirror的多人连接管理及房间系统
windows