一、迭代器模式介绍
如果我们的集合元素是用不同的方法实现的,有数组,还有java的集合类或者还有其他方法,当客户端要遍历这些集合元素的时候,就要使用多种遍历方式,而且还会暴露元素的内部结构,可以考虑使用迭代器模式解决。
迭代器模式提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需知道集合元素对象的底层表示,即不暴露其内部的结构。
二、迭代器原理图
说明:
1)Iterator:迭代器接口,是系统提供的。含有hasNext,next,remove
2)ConcreteIterator:具体的迭代器类,管理迭代
3)Aggregate:一个统一的聚合接口,将客户端和具体聚合解耦
4)ConcreteAggreage:具体的聚合,持有对象集合,并提供一个方法,返回一个迭代器,该迭代器可以正确遍历集合
5)Client:客户端,通过Iterator和Aggreagete依赖子类
三、迭代器模式例子
编写程序展示一个学校院系结构,需求是这样:要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。类图如下所示:
代码如下所示:
创建Department类:
public class Department{
private String name;
private String desc;
public Department(String name, String desc){
this.name = name;
this.desc = desc;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getDesc(){
return desc;
}
public void setDesc(String desc){
this.desc = desc;
}
}
创建Iterator子类:
public class ComputerCollegeIterator implements Iterator{
//需要知道Department是以怎样的方式存放
private Department[] departments;
//遍历的位置
private int position = 0;
public ComputerCollegeIterator(Department[] deparments){
this.dpeartments = departments
}
public boolean hasNext(){
if(position >= departments.length || departments[position] == null){
return false;
} else {
return true;
}
}
public Object next(){
Department department = departments[position];
position += 1;
return department;
}
//删除的方法默认空实现
public void remove(){
}
}
public class InfoCollegeIterator implements Iterator{
//需要知道Department是以怎样的方式存放
private List<Department> departments;
//遍历的位置
private int position = -1;
public InfoCollegeIterator(List<Department> deparments){
this.dpeartments = departments
}
public boolean hasNext(){
if(position >= departments.size() - 1){
return false;
} else {
position += 1;
return true;
}
}
public Object next(){
return departments.get(position);
}
//删除的方法默认空实现
public void remove(){
}
}
创建College接口及其子类:
public interface College{
String getName();
//增加系的方法
void addDepartment(String name, String desc);
//返回一个迭代器,遍历
Iterator createIterator();
}
public ComputerCollege implements College{
private Department[] departments;
//保存当前数据的对象个数
private int num;
public String getName(){
return "计算机学院"
}
public ComputerCollege(){
departments = new Department[5];
addDepartment("软件工程", "软件工程");
addDepartment("计算机科学与技术", "计算机科学与技术");
}
public void addDepartment(String name, String desc){
Department department = new Department(name, desc);
departments[num] = department;
num += 1;
}
public Iterator createIterator{
return new ComputerCollegeIterator(departments);
}
}
public InfoCollege implements College{
private List<Department> departments;
public String getName(){
return "信息学院"
}
public ComputerCollege(){
departments = new ArrayList<>();
addDepartment("信息安全", "信息安全");
addDepartment("电子科学与技术", "电子科学与技术");
addDepartment("网络安全", "网络安全");
}
public void addDepartment(String name, String desc){
Department department = new Department(name, desc);
departments.add(department);
}
public Iterator createIterator{
return new InfoCollegeIterator(departments);
}
}
创建OutPutImpl类:
public class OutPutImpl{
private List<College> colleges;
public OutPutImpl(List<College> colleges){
this.colleges = colleges;
}
//遍历所有学院,然后调用printDepartment输出各个学院的系
public void printCollege(){
//从Colleges取出所有学院,Java中的List已经实现了Iterator
Iterator<College> iterator = colleges.iterator();
while(iterator.hasNext()){
College college = iterator.next();
System.out.println("===========" + college.getName() + "==============");
printDepartment(college.createIterator());
}
}
//学院输出 系
public void printDepartment(Iterator iterator){
while(iterator.hasNext()){
Department department = (Department)iterator.next();
System.out.println(department.getName());
}
}
}
创建Client类:
public class Client{
public static void main(String[] args){
List<College> collegeList = new ArrayList<College>();
ComputerCollege computerCollege = new ComputerCollege();
InfoCollege infoCollege = new InfoCollege();
collegeList.add(computerCollege);
collegeList.add(infoCollege);
OutPutImpl output = new OutPutImpl(collegeList);
output.printCollege();
}
}
四、迭代器模式在JDK中使用
JDK的ArrayList集合中就使用了迭代器模式。类图如下所示:
角色分析说明:
1)内部类Itr充当具体实现迭代器Iterator的类,作为ArrayList内部类
2)List就是充当了聚合接口,含有一个iterator()方法,返回一个迭代器对象
3)ArrayList是实现聚合接口List的子类,实现了iterator()方法
4)Iterator接口是系统提供的
5)迭代器模式解决了不同集合(ArrayList、LinkedList)统一遍历问题
五、迭代器模式的注意事项和细节
优点:
1)提供一个统一的方法遍历对象,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象了
2)隐藏了聚合的内部结构,客户端要遍历聚合的时候,只能取到迭代器,而不会知道聚合的具体组成
3)提供了一种设计思想,就是一个类应该只有一个引起变化的原因(叫做单一责任原则)。在聚合类中,我们把迭代器分开,就是要把管理对象集合和遍历对象集合的责任分开,这样一来集合改变的话,只影响到聚合对象,而如果遍历方法改变的话,只影响了迭代器
4)当要展示一组相似对象,或者遍历一组相同对象时,适合使用迭代器模式
缺点:
每个聚合对象都要一个迭代器,会生成多个迭代器,不好管理