一、理论
迭代器模式(Iterator Pattern)目前已经是一个没落的模式,基本上没人会单独写一个迭代器,除非是产品性质的开发,其定义如下:
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
它提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。
- iterator(迭代器)
抽象迭代器负责定义访问和遍历元素的接口,而且基本上是有固定的3个方法:first()获得第一个元素,next()访问下一个元素,isDone()是否已经访问到底部(Java叫做hasNext()方法)。 - ConcreteIterator(具体的迭代器)
具体迭代器角色要实现迭代器接口,完成容器元素的遍历。 - Aggregate(抽象容器)
容器角色负责提供创建具体迭代器角色的接口,必然提供一个类似createIterator()这样的方法,在Java中一般是iterator()方法。 - ConcreteAggregate(具体容器)
具体容器实现容器接口定义的方法,创建出容纳迭代器的对象。
如果我们的集合元素是用不同的方式实现的,有数组,还有 java 的集合类,或者还有其他方式,当客户端要遍历这些集合元素的时候就要使用多种遍历方式,而且还会暴露元素的内部结构,可以考虑使用迭代器模式解决。
二、代码
书架上面有很多书,遍历每一本书
iterator(迭代器):
public interface IIterator {
/**
* 是否还有下一个(坐标)
*
* @return
*/
boolean hasNext();
/**
* 返回当前对象,指针指向下一个(坐标)
*
* @return
*/
Object next();
}
ConcreteIterator(具体的迭代器):
public class BookAggregateIterator implements IIterator {
private BookAggregate bookAggregate;
private int index;
public BookAggregateIterator(BookAggregate bookAggregate) {
this.bookAggregate = bookAggregate;
this.index = 0;
}
@Override
public boolean hasNext() {
return index < bookAggregate.getLength();
}
@Override
public Object next() {
Book book = bookAggregate.getAt(index);
index++;
return book;
}
}
Aggregate(抽象容器):
public interface IAggregate {
IIterator iterator();
}
ConcreteAggregate(具体容器):
@Data
@AllArgsConstructor
public class Book {
private String name;
private String author;
}
public class BookAggregate implements IAggregate {
private Book[] books;
private int last = 0;
public BookAggregate(int maxSize) {
this.books = new Book[maxSize];
}
public void addBook(Book book) {
this.books[last] = book;
last++;
}
public int getLength() {
return last;
}
public Book getAt(int index) {
return books[index];
}
@Override
public IIterator iterator() {
return new BookAggregateIterator(this);
}
}
客户端:
public class Client {
public static void main(String[] args) {
BookAggregate bookAggregate = new BookAggregate(4);
Book book1 = new Book("钢铁是怎样炼成的", "保尔");
Book book2 = new Book("基督山伯爵", "大仲马");
Book book3 = new Book("三体", "刘慈欣");
Book book4 = new Book("JAVA核心", "java");
bookAggregate.addBook(book1);
bookAggregate.addBook(book2);
bookAggregate.addBook(book3);
bookAggregate.addBook(book4);
IIterator iterator = bookAggregate.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
三、总结
- 优点
- 提供一个统一的方法遍历对象,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象了。
- 隐藏了聚合的内部结构,客户端要遍历聚合的时候只能取到迭代器,而不会知道聚合的具体组成。
- 提供了一种设计思想,就是一个类应该只有一个引起变化的原因(叫做单一责任原则)。在聚合类中,我们把迭代器分开,就是要把管理对象集合和遍历对象集合的责任分开,这样一来集合改变的话,只影响到聚合对象。而如果遍历方式改变的话,只影响到了迭代器。
- 当要展示一组相似对象,或者遍历一组相同对象时使用, 适合使用迭代器模式
- 缺点
每个聚合对象都要一个迭代器,会生成多个迭代器不好管理类