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

打印结果:

相关推荐
geovindu1 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh6 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊7 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua3337 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
wy_hhxx8 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
AAA修煤气灶刘哥8 天前
DSH Super-agent 双层架构:如何兼顾后台执行与持续响应
人工智能·后端·设计模式
用户624293196629 天前
告别 Electron 臃肿与 Wireshark 老化界面:用 Rust + Tauri 2.0 构建多总线网络与现场总线深度分析仪 Omni-Probe
设计模式
是2的10次方啊9 天前
设计模式与生活㉒:暖气热不热,得一间一间摸过去
设计模式
bigbread9 天前
迭代器模式-在区域健康档案导出中的实战应用
设计模式
装不满的克莱因瓶9 天前
设计模式之策略模式核心概念和应用场景
java·后端·设计模式·架构·系统架构·策略模式