[toc]

迭代器模式的类图

类图的解释

名称 说明
Aggregate 集合接口,有提供迭代器的方法
Iterator 迭代器接口,提供迭代操作
BookShelf 书架类
Book 书类
BookShelfIterator 书架类的迭代器

迭代器模式的代码

//集合接口
public interface Aggregate {
//生成迭代器的方法
public abstract Iterator iterator();
} //迭代器接口
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
} //书架类
public class BookShelf implements Aggregate {
private Book[] books;
private int last = 0;
public BookShelf(int maxSize) {
this.books = new Book[maxSize];
}
public Book getBookAt(int index) {
return this.books[index];
}
public void appendBook(Book book) {
this.books[last] = book;
last++;
}
public int getLength() {
return last;
}
//获得BookShelf迭代器
public Iterator iterator() {
return new BookShelfIterator(this);
}
} //书类
public class Book {
private String name;
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} //书架迭代器类
public class BookShelfIterator implements Iterator {
private BookShelf bookShelf;
private int index;
public BookShelfIterator(BookShelf bookShelf) {
this.bookShelf = bookShelf;
this.index = 0;
}
public boolean hasNext() {
if (index < bookShelf.getLength()) {
return true;
} else {
return false;
}
}
public Object next() {
Book book = bookShelf.getBookAt(index);
index++;
return book;
}
} //测试方法
public static void main(String[] args) {
BookShelf bookShelf = new BookShelf(4);
bookShelf.appendBook(new Book("西游记"));
bookShelf.appendBook(new Book("红楼梦"));
bookShelf.appendBook(new Book("水浒传"));
bookShelf.appendBook(new Book("三国演义")); Iterator iterator = bookShelf.iterator();
while (iterator.hasNext()) {
Book book = (Book)iterator.next();
System.out.println(book.getName());
}
}

解释

  • BookShelf相当于一个Book的集合,Aggregate接口相当于“集合操纵工具”的工具箱,BookShelf实现了Aggregate接口,就可以从工具箱里面取出集合操纵工具。

  • Iterator是集合的遍历工具,BookShelfIterator实现Iterator接口,就拥有了遍历集合的能力,经过特殊化修改,成为专门遍历BookShelf的工具。

  • 这样,我们就可以从BookShelf中取得Book集合遍历工具BookShelfIterator,使用它遍历这个BookShelf了。

原因

  • 既然是遍历一个集合,直接写一个for循环不就行了,为什么要搞这么麻烦?

  • 因为:集合可以是一个数组,也可以是一个Vector,还可以是一个list,每种的遍历方式都不一样。如果要把数组换成list来用,那么整套代码都得重写。而用了迭代器模式后,只需要改变具体的迭代方法的实现即可。

思想

  • 将通用的方法与具体的实现分离。

  • 弱化类之间的耦合,使类作为组件更容易被复用。

  • 尽量避免用具体的类来编程,优先使用抽象类和接口。

  • 说白了,写代码不要写的太具体,要抽象些...改着也好改。

《图解设计模式》读书笔记1-1 Iterator模式的更多相关文章

  1. HeadFirst设计模式读书笔记(3)-装饰者模式(Decorator Pattern)

    装饰者模式:动态地将责任附件到对象上.若要扩展功能,装饰者提东了比继承更有弹性的替代方案. 装饰者和被装饰对象有相同的超类型 你可以用一个或者多个装饰者包装一个对象. 既然装饰者和被装饰对象有相同的超 ...

  2. HeadFirst设计模式读书笔记--目录

    HeadFirst设计模式读书笔记(1)-策略模式(Strategy Pattern) HeadFirst设计模式读书笔记(2)-观察者模式(Observer Pattern) HeadFirst设计 ...

  3. Head First 设计模式读书笔记(1)-策略模式

    一.策略模式的定义 策略模式定义了算法族,分别封装起来,让它们之间可以互换替换,此模式让算法的变化独立使用算法的客户. 二.使用策略模式的一个例子 2.1引出问题 某公司做了一套模拟鸭子的游戏:该游戏 ...

  4. JavaScript设计模式:读书笔记(未完)

    该篇随我读书的进度持续更新阅读书目:<JavaScript设计模式> 2016/3/30 2016/3/31 2016/4/8 2016/3/30: 模式是一种可复用的解决方案,可用于解决 ...

  5. 图解http读书笔记

    以前对HTTP协议一知半解,一直不清楚前端需要对于HTTP了解到什么程度,知道接触的东西多了,对于性能优化.服务端的配合和学习中也渐渐了解到了HTTP基础的重要性,看了一些大神对HTTP书籍的推荐,也 ...

  6. Java设计模式学习笔记(二) 简单工厂模式

    前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 正文开始... 1. 简介 简单工厂模式不属于GoF23中设计模式之一,但在软件开发中应用也较为 ...

  7. Java设计模式学习笔记(三) 工厂方法模式

    前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 简介 上一篇博客介绍了简单工厂模式,简单工厂模式存在一个很严重的问题: 就是当系统需要引入 ...

  8. Java设计模式学习笔记(四) 抽象工厂模式

    前言 本篇是设计模式学习笔记的其中一篇文章,如对其他模式有兴趣,可从该地址查找设计模式学习笔记汇总地址 1. 抽象工厂模式概述 工厂方法模式通过引入工厂等级结构,解决了简单工厂模式中工厂类职责太重的问 ...

  9. C#设计模式学习笔记:(23)解释器模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/8242238.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第十一个模式-- ...

  10. 设计模式C++描述----20.迭代器(Iterator)模式

    一. 举例说明 我们知道,在 STL 里提供 Iterator 来遍历 Vector 或者 List 数据结构. Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 ...

随机推荐

  1. Java相关面试题总结+答案(五)

    [异常] 74. throw 和 throws 的区别? throw 是真实抛出一个异常: throws 是声明可能会抛出一个异常. 75. final.finally.finalize 有什么区别? ...

  2. [Git] 009 逆转未来

    1. 想逆转未来,得先知道时间线 1.1 git log 1.2 git log --oneline 此命令的显示结果比 git log 简洁 1.3 git reflog 此命令显示的记录比前两者完 ...

  3. 10 个常用的 Linux 命令?

    pwd 显示工作路径ls 查看目录中的文件 cd /home 进入 '/ home' 目录'cd .. 返回上一级目录cd ../.. 返回上两级目录mkdir dir1 创建一个叫做 'dir1' ...

  4. python学习第四十六天dir( )函数用法

    dir( )函数有点像目录的意思,但是他是包含由模块定义的名称的字符串的排序列表.这个列表包含模块中定义的所有模块,变量和函数的名称. 列举其用法 import time content = dir( ...

  5. Sobel硬件实现的硬件代码分析(三)

    #include "xaxivdma.h" #include "xaxivdma_i.h" #include "xhls_sobel.h" ...

  6. MVC框架与MTC框架

    3.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...

  7. ReentrantLock等待通知机制Condition介绍

    Object类中的wait(),notify()和notifyAll()可以实现线程的等待通知模型,同样在ReentrantLock中可以借助Condition来完成这种机制.本篇就简要介绍Condi ...

  8. C# 获取一个文件的MD5值

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  9. 11JSP基础

    1.Jsp基础 1.1 简介 Jsp,全称 Java Server Page java服务页面,能提供java服务的页面 jsp vs html html: 由html标签组成的,输出静态内容. js ...

  10. man(2) W

    wait(2) wait3(2) wait4(2) waitpid(2) waitid(2) SEE ALSO _exit(2), clone(2), fork(2), kill(2), ptrace ...