迭代器模式(Iterator Pattern):提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor)。迭代器模式是一种对象行为型模式。
Iterator(抽象迭代器
角色分析Aggregate(抽象聚合类):它用于存储和管理元素对象,声明一个createIterator()方法用于创建一个迭代器对象,充当抽象迭代器工厂角色。
Iterator(抽象迭代器):它定义了访问和遍历元素的接口,声明了用于遍历数据元素的方法,例如:用于获取第一个元素的first()方法,用于访问下一个元素的next()方法,用于判断是否还有下一个元素的hasNext()方法,用于获取当前元素的currentItem()方法等,在具体迭代器中将实现这些方法。
ConcreteIterator(具体迭代器):它实现了抽象迭代器接口,完成对聚合对象的遍历,同时在具体迭代器中通过游标来记录在聚合对象中所处的当前位置,在具体实现时,游标通常是一个表示位置的非负整数。
Aggregate(抽象聚合类):它用于存储和管理元素对象,声明一个createIterator()方法用于创建一个迭代器对象,充当抽象迭代器工厂角色。
ConcreteAggregate(具体聚合类):它实现了在抽象聚合类中声明的createIterator()方法,该方法返回一个与该具体聚合类对应的具体迭代器ConcreteIterator实例。
在迭代器模式中,提供了一个外部的迭代器来对聚合对象进行访问和遍历,迭代器定义了一个访问该聚合元素的接口,并且可以跟踪当前遍历的元素,了解哪些元素已经遍历过而哪些没有。迭代器的引入,将使得对一个复杂聚合对象的操作变得简单。
在迭代器模式中应用了工厂方法模式,抽象迭代器对应于抽象产品角色,具体迭代器对应于具体产品角色,抽象聚合类对应于抽象工厂角色,具体聚合类对应于具体工厂角色。
实现一个学生报数的示例
uml图
@Getter
@Setter
@ToString
public class Student {
private String name;
private Integer number;
public Student(String name, Integer number) {
this.name = name;
this.number = number;
}
public void count() {
System.out.println(String.format("我是 %d 号 %s", this.number, this.name));
}
}
//抽象聚合
public interface StudentAggregate {
void addStudent(Student student);
void removeStudent(Student student);
StudentIterator getStudentIterator();
}
//具体聚合
public class StudentAggregateImpl implements StudentAggregate {
private List<Student> list; // 学生列表
public StudentAggregateImpl() {
this.list = new ArrayList<Student>();
}
@Override
public void addStudent(Student student) {
this.list.add(student);
}
@Override
public void removeStudent(Student student) {
this.list.remove(student);
}
@Override
public StudentIterator getStudentIterator() {
return new StudentIteratorImpl(list);
}
}
//定义迭代器接口并实现迭代器
public interface StudentIterator {
boolean hashNext();
Student next();
}
public class StudentIteratorImpl implements StudentIterator{
private List<Student> list;
private int position = 0;
private Student currentStudent;
public StudentIteratorImpl(List<Student> list) {
this.list = list;
}
@Override
public boolean hashNext() {
return position < list.size();
}
@Override
public Student next() {
currentStudent = list.get(position);
position ++;
return currentStudent;
}
}
//测试,进行报数
public class Test {
public static void main(String[] args) {
StudentAggregate classOne = new StudentAggregateImpl();
classOne.addStudent(new Student("张三", 1));
classOne.addStudent(new Student("李四", 2));
classOne.addStudent(new Student("王五", 3));
classOne.addStudent(new Student("赵六", 4));
// 遍历,报数
StudentIterator iterator = classOne.getStudentIterator();
while (iterator.hashNext()){
Student student = iterator.next();
student.count();
}
}
}
迭代器模式的主要优点如下:
-
它支持以不同的方式遍历一个聚合对象,在同一个聚合对象上可以定义多种遍历方式。在迭代器模式中只需要用一个不同的迭代器来替换原有迭代器即可改变遍历算法,我们也可以自己定义迭代器的子类以支持新的遍历方式。
-
迭代器简化了聚合类。由于引入了迭代器,在原有的聚合对象中不需要再自行提供数据遍历等方法,这样可以简化聚合类的设计。
-
在迭代器模式中,由于引入了抽象层,增加新的聚合类和迭代器类都很方便,无须修改原有代码,满足 "开闭原则" 的要求。
迭代器模式的主要缺点如下:
-
由于迭代器模式将存储数据和遍历数据的职责分离,增加新的聚合类需要对应增加新的迭代器类,类的个数成对增加,这在一定程度上增加了系统的复杂性。
-
抽象迭代器的设计难度较大,需要充分考虑到系统将来的扩展,例如JDK内置迭代器Iterator就无法实现逆向遍历,如果需要实现逆向遍历,只能通过其子类ListIterator等来实现,而ListIterator迭代器无法用于操作Set类型的聚合对象。在自定义迭代器时,创建一个考虑全面的抽象迭代器并不是件很容易的事情。
适用场景:
-
访问一个聚合对象的内容而无须暴露它的内部表示。将聚合对象的访问与内部数据的存储分离,使得访问聚合对象时无须了解其内部实现细节。
-
需要为一个聚合对象提供多种遍历方式。
-
为遍历不同的聚合结构提供一个统一的接口,在该接口的实现类中为不同的聚合结构提供不同的遍历方式,而客户端可以一致性地操作该接口。