目录
1)属于行为模式
2)如果我们的集合元素是用不同的方式实现的,有数组,还有java的集合类,或者还有其他方式,当客户 端要遍历这些集合元素的时候就要使用多种遍历方式,而且还会暴露元素的内部结构,可以考虑迭代器模式解决
3)迭代器模式,提供一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即:不暴露其内部结构
1)Iterator:迭代器接口,是系统提供,含有hasNext, next, remove
2)ConcreteIterator:具体的迭代器类,管理迭代
3)Aggregate:一个统一的聚合接口,将客户端和具体的聚合解耦
4)ConcreateAggreage:具体的聚合,持有对象集合
5)ClientL:客户端,通过Iterator和Aggregate依赖子类
- // 系
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Department {
- private String name;
- private String desc;
- }
- 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;
- }
-
- // 删除的方法默认空实现
- public void remove() {
-
- }
- }
- public class InfoColleageIterator implements Iterator {
-
- List
departmentList;// 信息工程学院是以list的方式存放系 - int index = -1;// 索引
-
- public InfoColleageIterator(List
departmentList) { - this.departmentList = departmentList;
- }
-
- // 判断list中还有没有下一个元素
- @Override
- public boolean hasNext() {
- if (index >= departmentList.size() - 1) {
- return false;
- } else {
- index += 1;
- return true;
- }
- }
-
- @Override
- public Object next() {
- return departmentList.get(index);
- }
-
- // 空实现remove
- public void remove() {
-
- }
- }
- public class ComputerCollege implements College {
- Department[] departments;
- int numOfDepartments = 0;// 保存当前数组的个数
-
- public ComputerCollege() {
- departments = new Department[5];
- addDepartment("java", "java");
- addDepartment("php", "php");
- addDepartment("大数据", "大数据");
- }
-
- @Override
- public String getName() {
- return "计算机学院";
- }
-
- @Override
- public void addDepartment(String name, String desc) {
- Department department = new Department(name, desc);
- departments[numOfDepartments] = department;
- numOfDepartments++;
-
- }
-
- @Override
- public Iterator createIterator() {
- return new ComputerCollegeIterator(departments);
- }
- }
- public class InfoCollege implements College{
-
- List
departmentList; -
- public InfoCollege() {
- departmentList = new ArrayList
(); - 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 InfoColleageIterator(departmentList);
- }
- }
- public interface College {
- public String getName();
-
- // 增加系的方法
- public void addDepartment(String name, String desc);
-
- // 返回一个迭代器遍历
- public Iterator createIterator();
-
- }
- public class OutPutImpl {
- // 学院集合
- List
collegeList; -
- public OutPutImpl(List
collegeList) { - this.collegeList = collegeList;
- }
-
- // 遍历所有的学院,然后调用printDepartment 输出各个学院的系
- public void printCollege() {
- // 从collegeList取出所有学院
- Iterator
iterator = collegeList.iterator(); - while (iterator.hasNext()) {
- // 取出一个学院
- College college = iterator.next();
- System.out.println("College: " + college.getName());
- printDepartment(college.createIterator());
- }
- }
-
- // 学院输出系
- 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) {
- // 创建学院
- ArrayList
colleges = new ArrayList<>(); - ComputerCollege computerCollege = new ComputerCollege();
- InfoCollege infoCollege = new InfoCollege();
-
- colleges.add(computerCollege);
- colleges.add(infoCollege);
-
- OutPutImpl outPut = new OutPutImpl(colleges);
- outPut.printCollege();
- }
- }
1)提供了统一的遍历方法,客户不用再考虑聚合的类型,使用一种方法就可以遍历对象了
2)隐藏了聚合的内部结构,客户端要遍历聚合的时候,只能取到迭代器,而不会周到聚合的具体组成
3)当要展示一组相似对象,或者遍历一组相同对象时使用,适合使用迭代器模式
每个聚合对象都要有一个迭代器,会生成许多迭代器不好管理类