java常见面试题:什么是迭代器模式(Iterator Pattern)?如何实现迭代器模式?

迭代器模式(Iterator Pattern)是设计模式中的一种,它提供了一种顺序访问一个聚合对象(如列表、集合等)中各个元素的方法,而又不需要暴露该对象的内部表示。使用迭代器模式,可以方便地遍历一个聚合对象的所有元素,而不需要了解该对象的底层结构。

迭代器模式主要包含以下角色:

  1. 迭代器接口(Iterator):定义访问和遍历元素的接口,通常包含 hasNext() 和 next() 等方法。
  2. 具体迭代器(Concrete Iterator):实现迭代器接口,完成对聚合对象的遍历,并记录当前遍历的位置。
  3. 聚合接口(Aggregate):定义创建相应迭代器对象的接口。
  4. 具体聚合(Concrete Aggregate):实现创建相应迭代器的接口,返回一个具体迭代器实例。

以下是使用 Java 实现迭代器模式的示例:

复制代码

java复制代码

|---|----------------------------------------------------------------|
| | // 迭代器接口 |
| | public interface Iterator<T> { |
| | boolean hasNext(); |
| | T next(); |
| | } |
| | |
| | // 聚合接口 |
| | public interface Aggregate<T> { |
| | Iterator<T> createIterator(); |
| | } |
| | |
| | // 具体聚合 |
| | public class ConcreteAggregate<T> implements Aggregate<T> { |
| | private List<T> list = new ArrayList<>(); |
| | |
| | public void add(T item) { |
| | list.add(item); |
| | } |
| | |
| | public void remove(T item) { |
| | list.remove(item); |
| | } |
| | |
| | @Override |
| | public Iterator<T> createIterator() { |
| | return new ConcreteIterator<>(list); |
| | } |
| | } |
| | |
| | // 具体迭代器 |
| | public class ConcreteIterator<T> implements Iterator<T> { |
| | private List<T> list; |
| | private int index = 0; |
| | |
| | public ConcreteIterator(List<T> list) { |
| | this.list = list; |
| | } |
| | |
| | @Override |
| | public boolean hasNext() { |
| | return index < list.size(); |
| | } |
| | |
| | @Override |
| | public T next() { |
| | if (hasNext()) { |
| | return list.get(index++); |
| | } |
| | return null; |
| | } |
| | } |
| | |
| | // 客户端代码 |
| | public class Client { |
| | public static void main(String[] args) { |
| | Aggregate<String> aggregate = new ConcreteAggregate<>(); |
| | aggregate.add("Item 1"); |
| | aggregate.add("Item 2"); |
| | aggregate.add("Item 3"); |
| | |
| | Iterator<String> iterator = aggregate.createIterator(); |
| | while (iterator.hasNext()) { |
| | System.out.println(iterator.next()); |
| | } |
| | } |
| | } |

在这个示例中,我们首先定义了一个迭代器接口 Iterator,它包含 hasNext()next() 方法。然后,我们定义了一个聚合接口 Aggregate,它包含 createIterator() 方法,用于创建相应聚合对象的迭代器。

接着,我们创建了一个具体聚合类 ConcreteAggregate,它实现了 Aggregate 接口,并包含一个内部列表来存储元素。ConcreteAggregatecreateIterator() 方法返回一个具体迭代器实例,用于遍历聚合对象中的元素。

最后,我们创建了一个具体迭代器类 ConcreteIterator,它实现了 Iterator 接口,并使用一个内部索引来记录当前遍历的位置。hasNext() 方法检查是否还有更多元素,next() 方法返回下一个元素,并将索引递增。

在客户端代码中,我们创建了一个具体聚合对象,向其中添加了一些元素,并使用聚合对象的 createIterator() 方法创建了一个迭代器。然后,我们使用迭代器的 hasNext()next() 方法遍历了聚合对象中的所有元素。

相关推荐
cxoptics15 分钟前
冰洲石分束器设计与应用
java
ck-joker30 分钟前
M1 Pro跑LLM实测:Ollama+LangChain4j零成本本地大模型开发,比云端API快在哪?
java·语言模型
阿米亚波1 小时前
【C++ STL】std::unordered_multiset
开发语言·数据结构·c++·笔记·stl
丙氨酸長鏈1 小时前
[Bukkit插件开发]手持发射器箭矢机枪 教学文档 面向Python/C#开发者入门Java与Bukkit API
java·python·c#
Nontee1 小时前
设计模式:模板方法与策略,从“每个字都认识“到能说清它们在干嘛
java·数据库·设计模式
我是唐青枫1 小时前
Java ReentrantLock 实战详解:比 synchronized 更灵活的可重入锁
java·开发语言
QH139292318801 小时前
# R&S ZNB43 ZNA43 ZNA67矢量网络分析仪
开发语言·网络·php
晨曦中的暮雨2 小时前
Virtual Thread 优化 Spring AI 阻塞式 LLM I/O:对照压测报告
java·并发·个人项目
脚踏实地皮皮晨2 小时前
003003001_Grid控件
开发语言·windows·算法·c#·visual studio
十年磨剑走天涯2 小时前
C++ STL 容器操作复杂度速查表
开发语言·c++