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

行为设计模式之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;}
}

相关文章:

  • ​**​CID字体​**​ 和 ​**​Simple字体​**​
  • 新手如何选择前端框架?
  • 行为型设计模式之Mediator(中介者)
  • 【Redis】持久化
  • 苍穹外卖-day01
  • 告别 @MockBean!在 Spring Boot 3.2+ 中使用 @MockitoBean 进行单元测试
  • NLP学习路线图(三十三): 文本分类
  • redis--黑马点评--分布式锁实现详解
  • uni-app学习笔记二十七--设置底部菜单TabBar的样式
  • AWS 公开数据集下载与操作说明
  • iOS 抖音导航栏首页一键分两列功能的实现
  • 【iOS】多线程NSOperation,NSOperationQueue
  • 从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(十二)
  • 【iOS】 Block再学习
  • Ubuntu20.04中 Redis 的安装和配置
  • Cursor 编辑器, 使用技巧,简单记录一下
  • 【人工智能 | 项目开发】Python Flask实现本地AI大模型可视化界面
  • 2025年与2030年AI及AI智能体 (Agent) 市场份额分析报告
  • 【GO性能优化】第十五章:性能优化艺术——揭秘Go程序的性能调优技巧
  • CppCon 2015 学习:Live Lock-Free or Deadlock
  • 杭州知名设计公司/seo关键词优化经验技巧
  • 深圳做网站哪个好/信阳网站推广公司
  • 简单html网页代码/北京百度推广优化公司
  • 世界疫情最新消息数据/汕头seo公司
  • 优化外贸网站/白云百度seo公司
  • 专业做二手房装修网站/网站模板购买