1参考文章
参考1:http://topic.csdn.net/u/20080722/16/e7fcffc3-3d54-48f6-9d3a-2cd39073a287.html
参考2:http://www.java63.com/design_pattern/iterator_pattern.html
参考3:http://blog.csdn.net/aaaaaaaa0705/article/details/6282305
问题起源
想找一些关于迭代器next()和hasNext()方法的内容,最后都归宗到迭代器以及设计模式中的迭代器模式。现在记录一下,有空写一篇学习心得。
一、什么是迭代模式
Iterator模式也叫迭代模式,是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator按顺序进行遍历访问的设计模式。
二、不使用迭代模式的应用
在应用Iterator模式之前,首先应该明白Iterator模式用来解决什么问题。或者说,如果不使用Iterator模式,会存在什么问题。
- 由容器自己实现顺序遍历。直接在容器类里直接添加顺序遍历方法
- 让调用者自己实现遍历。直接暴露数据细节给外部。
三、不使用迭代模式的缺点
以上方法1与方法2都可以实现对遍历,这样有问题呢?
- 容器类承担了太多功能:一方面需要提供添加删除等本身应有的功能;一方面还需要提供遍历访问功能。
- 往往容器在实现遍历的过程中,需要保存遍历状态,当跟元素的添加删除等功能夹杂在一起,很容易引起混乱和程序运行错误等。
四、使用迭代模式的应用
Iterator模式就是为了有效地处理按顺序进行遍历访问的一种设计模式,简单地说,Iterator模式提供一种有效的方法,可以屏蔽聚集对象集合的容器类的实现细节,而能对容器内包含的对象元素按顺序进行有效的遍历访问。
所以,Iterator模式的应用场景可以归纳为满足以下几个条件:
- 访问容器中包含的内部对象
- 按顺序访问
五、迭代模式的结构
六、迭代模式的角色和职责
Iterator(迭代器接口):该接口必须定义实现迭代功能的最小定义方法集,比如提供hasNext()和next()方法。
ConcreteIterator(迭代器实现类): 比如BookShelfIterator,迭代器接口Iterator的实现类。可以根据具体情况加以实现。
Aggregate(容器接口):定义基本功能以及提供类似Iterator iterator()的方法。
ConcreteAggregate(容器实现类): 比如BookShelf,容器接口的实现类。必须实现Iterator iterator()方法。
七、迭代模式的优点
- 实现功能分离,简化容器接口。让容器只实现本身的基本功能,把迭代功能委让给外部类实现,符合类的设计原则。
- 隐藏容器的实现细节。
- 为容器或其子容器提供了一个统一接口,一方面方便调用;另一方面使得调用者不必关注迭代器的实现细节。
- 可以为容器或其子容器实现不同的迭代方法或多个迭代方法。
八、代码实例
容器接口Aggregate
public interface Aggregate { public abstract Iterator iterator(); }
迭代器接口类Iterator
public interface Iterator { public abstract boolean hasNext(); public abstract Object next(); }
Book.java
public class Book { private String name=""; public Book(String name) { this.name = name; } /** * 获得书籍名称 * @return String */ public String getName() { return name; } }
容器接口实现类BookShelf
/** * 书架类 * ConcreateAggregate */ 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 books[index]; } //添加书籍 public void appendBook(Book book) { this.books[last] = book; last++; } //获得书架存书的数量 public int getLength() { return books.length; } //获得书架迭代器对象 @Override public Iterator iterator() { return new BookShelfIterator(this); } }
迭代器接口实现类BookShelfIterator
//ConcreateIterator 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 class Main { public static void main(String[] args) { //生成一个书架 BookShelf bookShelf = new BookShelf(4); //向书架添加书籍 bookShelf.appendBook(new Book("周恩来的晚年岁月")); bookShelf.appendBook(new Book("C++网络编程")); bookShelf.appendBook(new Book("J2EE网络编程精解")); bookShelf.appendBook(new Book("Java编程思想")); //获得书架迭代器 Iterator it = bookShelf.iterator(); while(it.hasNext()) { Book book = (Book)it.next(); System.out.println(book.getName()); } } }
总结
总而言之,迭代器设计模式的总体思想就是“在容器实现类中实现对实体的增删改查操作,而在迭代器实现类中实现遍历操作。”