当前位置: 首页 > news >正文

行为型模式 - 迭代器模式 (Iterator Pattern)

行为型模式 - 迭代器模式 (Iterator Pattern)

迭代器模式提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。


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

// 迭代器接口
interface Iterator<T> {
    boolean hasNext();
    T next();
}

// 集合接口
interface Aggregate<T> {
    Iterator<T> createIterator();
}

// 自定义集合类
class CustomList<T> implements Aggregate<T> {
    private List<T> items = new ArrayList<>();

    public void add(T item) {
        items.add(item);
    }

    @Override
    public Iterator<T> createIterator() {
        return new CustomListIterator<>(items);
    }
}

// 自定义迭代器类
class CustomListIterator<T> implements Iterator<T> {
    private List<T> items;
    private int position = 0;

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

    @Override
    public boolean hasNext() {
        return position < items.size();
    }

    @Override
    public T next() {
        if (hasNext()) {
            return items.get(position++);
        }
        return null;
    }
}

// 客户端代码
public class CustomListExample {
    public static void main(String[] args) {
        CustomList<String> list = new CustomList<>();
        list.add("苹果");
        list.add("香蕉");
        list.add("橙子");

        Iterator<String> iterator = list.createIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

在JDK源码中, 例如 ArrayList 的 Iterator
List: 抽象聚合类
ArrayList: 具体聚合类
Iterator: 抽象迭代器
list.iterator(): 返回的是实现了 Iterator 接口的具体迭代器


文章转载自:
http://agravic.hyyxsc.cn
http://beslaver.hyyxsc.cn
http://amidase.hyyxsc.cn
http://bioelectricity.hyyxsc.cn
http://astringency.hyyxsc.cn
http://army.hyyxsc.cn
http://bathybic.hyyxsc.cn
http://archdeaconship.hyyxsc.cn
http://caviler.hyyxsc.cn
http://catatonic.hyyxsc.cn
http://author.hyyxsc.cn
http://aeg.hyyxsc.cn
http://cauldron.hyyxsc.cn
http://aeneous.hyyxsc.cn
http://benefactor.hyyxsc.cn
http://calamint.hyyxsc.cn
http://caprification.hyyxsc.cn
http://calyculate.hyyxsc.cn
http://bijouterie.hyyxsc.cn
http://chainbelt.hyyxsc.cn
http://bitonal.hyyxsc.cn
http://backgrounder.hyyxsc.cn
http://bondman.hyyxsc.cn
http://babysiting.hyyxsc.cn
http://baisakh.hyyxsc.cn
http://blair.hyyxsc.cn
http://cassab.hyyxsc.cn
http://birchen.hyyxsc.cn
http://bellman.hyyxsc.cn
http://busk.hyyxsc.cn
http://www.dtcms.com/a/46220.html

相关文章:

  • SpringCloud + Spring AI Alibaba 整合阿里云百炼大模型
  • 【大模型】大模型推理部署工具之vLLM的使用(1)
  • 在Nginx上配置并开启WebDAV服务的完整指南
  • AI赋能教育:用智能体点亮教育的温度
  • 【Python】OpenCV算法使用案例全解
  • 【Java】JDK17新特性
  • C++函数重载
  • 大白话React第十一章React 相关的高级特性以及在实际项目中的应用优化
  • 服务器虚拟化:释放计算潜力的魔法
  • Vscode 便用快捷键设置教程
  • 蓝桥杯备考:动态规划入门题目之下楼梯问题
  • YOLOv8目标检测推理流程及C++代码
  • Android6到Android15版本新增的功能和api
  • Docker项目部署-部署前端
  • 深入解析JasperReports模板标签构建高效报表的利器系列一
  • 1985-2019年上市公司绿色专利申请量数据
  • 云服务培训四-网络服务
  • python---‘DataFrame‘ object has no attribute ‘read‘ 报错的解决
  • 牛客周赛83:A:JAVA
  • 面试题:说一下你对DDD的了解?
  • 常用的api测试软件
  • zookeeper-docker版
  • DeepSeek本地部署教程
  • gmock和cppfreemock原理学习
  • Python使用pyobdc库和tkinter框架连接数据库
  • oracle游标为什么没有共享,统计一下原因
  • 水果识别系统 | BP神经网络水果识别系统,含GUI界面(Matlab)
  • 《白帽子讲 Web 安全:点击劫持》
  • Makefile、Make和CMake:构建工具的三剑客
  • 授权与认证之jwt(一)创建Jwt工具类