17、迭代器模式(Iterator Pattern)

迭代器模式提供了顺序访问集合对象中的各种元素,而不暴露该对象内部结构的方法。如Java中遍历HashMap。

迭代器模式将遍历集合中所有元素的操作封装成迭代器类,其目的是在不暴露集合对象内部结构的情况下,对外提供统一访问集合的内部数据的方法。迭代器的实现一般包括一个迭代器,用于执行具体的遍历操作;一个Collection,用于存储具体的数据。Collection集合的迭代器UML设计图:

1)Collection接口:

java 复制代码
package cn.jaa.iterator_pattern;

/**
 * @Author: Jaa
 * @Description:
 * @Date 2023/12/5 23:02
 */
public interface Collection {
    // 对集合元素的迭代
    public Iterator iterator();
    // 获取集合元素
    public Object get(int i);
    // 向添加元素
    public boolean add(Object obj);
    // 获取集合的大小
    public int size();
}

2)Collection接口实现类ListCollection:

java 复制代码
package cn.jaa.iterator_pattern;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Jaa
 * @Description: Collection接口实现类ListCollection
 * @Date 2023/12/5 23:07
 */
public class ListCollection implements Collection {

    public List list = new ArrayList();

    @Override
    public Iterator iterator() {
        return new ConcreteIterator(this);
    }

    @Override
    public Object get(int i) {
        return list.get(i);
    }

    @Override
    public boolean add(Object obj) {
        list.add(obj);
        return true;
    }

    @Override
    public int size() {
        return list.size();
    }
}

3)迭代器接口Iterator:

java 复制代码
package cn.jaa.iterator_pattern;

/**
 * @Author: Jaa
 * @Description:
 * @Date 2023/12/5 23:08
 */
public interface Iterator {
    // 前移指针
    public Object previous();
    // 后移指针
    public Object next();
    public boolean hasNext();
}

4)Iterator的实现类ConcreteIterator:

java 复制代码
package cn.jaa.iterator_pattern;

/**
 * @Author: Jaa
 * @Description: Iterator的实现类ConcreteIterator
 * @Date 2023/12/5 23:10
 */
public class ConcreteIterator implements Iterator {

    private Collection collection;
    // 当前迭代器遍历到的元素位置
    private int pos = -1;

    public ConcreteIterator(Collection collection) {
        this.collection = collection;
    }

    @Override
    public Object previous() {
        if (pos > 0) {
            pos--;
        }
        return collection.get(pos);
    }

    @Override
    public Object next() {
        if (pos < collection.size() - 1) {
            pos++;
        }
        return collection.get(pos);
    }

    @Override
    public boolean hasNext() {
        if (pos < collection.size() - 1) {
            return true;
        } else {
            return false;
        }
    }
}

5)测试迭代器模式:

java 复制代码
package cn.jaa.iterator_pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author: Jaa
 * @Description:
 * @Date 2023/12/5 23:19
 */
@Slf4j
public class IteratorDemoTest {

    public static void main(String[] args) {
        Collection collection = new ListCollection();
        collection.add("obj1");
        collection.add("obj2");
        collection.add("obj3");
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            log.info((String) it.next());
        }
    }
}

打印结果:

相关推荐
Shadow(⊙o⊙)10 小时前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki1 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅661 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa1 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
sarasuki1 天前
如何让 Agent 获取更多的能力?插件 / 技能系统
人工智能·设计模式·agent
sarasuki1 天前
如何让 Agent 安全运行你的命令 :命令分级 + Hook + 读写锁
人工智能·设计模式·agent
Zane19941 天前
技术不难,为什么项目却越改越不敢动?聊聊复杂系统开发该怎么想
设计模式
geovindu1 天前
rust: Factory Method Pattern
开发语言·设计模式·rust·工厂方法模式·创建型模式
一木 之林2 天前
第 8 节 C++ 设计模式在 AI 推理 SDK 和 Agent 工具运行时中如何落地
c++·人工智能·设计模式
Zane19942 天前
MVC 三层架构被打上反模式标签?一次账户扣款需求,讲清楚贫血模型和充血模型该怎么选
设计模式