Java设计模式-迭代器模式Iterator

介绍
根据GoF的定义,迭代器模式提供了一种顺序访问聚合对象的元素而不暴露其底层表示的方法。这是一种行为设计模式。
顾名思义,迭代器有助于以定义的方式遍历对象集合,这对客户端应用程序很有用。在迭代期间,客户端程序可以根据需求对元素执行各种其他操作。
原理类图

迭代器模式的角色说明如下:
Iterator:访问或遍历元素集合的接口。提供具体迭代器必须实现的方法。ConcreteIterator:实现Iterator接口方法。它还可以跟踪聚合集合遍历中的当前位置。Aggregate:它通常是一个集合接口,它定义了一个可以创建迭代器对象的方法。ConcreteAggregate:它实现Aggregate接口,其特定方法返回- -ConcreteIterator的实例。
什么时候使用?
每种编程语言都支持一些数据结构,如列表或映射,用于存储一组相关对象。在Java中,我们有List、Map和Set接口及其实现,如ArrayList和HashMap。
只有当集合提供了访问其元素而不暴露其内部结构的方法时,它才有用。迭代器承担这一责任。
因此,无论何时,我们都有对象集合,并且客户端需要一种方法以适当的顺序迭代每个集合元素,我们必须使用迭代器模式来设计解决方案。
迭代器模式允许我们以如下方式设计集合迭代器:
- 我们能够访问集合的元素,而不暴露元素的内部结构或集合本身。
迭代器支持在向前、向后或两个方向上从开始到结束对集合进行多次同时遍历。
迭代器为透明地遍历不同的集合类型提供了统一的接口。
JDK中的实现
在Java中,我们有Java.util.Iterator接口及其特定的实现,例如ListIterator。所有Java集合都提供了Iterator接口的一些内部实现,该接口用于迭代集合元素。
List<String> names = Arrays.asList("alex", "brian", "charles");
Iterator<String> namesIterator = names.iterator();
while (namesIterator.hasNext())
{
String currentName = namesIterator.next();
System.out.println(currentName);
}
实战案例
在这个迭代器模式示例中,我们正在创建一个集合,该集合可以保存Topic类的实例,并将提供一个迭代器来按顺序迭代Topic集合。
public class Topic
{
private String name;
public Topic(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface Iterator<E>
{
void reset(); // reset to the first element
E next(); // To get the next element
E currentItem(); // To retrieve the current element
boolean hasNext(); // To check whether there is any next element or not.
}
public class TopicIterator implements Iterator<Topic> {
private Topic[] topics;
private int position;
public TopicIterator(Topic[] topics)
{
this.topics = topics;
position = 0;
}
@Override
public void reset() {
position = 0;
}
@Override
public Topic next() {
return topics[position++];
}
@Override
public Topic currentItem() {
return topics[position];
}
@Override
public boolean hasNext() {
if(position >= topics.length)
return false;
return true;
}
}
public interface List<E>
{
Iterator<E> iterator();
}
public class TopicList implements List<Topic>
{
private Topic[] topics;
public TopicList(Topic[] topics)
{
this.topics = topics;
}
@Override
public Iterator<Topic> iterator() {
return new TopicIterator(topics);
}
}
public class Main
{
public static void main(String[] args)
{
Topic[] topics = new Topic[5];
topics[0] = new Topic("topic1");
topics[1] = new Topic("topic2");
topics[2] = new Topic("topic3");
topics[3] = new Topic("topic4");
topics[4] = new Topic("topic5");
List<Topic> list = new TopicList(topics);
Iterator<Topic> iterator = list.iterator();
while(iterator.hasNext()) {
Topic currentTopic = iterator.next();
System.out.println(currentTopic.getName());
}
}
}
- 程序输出:

Java设计模式-迭代器模式Iterator的更多相关文章
- JAVA 设计模式 迭代器模式
用途 迭代器模式 (Iterator) 提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示. 迭代器模式是一种行为型模式. 结构
- java设计模式——迭代器模式
一. 定义与类型 定义:提供一种方法,顺序访问一个集合对象中的各个元素,而又不暴露该对象的内部表示 类型:行为型. 二. 使用场景 (1) 访问一个集合对象的内容而无需暴露它的内部表示 (2) 为遍 ...
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- javascript设计模式-迭代器模式(Iterator)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 设计模式 -- 迭代器模式(Iterator)
--------------------------------------------------------------------- 1.场景问题 考虑这样一个问题: 9个学生对象分别通过数组存 ...
- Java设计模式の迭代器模式
迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. 迭代器模式的角色构成 (1)迭代器角色(Iterator):定义遍历元素所需 ...
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
- JAVA设计模式---迭代器模式
1.定义: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 2.实例:1)需求: 菜单(煎饼屋菜单.餐厅菜单和咖啡菜单)采用不同的集合存取(ArrayList,String[] ...
- java设计模式----迭代器模式和组合模式
迭代器模式: 提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 设计原则: 单一责任:一个类应该只有一个引起变化的原因 组合模式: 允许你将对象组合成树形结构来表现“整体/部分” ...
- Java 设计模式系列(十五)迭代器模式(Iterator)
Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...
随机推荐
- MyBatis06——动态SQL
动态SQL if choose (when, otherwise) trim (where, set) foreach 搭建环境 1.搭建数据库 CREATE TABLE `blog` ( `id` ...
- [转帖]datax安装+配置+使用文档
1 DataX离线同步工具DataX3.0介绍 DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具/平台,实现包括 MySQL.Oracle.SqlServer.Postgre.HDFS.Hiv ...
- [转帖]记一次vcsa6修复过程
一. 某天发现一台vmware vCenter Server Appliance services 6偶尔能登陆了,但极不稳定,连shell都偶尔能进...... 然后利用各种手段想方设法进到she ...
- zabbix 6.0 官方文档
Choose your platform ZABBIX VERSION 6.0 LTS 5.4 5.0 LTS 4.0 LTS OS DISTRIBUTION Red Hat Enterprise ...
- 你不知道的Promise状态变化机制
1.Promise中PromiseStatus的三种状态 var p = new Promise((resolve, reject) => { // resolve 既是函数也是参数,它用于处理 ...
- 往返回来的数据数组Array中添加一个字段的最优写法
在工作中我们经常会对后端返回来的数据进行添加一个字段: 最优的写法是 直接在 res.data[i].xx=aa 这样的方式去添加: 添加好了之后美酒 可以去赋值了: 让表格去渲染数据 this.$a ...
- gin框架中如何实现流式下载
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 团队中之前的文件下载做得比较复杂,因为担心量太大,是后台做 ...
- Gin-官方文档
目录 官方文档 官方文档 https://learnku.com/docs/gin-gonic/2018/gin-readme/3819 https://www.kancloud.cn/shuangd ...
- c和c++编译器之gcc和mingw
三大编译器:gcc,llvm,clang 什么是gcc? gcc 官方网站:https://gcc.gnu.org GCC(GNU Compiler Collection,GNU编译器套件),是由 G ...
- python2和python3的版本历史及入门书籍
python版本历史 我们端游项目使用是python2.7版本 32位 python2 2.7.18 last version on 2020.4.20 2.7 first version on 20 ...