• 什么时候需要用到迭代器模式?

  有许多中方法,可以把对象堆起来放进一个集合(可以是数组、堆栈、列表、哈希表,等等)。

  每一种类型的集合,都有各自适用的时机。但是某个时间段,客户端可能希望去遍历这个集合。

  怎么做?

  让客户去得到这个集合的具体实现?显然这不是很现实。

  而且针对不同的集合,我们需要用不同的方式去遍历它,这需要去深入了解各种数据结构,对客户端很不友好。

  这时候,我们希望创建一种超集合(super collection),是客户端能够使用统一的方法去遍历集合,同时不需要关心具体的数据结构。

  这就是迭代器模式。

  • 迭代器模式:

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

  从 Java 的角度来说,迭代器模式就是 Iterator 接口。

  • 模拟场景:

  有一个水果商店(FruitShop),内部采取数组的方式存储数据。

  有一个肉食商店(MeatShop),内部采用链表的方式存储数据。

  客户端希望去遍历这些数据,

  • 没有迭代器的实现:
public final class FruitShop {

    private String[] products = new String[MAX_PRODUCT];
private int index = 0; private static final int MAX_PRODUCT = 10; public void addProduct(String product) {
if (index < MAX_PRODUCT) {
products[index++] = product;
} else {
throw new RuntimeException("FruitShop is full");
}
} public String[] getProducts() {
return Arrays.copyOf(products, index);
}
}
public final class MeatShop {

    private List<String> products = new LinkedList<>();

    public void addProduct(String product) {
products.add(product);
} public List<String> getProducts() {
return products;
}
}
    @Test
void testFruitShop() {
FruitShop fruitShop = new FruitShop();
fruitShop.addProduct("Apple");
fruitShop.addProduct("Orange");
String[] products = fruitShop.getProducts();
for (String product : products) {
System.out.println(product);
}
} @Test
void testMeatShop() {
MeatShop meatShop = new MeatShop();
meatShop.addProduct("Beef");
meatShop.addProduct("Pork");
List<String> products = meatShop.getProducts();
for (String product : products) {
System.out.println(product);
}
}

  上面的代码有如下的问题:

  1. 两个 Shop 暴露了内部的数据结构。
  2. 客户端采取不同的方法去遍历数据。
  • 包含迭代器的实现
public final class FruitShopIterator implements Iterator<String> {

    private String[] products = new String[MAX_PRODUCT];
private int index = 0; private int iteratorIndex = 0; private static final int MAX_PRODUCT = 10; public void addProduct(String product) {
if (index < MAX_PRODUCT) {
products[index++] = product;
} else {
throw new RuntimeException("FruitShop is full");
}
} @Override
public boolean hasNext() {
return iteratorIndex < index;
} @Override
public String next() {
return products[iteratorIndex++];
}
}
public final class MeatShopIterator implements Iterator<String> {

    private List<String> products = new LinkedList<>();
private int iteratorIndex = 0; public void addProduct(String product) {
products.add(product);
} @Override
public boolean hasNext() {
return iteratorIndex < products.size();
} @Override
public String next() {
return products.get(iteratorIndex++);
}
}
    @Test
void testFruitShopIterator() {
FruitShopIterator fruitShop = new FruitShopIterator();
fruitShop.addProduct("Apple");
fruitShop.addProduct("Orange");
while (fruitShop.hasNext()) {
System.out.println(fruitShop.next());
}
} @Test
void testMeatShopIterator() {
MeatShopIterator meatShop = new MeatShopIterator();
meatShop.addProduct("Beef");
meatShop.addProduct("Pork");
while (meatShop.hasNext()) {
System.out.println(meatShop.next());
}
}

  可以看出:

  1. Iterator 提供了 hasNext() 和 next() 方法负责集合数据的遍历。
  2. Shop 类避免了暴露具体的数据结构。
  3. 客户端采用统一的方式去遍历数据。

设计模式(十六)迭代器模式 Iterator的更多相关文章

  1. 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)

      设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...

  2. 设计模式系列之迭代器模式(Iterator Pattern)——遍历聚合对象中的元素

    模式概述 模式定义 模式结构图 模式伪代码 模式改进 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修 ...

  3. C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看."迭代器模 ...

  4. 《JAVA设计模式》之迭代器模式(Iterator)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述迭代子(Iterator)模式的: 迭代子模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不 ...

  5. 迭代器模式 Iterator 行为型 设计模式(二十)

    迭代器模式(Iterator)   走遍天下,世界那么大,我想去看看   在计算机中,Iterator意为迭代器,迭代有重复的含义,在程序中,更有“遍历”的含义 如果给定一个数组,我们可以通过for循 ...

  6. Java 设计模式系列(十五)迭代器模式(Iterator)

    Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...

  7. 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)

    上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...

  8. 二十四种设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...

  9. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  10. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)

    设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...

随机推荐

  1. COGS 2794. 爱摔跤的比利海灵顿

    ★☆   输入文件:find_k.in   输出文件:find_k.out   简单对比时间限制:1.4 s   内存限制:128 MB [题目描述] B•海灵顿•雷想要和n个巨人比试摔♂跤,他想先和 ...

  2. 打开某exe提示"应用程序无法启动,因为应用程序的并行配置不正确……"的解决方案

    本人在新安装好了的windows server 2008 r2 (64位)上运行“RefilesName V2.0(文件批量改名).exe”,结果提示: 应用程序无法启动,因为应用程序的并行配置不正确 ...

  3. Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )

    图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...

  4. Spark 配置整理

    Spark 的配置有很多,这里一方面总结一下官方文档中的内容,一方面将网上查到的资料中用到的针对特定问题的配置整理一下. 先看一下官网的配置:http://spark.apache.org/docs/ ...

  5. Python面向对象进阶(二)

    Python面向对象进阶2.html :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba(0,0,0,.1 ...

  6. RestSharp使用备忘

    (1)一般调用: public static List<T> Execute<T>(string resourceUrl, object obj, out int totalN ...

  7. webservice基础

    一.webservice概念 webservice用于异构平台之间的交互,我用Java写的程序,可以用php..net.pythod等其它语言的程序来访问我的接口.webservice有很多框架帮我们 ...

  8. javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件

    有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...

  9. 如何用纯 CSS 创作一副国际象棋

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/WyXrjz 可交互视频 ...

  10. 【js】window.onscroll 无效问题

    body 设置为height:100% 导致window.onscroll 无效