1、例子
分析:
1)
将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分
层次的
2)
实际上我们的要求
是 :在一个页面中展示出学校的院系组成,一个学校有多个
学院,一个学院有多个系, 因此这种方案,不能很好实现
的遍历的
操作
3)
解决方案:
=>
迭代器
模式
2、基本定义
1)
迭代器模式(
Iterator Pattern
)是常用的设计模式,属于行为型模式
2)
如果我们的
集合元素是用不同的方式实现
的,有数组,还有
java
的集合类,
或者还有其他方式,当客户端要
遍历这些集合元素
的时候就要使用多种遍历
方式,而且还会暴露元素的内部结构,可以考虑使用迭代器模式解决。
3)
迭代器模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,
不需要知道集合对象的底层表示,即:不暴露其内部的结构。
总结:解决元素存储类型不同
![](https://i-blog.csdnimg.cn/blog_migrate/f880721d53da18dfb991fa3b04e7252c.png)
1) Iterator : 迭代器接口,是系统提供,含义 hasNext, next, remove
2) ConcreteIterator : 具体的迭代器类,管理迭代
3) Aggregate :一个统一的聚合接口, 将客户端和具体聚合解耦
4) ConcreteAggreage:具体的聚合持有对象集合
3、代码
public class Department {
private String name;
private String 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;
}
public Department(String name, String desc) {
this.name = name;
this.desc = desc;
}
}
public interface College {
public String getName();
//增加系的方法
public void addDepartment(String name,String desc);
//返回一个迭代器,遍历
public Iterator createIterator();
}
public class ComputerCollege implements College {
Department[] departments;
int numOfDepartment=0;//保存当前数组的对象个数
public ComputerCollege() {
departments=new Department[5];
addDepartment("Java专业","JAVA专业");
addDepartment("PHP专业","PHP专业");
addDepartment("大数据专业","大数据专业");
addDepartment("JS专业","JS专业");
}
@Override
public String getName() {
return "计算几学院";
}
@Override
public void addDepartment(String name, String desc) {
Department department=new Department(name,desc);
departments[numOfDepartment]=department;
numOfDepartment+=1;
}
@Override
public Iterator createIterator() {
return new ComputerCollegeIterator(departments);
}
}
public class ComputerCollegeIterator implements Iterator {
//需要Department是以怎么样的方式存放
Department[] departments;
int position=0;//遍历位置
public ComputerCollegeIterator(Department[] departments) {
this.departments = departments;
}
@Override
public boolean hasNext() {
if(position>=departments.length||departments[position]==null){
return false;
}else{
return true;
}
}
@Override
public Object next() {
Department department=departments[position];
position+=1;
return department;
}
@Override
public void remove() {
}
}
public class InfoCollege implements College {
List<Department> departmentList;
public InfoCollege() {
departmentList=new ArrayList<Department>();
addDepartment("信息安全专业","信息安全专业");
addDepartment("服务网络安全专业","服务网络安全专业");
addDepartment("服务器安全专业","服务器安全专业");
}
@Override
public String getName() {
return "信息工程学院";
}
@Override
public void addDepartment(String name, String desc) {
Department department=new Department(name,desc);
departmentList.add(department);
}
@Override
public Iterator createIterator() {
return new InfoCollegeIterator(departmentList);
}
}
public class InfoCollegeIterator implements Iterator {
List<Department> departmentList;
int index=-1;//索引
public InfoCollegeIterator(List<Department> departmentList) {
this.departmentList = departmentList;
}
@Override
public boolean hasNext() {
if(index>=departmentList.size()-1){
return false;
}else{
index+=1;
return true;
}
}
@Override
public Object next() {
return departmentList.get(index);
}
@Override
public void remove() {
}
}
public class OutPutImpl {
//学院集合
List<College> collegeList;
public void printCollege(){
//从collegeList 取出所有学院
Iterator<College> iterator = collegeList.iterator();
while (iterator.hasNext()){
//取出一个学院
College college = iterator.next();
System.out.println("==="+college.getName()+"=====");
printDepartment(college.createIterator());
}
}
public OutPutImpl(List<College> collegeList) {
this.collegeList = collegeList;
}
public void printDepartment(Iterator iterator){
while (iterator.hasNext()){
Department d = (Department) iterator.next();
System.out.println(d.getName());
}
}
}
public class Client {
public static void main(String[] args) {
ArrayList<College> colleges = new ArrayList<>();
ComputerCollege computerCollege = new ComputerCollege();
InfoCollege infoCollege = new InfoCollege();
colleges.add(computerCollege);
colleges.add(infoCollege);
new OutPutImpl(colleges).printCollege();
}
}
总结:方法和数据 隔离