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());
        }
    }
}

打印结果:

相关推荐
kisshyshy5 小时前
从无崖子到OpenAI:大模型间的“传功”,动了谁的奶酪?
人工智能·深度学习·设计模式
某不知名網友9 小时前
C++ 六大常用设计模式详解(单例、工厂、策略、状态、观察者、代理)
开发语言·c++·设计模式
咖啡八杯9 小时前
GoF设计模式——责任链模式
设计模式·面试·架构
大辉狼_音频架构1 天前
Vol.01 高频设计模式
设计模式
我登哥MVP1 天前
走进 Gang of Four 设计模式:代理模式
设计模式·代理模式
我登哥MVP1 天前
走进 Gang of Four 设计模式:外观模式
java·设计模式·外观模式
ttod_qzstudio2 天前
【软考设计模式】备忘录模式:对象状态的捕获与无损恢复精讲
设计模式·备忘录模式
ttod_qzstudio2 天前
【软考设计模式】责任链模式:请求传递的多级处理与发送接收解耦精讲
设计模式·责任链模式
2501_914245932 天前
C语言设计模式详解:从理论到实践的完整指南
c语言·开发语言·设计模式
CaffeinePro2 天前
四⼤极简架构原则KISS/DRY/YAGNI/LOD
设计模式·架构