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

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

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

  怎么做?

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

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

  这时候,我们希望创建一种超集合(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. SGU 258 Almost Lucky Numbers 接近幸运数(数位DP)

    题意: 定义一个具有2n位的正整数,其前n位之和与后n位之和相等,则为lucky数.给定一个区间,问有多少个正数可以通过修改某一位数从而变成lucky数?注意不能含前导0. 思路: 我的想法是记录那些 ...

  2. 洛谷 P1079 Vigenère 密码

    题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...

  3. Codeforces Round #411 div2

    A. Fake NP 题意:询问一个区间[L,R]出现次数最多的正整数因子(>1). 一个区间内一个因子P出现次数大概为[R/P]-[(L-1)/P],约等于(R-L+1)/P,P取2时最优.注 ...

  4. Problem T: 结构体--学生信息排序

    Problem T: 结构体--学生信息排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2219  Solved: 1305[Submit][Sta ...

  5. Django 的母板及布局(Bootstrap)

    title: Django 的母板及布局(Bootstrap) tags: Django --- Django 的母板及布局(Bootstrap) Django 的母板是作为公共的部分,其他的页面都能 ...

  6. Python中文编码问题(字符串前面加'u')

    中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...

  7. errno的用法

    Linux中系统调用的错误都存储于 errno中,errno由操作系统维护,存储就近发生的错误,即下一次的错误码会覆盖掉上一次的错误. 编程时需要包含#include <errno.h>, ...

  8. hibernate简介以及简单配置

    Hibernate简介: Hibernate是一个开源对象关联关系映射的框架,他对JDBC做了轻量级的封装,使我们可以通过面向对象的思想操作数据库. 为什么要用Hibernate: 1: 对JDBC访 ...

  9. linux关于进程、内存和cpu情况

    1.load average: 2.03, 1.76, 1.80 1分钟.5分钟.15分钟平均负载 2.%Cpu(s):100.0 us, 0.0 sy, 0.0 ni, 0.0 id, 0.0 wa ...

  10. jQuery plugin : bgStretcher 背景图片切换效果插件

    转自:http://blog.dvxj.com/pandola/jQuery_bgStretcher.html bgStretcher 2011 (Background Stretcher)是一个jQ ...