设计模式-迭代器模式(Iterator Pattern)
推荐:Java设计模式汇总
迭代器模式
定义
提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor)。
类型
行为型。
角色
- 抽象迭代器(Iterator):抽象迭代器负责定义访问和遍历元素的接口。
- 具体迭代器(ConcreteIterator):提供具体的元素遍历行为。
- 抽象容器(Aggregate):负责定义提供具体迭代器的接口。
- 具体容器(ConcreteAggregate):创建具体迭代器。
例子
这里举课程的例子。
Course类(课程类),实体类。
package com.kaven.design.pattern.behavioral.iterator;
public class Course {
private String name;
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
CourseIterator接口(抽象迭代器)。
package com.kaven.design.pattern.behavioral.iterator;
public interface CourseIterator {
Course nextCourse();
boolean isLastCourse();
}
CourseIteratorImpl类(具体迭代器),实现了CourseIterator接口,方法名的命名不用纠结。
package com.kaven.design.pattern.behavioral.iterator;
import java.util.List;
public class CourseIteratorImpl implements CourseIterator {
private List courseList;
private int position;
private Course course;
public CourseIteratorImpl(List courseList) {
this.courseList = courseList;
}
public Course nextCourse() {
System.out.println("返回课程,位置是:"+position);
course = (Course) courseList.get(position);
position++;
return course;
}
public boolean isLastCourse() {
if(position < courseList.size()){
return false;
}
return true;
}
}
CourseAggregate接口(抽象容器)。
package com.kaven.design.pattern.behavioral.iterator;
public interface CourseAggregate {
void addCourse(Course course);
void removeCourse(Course course);
CourseIterator getCourseIterator();
}
CourseAggregateImpl类(具体容器),实现了CourseAggregate接口。
package com.kaven.design.pattern.behavioral.iterator;
import java.util.ArrayList;
import java.util.List;
public class CourseAggregateImpl implements CourseAggregate {
private List courseList;
public CourseAggregateImpl() {
this.courseList = new ArrayList();
}
public void addCourse(Course course) {
courseList.add(course);
}
public void removeCourse(Course course) {
courseList.remove(course);
}
public CourseIterator getCourseIterator() {
return new CourseIteratorImpl(courseList);
}
}
应用层代码:
package com.kaven.design.pattern.behavioral.iterator;
public class Test {
public static void main(String[] args) {
Course course = new Course("设计模式");
Course course1 = new Course("数据结构");
Course course2 = new Course("机器学习");
Course course3 = new Course("算法");
Course course4 = new Course("计算机网络");
Course course5 = new Course("RabbitMQ");
Course course6 = new Course("Docker");
CourseAggregate courseAggregate = new CourseAggregateImpl();
courseAggregate.addCourse(course);
courseAggregate.addCourse(course1);
courseAggregate.addCourse(course2);
courseAggregate.addCourse(course3);
courseAggregate.addCourse(course4);
courseAggregate.addCourse(course5);
courseAggregate.addCourse(course6);
System.out.println("课程列表--------");
printCourses(courseAggregate);
courseAggregate.removeCourse(course2);
courseAggregate.removeCourse(course5);
printCourses(courseAggregate);
}
public static void printCourses(CourseAggregate courseAggregate){
CourseIterator courseIterator = courseAggregate.getCourseIterator();
while (!courseIterator.isLastCourse()){
Course course = courseIterator.nextCourse();
System.out.println(course.getName());
}
}
}
输出:
课程列表--------
返回课程,位置是:0
设计模式
返回课程,位置是:1
数据结构
返回课程,位置是:2
机器学习
返回课程,位置是:3
算法
返回课程,位置是:4
计算机网络
返回课程,位置是:5
RabbitMQ
返回课程,位置是:6
Docker
返回课程,位置是:0
设计模式
返回课程,位置是:1
数据结构
返回课程,位置是:2
算法
返回课程,位置是:3
计算机网络
返回课程,位置是:4
Docker
这里便实现了一个简单的迭代器模式的例子,还是比较简单的。
适用场景
- 需要将聚合对象的访问与内部数据的存储分离,使得访问聚合对象时无须了解其内部实现细节。
- 需要为一个聚合对象提供多种遍历方式。
- 需要为遍历不同的聚合结构提供一个统一的接口,在该接口的实现类中为不同的聚合结构提供不同的遍历方式,而应用层可以一致性地操作该接口。
优点
- 它支持以不同的方式遍历一个聚合对象,在同一个聚合对象上可以定义多种遍历方式。
- 在迭代器模式中只需要用一个不同的迭代器来替换原有迭代器即可改变遍历算法,我们也可以自己定义迭代器的子类以支持新的遍历方式。
- 迭代器模式简化了聚合类。由于引入了迭代器,在原有的聚合对象中不需要再自行提供数据遍历等方法,这样可以简化聚合类的设计。
- 在迭代器模式中,由于引入了抽象层,增加新的聚合类和迭代器类都很方便,无须修改原有代码,符合 “开闭原则”。
缺点
- 由于迭代器模式将存储数据和遍历数据的职责分离,增加新的聚合类需要对应增加新的迭代器类,类的个数成对增加,这在一定程度上增加了系统的复杂性。
- 抽象迭代器的设计难度较大,需要充分考虑到系统将来的扩展,例如JDK内置迭代器Iterator就无法实现逆向遍历,如果需要实现逆向遍历,只能通过其子类ListIterator等来实现,而ListIterator迭代器无法用于操作Set类型的聚合对象。在自定义迭代器时,创建一个考虑全面的抽象迭代器并不是件很容易的事情。
如果有说错的地方,请大家不吝赐教(记得留言哦~~~~)。
参考博客
设计模式 | 迭代器模式及典型应用
迭代器模式
迭代器模式
还没有评论,来说两句吧...