基本介绍
- 是常用的设计模式,属性行为型模式
- 如果我们的集合元素是用不同的方式实现的,有数组,还有java的集合类,或者还有其他方式,当客户端要遍历这些集合元素的时候就要使用多种遍历方式,而且还会暴露元素的内部结构,可以考虑使用迭代器模式解决
- 迭代器模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部结构
角色及职责
- Iterator:迭代器接口,是系统提供,含nextNext,next,remove
- ConcreteIterator:具体的迭代器类,管理迭代
- Aggregate:一个统一的聚合接口,将客户端和具体的聚合解耦
- ConcreteAggregate:具体的聚合,持有对象的集合,并提供一个方法,返回一个迭代器,该迭代器可以正确遍历集合
- Client:客户端,依赖Iterator ,Aggregate的子类
- elemet:集合中的对象
示例代码
场景:有多个学院,学院下有多个系,集合方式使用数组和List,创建对应的迭代器 Iterator:使用Java自带的
element:
public class Department {
private String name;
private String dsc;
public Department(String name, String dsc) {
this.name = name;
this.dsc = dsc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDsc() {
return dsc;
}
public void setDsc(String dsc) {
this.dsc = dsc;
}
}
ConcreteIterator
public class ComputerCollegeIterator implements Iterator {
private Department[] departments;
private int position;
public ComputerCollegeIterator(Department[] departments) {
this.departments = departments;
}
@Override
public boolean hasNext() {
if (position >= departments.length || departments[position] == null){
return false;
}
return true;
}
@Override
public Object next() {
Department department = departments[position];
position++;
return department;
}
@Override
public void remove() {
}
}
public class InfoCollegeIterator implements Iterator {
private List<Department> departments;
private int index = -1;
public InfoCollegeIterator(List<Department> departments) {
this.departments = departments;
}
@Override
public boolean hasNext() {
if (index >= departments.size()-1){
return false;
}
index++;
return true;
}
@Override
public Object next() {
return departments.get(index);
}
@Override
public void remove() {
}
}
Aggregate:
public interface College {
String getName();
void addDepartment(String name,String dsc);
Iterator createIterator();
}
ConcreteAggregate
public class ComputerCollege implements College{
private Department[] departments;
private int numOfDepartment = 0;
public ComputerCollege() {
this.departments = new Department[5];
addDepartment("Java","java 专业");
addDepartment("PHP","PHP 专业");
addDepartment("大数据","大数据 专业");
}
@Override
public String getName() {
return "计算机学院";
}
@Override
public void addDepartment(String name, String dsc) {
Department department = new Department(name, dsc);
departments[numOfDepartment] = department;
numOfDepartment++;
}
@Override
public Iterator createIterator() {
return new ComputerCollegeIterator(departments);
}
}
public class InfoCollege implements College{
private List<Department> departmentList;
public InfoCollege() {
this.departmentList = new ArrayList<>();
this.departmentList.add(new Department("信息安全专业","信息安全专业"));
this.departmentList.add(new Department("网络安全专业","网络安全专业"));
this.departmentList.add(new Department("服务器安全专业","服务器安全专业"));
}
@Override
public String getName() {
return "信息工程学院";
}
@Override
public void addDepartment(String name, String dsc) {
Department department = new Department(name, dsc);
departmentList.add(department);
}
@Override
public Iterator createIterator() {
return new InfoCollegeIterator(departmentList);
}
}
定义一个类完成输出
public class OutputImpl {
List<College> collegeList;
public OutputImpl(List<College> collegeList) {
this.collegeList = collegeList;
}
public void printCollege(){
Iterator<College> iterator = collegeList.iterator();
while (iterator.hasNext()){
College college = iterator.next();
System.out.println("======="+college.getName()+"=======");
Iterator collegeIterator = college.createIterator();
printDepartment(collegeIterator);
}
}
public void printDepartment(Iterator iterator){
while (iterator.hasNext()){
Department department = (Department) iterator.next();
System.out.println(department.getName());
}
}
}
客户端调用实现
public class Client {
public static void main(String[] args) {
List<College> collegeArrayList = new ArrayList<>();
ComputerCollege computerCollege = new ComputerCollege();
InfoCollege infoCollege = new InfoCollege();
collegeArrayList.add(computerCollege);
collegeArrayList.add(infoCollege);
OutputImpl output = new OutputImpl(collegeArrayList);
output.printCollege();
}
}
输出:
=======计算机学院=======
Java
PHP
大数据
=======信息工程学院=======
信息安全专业
网络安全专业
服务器安全专业
注意事项和细节
优点:
- 提供了一个统一的方法遍历对象,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象了
- 隐藏了聚合的内部结构,客户端要遍历聚合的时候只能取迭代器,而不会自动聚合的具体组成
- 提供了一种设计思想,就是一个类一个只有一个引起变化的原因(叫做单一责任原则)。在聚合类中,我们把迭代器分开,就是要把管理对象集合和遍历对象集合的责任分开,这样一来集合改变的话,只影响到聚合对象。而如果遍历方式改变的话,只影响到了迭代器
- 当要展示一组相似对象,或者遍历一组相同对象时使用,适合使用迭代器模式
缺点
- 每个聚合对象都要一个迭代器,会生成对个迭代器不好管理类
|