设计模式笔记12:迭代器模式(Iterator Pattern)

来源:互联网 发布:js css display none 编辑:程序博客网 时间:2024/05/09 20:36

一、迭代器模式的内容

迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的标示。其别名为游标(Cursor)。迭代器模式是一种对象行为型模式。

从定义可见,迭代器模式是为容器而生。很明显,对容器对象的访问必然涉及到遍历算法。你可以一股脑的将遍历方法塞到容器对象中去;或者根本不去提供什么遍历算法,让使用容器的人自己去实现去吧。这两种情况好像都能够解决问题。

然而在前一种情况,容器承受了过多的功能,它不仅要负责自己“容器”内的元素维护(添加、删除等等),而且还要提供遍历自身的接口;而且由于遍历状态保存的问题,不能对同一个容器对象同时进行多个遍历。第二种方式倒是省事,却又将容器的内部细节暴露无遗。

二、迭代器的结构


1) 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口。
2) 具体迭代器角色(Concrete Iterator):具体迭代器角色要实现迭代器接口,并要记录遍历中的当前位置。
3) 容器角色(Container):容器角色负责提供创建具体迭代器角色的接口。
4) 具体容器角色(Concrete Container):具体容器角色实现创建具体迭代器角色的接口——这个具体迭代器角色于该容器的结构相关。

三、迭代器模式分析

  • 聚合是一个管理和组织数据对象的数据结构。
  • 聚合对象主要拥有两个职责:一是存储内部数据;二是遍历内部数据。 
  • 存储数据是聚合对象最基本的职责。
  • 将遍历聚合对象中数据的行为提取出来,封装到一个迭代器中,通过专门的迭代器来遍历聚合对象的内部数据,这就是迭代器模式的本质。迭代器模式是“单一职责原则”的完美体现。(迭代器就是将遍历内部数据的功能放在了内部实现)一个类应该只有一个引起变化的原因。根据类设计的开闭原则,我们知道要避免类内的改变,因为修改代码很容易造成许多的错误。如果有一个类具有两个改变的原因,那么这会使得将来该类的变化几率上升,而当它真的改变时,你的设计中同时有两个方面将会受到影响。
  • 迭代器模式中应用了工厂方法模式,聚合类充当工厂类,而迭代器充当产品类,由于定义了抽象层,系统的扩展性很好,在客户端可以针对抽象聚合类和抽象迭代器进行编程。
  • 由于很多编程语言的类库都已经实现了迭代器模式,因此在实际使用中我们很少自定义迭代器,只需要直接使用Java、C#等语言中已定义好的迭代器即可,迭代器已经成为我们操作聚合对象的基本工具之一。

四、迭代器模式代码示例

代码一
interface MyCollection{MyIterator createIterator();}interface MyIterator{void first();void next();boolean isLast();Object currentItem();}class NewCollection implements MyCollection{   private Object[] obj={"dog","pig","cat","monkey","pig"};   public MyIterator createIterator()   {    return new NewIterator();   }   private class NewIterator implements MyIterator   {   private int currentIndex=0;      public void first()   {     currentIndex=0;   }public void next(){if(currentIndex<obj.length){currentIndex++;}}public void previous(){if(currentIndex>0){currentIndex--;}}public boolean isLast(){return currentIndex==obj.length;}public boolean isFirst(){return currentIndex==0;}public Object currentItem(){return obj[currentIndex];}   }}class Client{public static void process(MyCollection collection){MyIterator i=collection.createIterator();while(!i.isLast()){System.out.println(i.currentItem().toString());i.next();}}public static void main(String a[]){MyCollection collection=new NewCollection();process(collection);}}
代码二、JDK中Iterator
import java.util.*;public class IteratorDemo{   public static void process(Collection c)   {     Iterator i=c.iterator();while(i.hasNext()){System.out.println(i.next().toString());}   }public static void main(String args[]){Collection list=new HashSet();list.add("Cat");list.add("Dog");list.add("Pig");list.add("Dog");list.add("Monkey");process(list);}}
代码三、
public interface TVIterator{void setChannel(int i);void next();void previous();boolean isLast();Object currentChannel();    boolean isFirst();}
public interface Television{TVIterator createIterator();}
public class TCLTelevision implements Television{private Object[] obj={"湖南卫视","北京卫视","上海卫视","湖北卫视","黑龙江卫视"};public TVIterator createIterator(){return new TCLIterator();}   class TCLIterator implements TVIterator{   private int currentIndex=0;    public void next(){if(currentIndex<obj.length){currentIndex++;}}public void previous(){if(currentIndex>0){currentIndex--;}}public void setChannel(int i){currentIndex=i;}public Object currentChannel(){return obj[currentIndex];}public boolean isLast(){return currentIndex==obj.length;}public boolean isFirst(){return currentIndex==0;}}}
public class SkyworthTelevision implements Television{private Object[] obj={"CCTV-1","CCTV-2","CCTV-3","CCTV-4","CCTV-5","CCTV-6","CCTV-7","CCTV-8"};public TVIterator createIterator(){return new SkyworthIterator();}private class SkyworthIterator implements TVIterator{   private int currentIndex=0;    public void next(){if(currentIndex<obj.length){currentIndex++;}}public void previous(){if(currentIndex>0){currentIndex--;}}public void setChannel(int i){currentIndex=i;}public Object currentChannel(){return obj[currentIndex];}public boolean isLast(){return currentIndex==obj.length;}public boolean isFirst(){return currentIndex==0;}}}
import javax.xml.parsers.*;import org.w3c.dom.*;import org.xml.sax.SAXException;import java.io.*;public class XMLUtil{//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象public static Object getBean(){try{//创建文档对象DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = dFactory.newDocumentBuilder();Document doc;doc = builder.parse(new File("config.xml")); //获取包含类名的文本节点NodeList nl = doc.getElementsByTagName("className");            Node classNode=nl.item(0).getFirstChild();            String cName=classNode.getNodeValue();                        //通过类名生成实例对象并将其返回            Class c=Class.forName(cName);      Object obj=c.newInstance();            return obj;           }              catch(Exception e)           {           e.printStackTrace();           return null;           }}}
<?xml version="1.0"?><config><className>SkyworthTelevision</className></config>
public class Client{public static void display(Television tv){TVIterator i=tv.createIterator();System.out.println("电视机频道:");while(!i.isLast()){System.out.println(i.currentChannel().toString());i.next();}}public static void reverseDisplay(Television tv){TVIterator i=tv.createIterator();i.setChannel(5);System.out.println("逆向遍历电视机频道:");while(!i.isFirst()){i.previous();System.out.println(i.currentChannel().toString());}}public static void main(String a[]){Television tv;tv=(Television)XMLUtil.getBean();display(tv);System.out.println("--------------------------");reverseDisplay(tv);}}

五、Java迭代器

在JDK中,Iterator接口具有如下3个基本方法:
(1) Object next():通过反复调用next()方法可以逐个访问聚合中的元素。
(2) boolean hasNext():hasNext()方法用于判断聚合对象中是否还存在下一个元素,为了不抛出异常,必须在调用next()之前先调用hasNext()。如果迭代对象仍然拥有可供访问的元素,那么hasNext()返回true。
(3) void remove():用于删除上次调用next()时所返回的元素。 
Java迭代器可以理解为它工作在聚合对象的各个元素之间,每调用一次next()方法,迭代器便越过下个元素,并且返回它刚越过的那个元素的地址引用。但是,它也有一些限制,如某些迭代器只能单向移动。在使用迭代器时,访问某个元素的唯一方法就是调用next()。

六、迭代器模式优缺点

迭代器模式的优点
  • 它支持以不同的方式遍历一个聚合对象。
  • 迭代器简化了聚合类。
  • 在同一个聚合上可以有多个遍历。
  • 在迭代器模式中,增加新的聚合类和迭代器类都很方便,无须修改原有代码,满足“开闭原则”的要求。

迭代器模式的缺点

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

七、迭代器模式适用环境

在以下情况下可以使用迭代器模式:
  1. 访问一个聚合对象的内容而无须暴露它的内部表示。
  2. 需要为聚合对象提供多种遍历方式。
  3. 为遍历不同的聚合结构提供一个统一的接口。

八、迭代器模式应用

  • JDK 1.2 引入了新的Java聚合框架Collections。 
  • Collection是所有Java聚合类的根接口。
  • 在JDK类库中,Collection的iterator()方法返回一个java.util.Iterator类型的对象,而其子接口java.util.List的listIterator()方法返回一个java.util.ListIterator类型的对象,ListIterator是Iterator的子类。它们构成了Java语言对迭代器模式的支持,Java语言的java.util.Iterator接口就是迭代器模式的应用。 

Iterator (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)

  • All implementations of java.util.Iterator (thus among others also java.util.Scanner!).
  • All implementations of java.util.Enumeration

九、参考资料

  1. 《设计模式》刘伟主编清华大学出版社
  2. 《Head First 设计模式》
  3. http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns