迭代器模式
需求案例
使用传统的设计方案
传统解决方案存在的问题
迭代器模式的基本介绍
迭代器模式的原理类图
迭代器模式应用实例
代码实现
package com.sky.iterator;
import java.util.Iterator;
// 学院接口
public interface College {
// 获取学院的名字
String getCollegeName();
// 添加院系
void addDepartment(Department department);
// 获取迭代器
Iterator getIterator();
}
package com.sky.iterator;
import java.util.Iterator;
// 学院的实现类
public class ComputerCollege implements College {
private Department[] departments;
private int num = 0; // 记录数组的对象数
public ComputerCollege(){
departments = new Department[4];
departments[0] = new Department("计算机科学与技术","计算机科学与技术描述");
departments[1] = new Department("软件工程","软件工程描述");
departments[2] = new Department("网络工程","网络工程描述");
departments[3] = new Department("大数据","大数据描述");
}
@Override
public String getCollegeName() {
return "计算机学院";
}
@Override
public void addDepartment(Department department) {
departments[num++] = department;
}
@Override
public Iterator getIterator() {
return new ComputerIterator(departments);
}
}
package com.sky.iterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class InfoManagerCollege implements College { private List<Department> list; public InfoManagerCollege(){ list = new ArrayList<>(); list.add(new Department("信息分析","信息分析描述")); list.add(new Department("信息统计","信息统计描述")); list.add(new Department("信息抽取","信息抽取描述")); } @Override public String getCollegeName() { return "信息工程学院"; } @Override public void addDepartment(Department department) { list.add(department); } @Override public Iterator getIterator() { return new InfoManagerIterator(list); } }
package com.sky.iterator;
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;
}
}
package com.sky.iterator;
import java.util.Iterator;
/**
* 计算机学院 的 迭代器
*/
public class ComputerIterator implements Iterator {
private Department[] departments; // 假设计算机学院中的系是以数组的形式保存的
private int index; // 下标 遍历的时候用到
public ComputerIterator(Department[] departments){
this.departments = departments;
}
// 判断遍历的时候,是否还存在元素
@Override
public boolean hasNext() {
if(index >= departments.length || departments[index] == null){
return false;
}
return true;
}
// 获取下一个元素
@Override
public Object next() {
return departments[index++];
}
// 移除元素
@Override
public void remove() {
// 空实现
}
}
package com.sky.iterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 信息管理学院 */ public class InfoManagerIterator implements Iterator { private List<Department> departmentList = new ArrayList<>(); // 假设信息管理学院中的系是以List集合的方式存储的 private int position; // 下标 遍历的时候用到 public InfoManagerIterator(List<Department> departmentList){ this.departmentList = departmentList; } // 判断遍历的时候,是否还存在元素 @Override public boolean hasNext() { if(position > departmentList.size()-1 || departmentList.get(position) == null){ return false; } return true; } // 获取下一个元素 @Override public Object next() { return departmentList.get(position++); } // 移除元素 @Override public void remove() { // 空实现 } }
package com.sky.iterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; // 打印学院院系 public class OutputCollegeImpl { private List<College> collegeList; public OutputCollegeImpl() { collegeList = new ArrayList<>(); collegeList.add(new ComputerCollege()); collegeList.add(new InfoManagerCollege()); } // 打印学院以及下面的院系 public void getCollegeAllDepartment() { Iterator<College> iterator = collegeList.iterator(); while (iterator.hasNext()) { // 获取到学院 College college = iterator.next(); System.out.println("==========" + college.getCollegeName() + "========="); // 获取学院的迭代器 // 计算机学院用的数组存储,信息管理学院用的列表存储,通过迭代器可统一使用 Iterator collegeIterator = college.getIterator(); while (collegeIterator.hasNext()){ // 获取到院系 Department department = (Department) collegeIterator.next(); System.out.println(department.getName() + ":" + department.getDesc()); } } } }
package com.sky.iterator;
// 客户端
public class Client {
public static void main(String[] args) {
OutputCollegeImpl outputCollege = new OutputCollegeImpl();
outputCollege.getCollegeAllDepartment();
}
}
==========计算机学院=========
计算机科学与技术:计算机科学与技术描述
软件工程:软件工程描述
网络工程:网络工程描述
大数据:大数据描述
==========信息工程学院=========
信息分析:信息分析描述
信息统计:信息统计描述
信息抽取:信息抽取描述
迭代器模式的注意事项和细节