概念:

  Iterator模式也叫迭代模式,是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator(遍历)按顺序进行遍历访问的设计模式。

  迭代模式使用比较少,JDK集合也提供了Iterator的具体实现,可以直接拿来用,不必自己实现

  在应用Iterator模式之前,首先应该明白Iterator模式用来解决什么问题。或者说,如果不使用Iterator模式,会存在什么问题。

   1.由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法

   2.让调用者自己实现遍历。直接暴露数据细节给外部

  我们用代码实现一下

  首先,新建一个Book类,这是容器中的内容

 public class Book {
private String id;
private String name;
private double price; public Book(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public void display() {
System.out.println("ID=" + id + ",\tname=" + name + ",\tprice" + price);
}
}

  再新建一个容器

 public class BookList {
//容器内部还是一个List,也可以用数组
private List<Book> bookList = new ArrayList<Book>();
private int index; //添加书籍
public void addBook(Book book){
bookList.add(book);
} //删除书籍
public void removeBook(Book book){
int bookIndex = bookList.indexOf(book);
bookList.remove(bookIndex);
} //判断是否有下一本书
public boolean hasNext(){
if(index >= bookList.size()){
return false;
}
return true;
} //获得下一本书
public Book getNext(){
return bookList.get(index++);
} //获取集合长度
public int getSize(){
return bookList.size();
} //根据index获取Book
public Book getByIndex(int index){
return bookList.get(index);
}
}

  接下来,就是迭代容器了,先采用第一种方式(由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法)

public class MainClass {
public static void main(String[] args) {
BookList bookList = new BookList(); Book book1 = new Book("001","设计模式",200);
Book book2 = new Book("002","Java核心编程",200);
Book book3 = new Book("003","计算机组成原理",200); bookList.addBook(book1);
bookList.addBook(book2);
bookList.addBook(book3); while(bookList.hasNext()){
Book book = bookList.getNext();
book.display();
}
}
}

  结果如下:

  然后是第二种方式(让调用者自己实现遍历。直接暴露数据细节给外部)

 public class MainClass {
public static void main(String[] args) {
BookList bookList = new BookList(); Book book1 = new Book("001","设计模式",200);
Book book2 = new Book("002","Java核心编程",200);
Book book3 = new Book("003","计算机组成原理",200); bookList.addBook(book1);
bookList.addBook(book2);
bookList.addBook(book3); for(int i = 0; i < bookList.getSize(); i++){
Book book = bookList.getByIndex(i);
book.display();
}
}
}

  结果同上

  但这样,一定是有缺点的,不然就没有必要使用迭代模式了

  不使用迭代模式的缺点

  以上方法1与方法2都可以实现对遍历,但有这些问题

  1,容器类承担了太多功能:一方面需要提供添加删除等本身应有的功能;一方面还需要提供遍历访问功能。

  2,往往容器在实现遍历的过程中,需要保存遍历状态,当跟元素的添加删除等功能夹杂在一起,很容易引起混乱和程序运行错误等。

  应用迭代模式的条件

  Iterator模式就是为了有效地处理按顺序进行遍历访问的一种设计模式,简单地说,Iterator模式提供一种有效的方法,可以屏蔽聚集对象集合的容器类的实现细节,而能对容器内包含的对象元素按顺序进行有效的遍历访问。

  所以,Iterator模式的应用场景可以归纳为满足以下几个条件:

  1、访问容器中包含的内部对象
  2、按顺序访问

  迭代模式的结构

  

  迭代模式的角色和职责

  1、Iterator(迭代器接口)

  该接口必须定义实现迭代功能的最小定义方法集

  比如提供hasNext()和next()方法。

  2、ConcreteIterator(迭代器实现类)

  迭代器接口Iterator的实现类。可以根据具体情况加以实现。

  3、Aggregate(容器接口)

  定义基本功能以及提供类似Iterator iterator()的方法。

  4、concreteAggregate(容器实现类)

  容器接口的实现类。必须实现Iterator iterator()方法。

  接下来,用代码实现一下迭代模式,只需修改BookList即可

 public class BookList {
//容器内部还是一个List,也可以用数组
private List<Book> bookList = new ArrayList<Book>();
private int index; //添加书籍
public void addBook(Book book){
bookList.add(book);
} //删除书籍
public void removeBook(Book book){
int bookIndex = bookList.indexOf(book);
bookList.remove(bookIndex);
} //判断是否有下一本书
// public boolean hasNext(){
// if(index >= bookList.size()){
// return false;
// }
// return true;
// } //获得下一本书
// public Book getNext(){
// return bookList.get(index++);
// } //获取集合长度
public int getSize(){
return bookList.size();
} //根据index获取Book
public Book getByIndex(int index){
return bookList.get(index);
} //得到Iterator实例
public Iterator Iterator() {
return new Itr();
} //内部类,Iterator实例(因为要使用容器的内部信息,所以要写成内部类)
private class Itr implements Iterator{
//判断是否有下一本书,将刚才hasNext()中内容复制过来即可
public boolean hasNext() {
if(index >= bookList.size()){
return false;
}
return true;
}
//获得下一本书,将刚才getNext()中内容复制过来即可
public Object next() {
return bookList.get(index++);
} public void remove() { }
}
}

  再在客户端实现一下

 public class MainClass {
public static void main(String[] args) {
BookList bookList = new BookList(); Book book1 = new Book("001","设计模式",200);
Book book2 = new Book("002","Java核心编程",200);
Book book3 = new Book("003","计算机组成原理",200); bookList.addBook(book1);
bookList.addBook(book2);
bookList.addBook(book3); Iterator iterator = bookList.Iterator();
while(iterator.hasNext()){
Book book = (Book) iterator.next();
book.display();
}
}
}

  可以看到,这和使用JDK提供集合的Iterator方法就一模一样了。

  迭代模式的优缺点

  优点:

  1,实现功能分离,简化容器接口。让容器只实现本身的基本功能,把迭代功能委让给外部类实现,符合类的设计原则。

  2,隐藏容器的实现细节。

  3,为容器或其子容器提供了一个统一接口,一方面方便调用;另一方面使得调用者不必关注迭代器的实现细节。

  4,可以为容器或其子容器实现不同的迭代方法或多个迭代方法。

  缺点:

  由于迭代器模式将存储数据和遍历数据的职责分离,增加新的聚合类需要对应增加新的迭代器类,类的个数成对增加,这在一定程度上增加了系统的复杂性。

java设计模式-----19、迭代模式的更多相关文章

  1. Java设计模式——装饰者模式

    JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...

  2. Java设计模式之代理模式(静态代理和JDK、CGLib动态代理)以及应用场景

    我做了个例子 ,需要可以下载源码:代理模式 1.前言: Spring 的AOP 面向切面编程,是通过动态代理实现的, 由两部分组成:(a) 如果有接口的话 通过 JDK 接口级别的代理 (b) 如果没 ...

  3. 浅析JAVA设计模式之工厂模式(一)

    1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...

  4. JAVA设计模式--装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

  5. 折腾Java设计模式之建造者模式

    博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...

  6. 折腾Java设计模式之备忘录模式

    原文地址:折腾Java设计模式之备忘录模式 备忘录模式 Without violating encapsulation, capture and externalize an object's int ...

  7. 折腾Java设计模式之状态模式

    原文地址 折腾Java设计模式之状态模式 状态模式 在状态模式(State Pattern)中,类的行为是基于它的状态改变的.这种类型的设计模式属于行为型模式.在状态模式中,我们创建表示各种状态的对象 ...

  8. 折腾Java设计模式之模板方法模式

    博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...

  9. 折腾Java设计模式之访问者模式

    博客原文地址:折腾Java设计模式之访问者模式 访问者模式 Represent an operation to be performed on the elements of an object st ...

  10. 折腾Java设计模式之命令模式

    博客原文地址 折腾Java设计模式之命令模式 命令模式 wiki上的描述 Encapsulate a request as an object, thereby allowing for the pa ...

随机推荐

  1. mybatis源码追踪2——将结果集映射为map

    org.apache.ibatis.binding.MapperMethod中execute方法 ...} else if (method.returnsMap()) { result = execu ...

  2. Windows下的Nginx安装与配置(PHP)

    因为一直用Apache作为服务器,对Apache的使用和配置已经相对熟悉,今天换了一下nginx的服务器,整个配置流程相对比较简单,php的配置没有任何变化. 主要的参考文档为 http://blog ...

  3. Mybatis常用知识点总结

    1. #{}和${}的区别是什么? ${}是Properties文件中的变量占位符,它可以用于标签属性值和sql内部,属于静态文本替换,比如${driver}会被静态替换为com.mysql.jdbc ...

  4. 尺寸单位em,rem,vh,vw

    这几天做demo,看了网上教程有用到尺寸单位vh,vw, 这些单位不是很熟悉,所以上网上找了些资料来认识了这些不认识的单位 1.em 在做手机端的时候经常会用到的做字体的尺寸单位 说白了 em就相当于 ...

  5. 使用git时出现Please make sure you have the correct access rights and the repository exists.问题已解决。

    使用git时,出现Please make sure you have the correct access rights and the repository exists.问题已解决. 今天我在使用 ...

  6. ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

    Struts2未配置Log4j2.xml报错 Log4j2.xml中的配置 log4j的jar包:log4j-core-2.7.jar log4j2只支持xml和json两种格式的配置,所以配置log ...

  7. UIDevice currentDevice model possible values

    NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the sa ...

  8. org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.

    当我用Springboot和mybatis进行延迟加载时候报出如下的错误: org.apache.ibatis.executor.loader.javassist.JavassistProxyFact ...

  9. 《Algorithms算法》笔记:优先队列(1)——API和初等实现

    1.优先队列的API和初等实现 做一个总结: 栈 :先进后出 队列 :先进先出 随机队列 : 随机出 优先队列:每次出来的是最大值或最小值 1.1优先队列的API 优先队列在很多场合都有用, 比如:在 ...

  10. 【转】Spark源码分析之-Storage模块

    原文地址:http://blog.csdn.net/aiuyjerry/article/details/8595991 Storage模块主要负责数据存取,包括MapReduce Shuffle中间结 ...