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

行为设计模式之Iterator(迭代器)

行为设计模式之Iterator(迭代器)

摘要:
迭代器模式(Iterator)是一种行为设计模式,它提供顺序访问聚合对象元素的方法,同时不暴露内部结构。该模式由迭代器接口(Iterator)、具体迭代器(ConcreteIterator)、聚合接口(Aggregate)和具体聚合(ConcreteAggregate)组成,适用于需要遍历不同聚合结构的场景。示例代码展示了通过BookIterator遍历BookAggregate中的图书信息,实现了数据存储与遍历逻辑的分离。这种模式支持多种遍历方式,并为不同聚合提供统一访问接口,提高了代码的灵活性和可维护性。

1)意图

提供一种方法顺序访问一个聚合对象中的各个元素,且不需要暴露该对象的内部表示。

2)结构

在这里插入图片描述

其中:

  • Iterator (迭代器)定义访问和遍历元素的接口。
  • ConcreteIterator(具体迭代器)实现迭代器接口;对该聚合遍历时跟踪当前位置。
  • Aggregate( 聚合)定义创建相应迭代器对象的接口。
  • ConcreteAggregate (具体聚合)实现创建相应迭代器的接口,该操作返回 ConcreteIterator
    的一个适当的实例。

3)适用性

Iterator 模式适用于:

  • 访问一个聚合对象的内容而无须暴露它的内部表示。
  • 支持对聚合对象的多种遍历。
  • 为遍历不同的聚合结构提供一个统一的接口
import java.util.ArrayList;
import java.util.List;public class IteratorPattern1 {public static void main(String[] args) {BookAggregate aggregate = new BookAggregate();String[] books = { "java", "python", "c++", "c" };double[] prices = { 100, 200, 300, 400 };for (int i = 0; i < 4; i++) {aggregate.addBook(new Book(books[i], prices[i]));}Iterator iterator = aggregate.createIterator();while (iterator.hasNext()) {Book book = (Book) iterator.next();System.out.println("书名:" + book.getName() + " 价格:" + book.getPrice());}}
}interface Iterator {boolean hasNext();Object next();
}class BookIterator implements Iterator {private int index;private BookAggregate bookAggregate;public BookIterator(BookAggregate bookAggregate) {this.index = 0;this.bookAggregate = bookAggregate;}@Overridepublic boolean hasNext() {if (index < bookAggregate.getBookCount()) {return true;}return false;}@Overridepublic Object next() {Book book = bookAggregate.getBook(index);index++;return book;}
}class BookAggregate implements Aggregate {private List<Book> bookList = new ArrayList<>();public void addBook(Book book) {bookList.add(book);}public Book getBook(int index) {return bookList.get(index);}public int getBookCount() {return bookList.size();}@Overridepublic Iterator createIterator() {return new BookIterator(this);}
}interface Aggregate {Iterator createIterator();
}class Book {private String name;private double price;public Book(String name, double price) {this.name = name;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}
}
http://www.xdnf.cn/news/12742.html

相关文章:

  • Ubuntu20.04中MySQL的安装和配置
  • 【iOS】JSONModel源码学习
  • LLMs 系列科普文(8)
  • 多线程语音识别工具
  • 【工具教程】多个条形码识别用条码内容对图片重命名,批量PDF条形码识别后用条码内容批量改名,使用教程及注意事项
  • 告别 @MockBean!在 Spring Boot 3.2+ 中使用 @MockitoBean 进行单元测试
  • 智慧园区管理平台
  • 阿里云Alibaba Cloud安装Docker与Docker compose【图文教程】
  • Spring 中的三级缓存机制详解
  • MySQL索引:7大类型+4维分类
  • 《Windows 10下QT+OpenCV+Yolo11:AI视觉开发实战指南》
  • GNSS高精度定位之-----星基差分
  • 数据网格的革命:从集中式到分布式的数据管理新范式
  • C++中的数组
  • Linux Docker的简介
  • uni-app学习笔记三十三--触底加载更多和下拉刷新的实现
  • 重新定义 AI 协同:三款开源 MCP 工具开启智能体从“聊天”到“操控”
  • [论文阅读] 人工智能+软件工程(软件测试) | 当大语言模型遇上APP测试:SCENGEN如何让手机应用更靠谱
  • 【论文阅读29】区间预测CIPM(2025)
  • RabbitMQ fanout交换机
  • 国防科技大学计算机基础慕课课堂学习笔记
  • Unity中的Mathf.Clamp01
  • 6.5 自学测试 数据库基础 Day5
  • 利用frp和腾讯云服务器将内网暴露至外网(内网穿透)
  • 【MATLAB代码】基于MCC(最大相关熵)的EKF,一维滤波,用于解决观测噪声的异常|附完整代码,订阅专栏后可直接查看
  • 模拟法解题的思路与算法分享
  • [GitHub] 优秀开源项目
  • python训练营打卡第47天
  • 27、基于map实现的简易kv数据库
  • AIGC的产品设计演进:从工具到协作者