一篇文章掌握迭代器模式

一.案例引入

具体描述

在这里插入图片描述
之前用组合模式解决过这个问题,这次针对遍历。

二.迭代器模式

1.基本介绍

在这里插入图片描述

2.原理类图

在这里插入图片描述

解释

在这里插入图片描述

3.迭代器模式解决上述问题的UML类图

在这里插入图片描述

4.具体代码

College
public interface College {
    public String getName();

    //增加系的方法
    public void addDepartment(String name,String desc);

    //返回一个迭代器
    public Iterator createIterator();
}
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;
    }
}
ComputerCollegeIterator
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(){

    }
}
InfoCollegeIterator
public class InfoCollegeIterator implements Iterator {
   //信息工程学院是以List方式存放
    List<Department> departments;
    int index = -1;

    public InfoCollegeIterator(List<Department> departments) {
        this.departments = departments;
    }

    @Override
    public boolean hasNext() {
        if(index >= departments.size() -1){
            return false;
        }else {
            index += 1;
            return true;
        }
    }


    @Override
    public Object next() {
        return departments.get(index);
    }

    public void remove(){}
}
ComputerCollege
public class ComputerCollege implements College{
    Department[] departments;
    int numOfDepartment = 0;//保存当前数组的对象个数

    public ComputerCollege(){
        departments = new Department[5];
        addDepartment("Java专业","Java专业");
        addDepartment("Python专业","Python专业");
    }
    @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);
    }
}
InfoCollege
public class InfoCollege implements College{
    List<Department> departments;

    public InfoCollege(){
        departments = new ArrayList<Department>();
        addDepartment("信息安全专业","信息安全专业");
        addDepartment("网络安全专业","网络安全专业");
    }
    @Override
    public String getName() {
        return "信息工程学院";
    }

    @Override
    public void addDepartment(String name, String desc) {
        Department department = new Department(name, desc);
        departments.add(department);
    }

    @Override
    public Iterator createIterator() {
        return new InfoCollegeIterator(departments);
    }
}
OutPutImpl
public class OutPutImpl {
    //学院集合
    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 next = iterator.next();
            System.out.println(next.getName());
            printDepartment(next.createIterator());//得到对应的迭代器
        }
    }
    //先解决学院输出系的问题
    public void printDepartment(Iterator iterator){
        while (iterator.hasNext()){
            Department next = (Department)iterator.next();
            System.out.println(next.getName());
        }
    }
}
Client
public class Client {
    public static void main(String[] args) {
        //创建学院
        List<College> colleges = new ArrayList<>();
        ComputerCollege computerCollege = new ComputerCollege();
        InfoCollege infoCollege = new InfoCollege();

        colleges.add(computerCollege);
        colleges.add(infoCollege);

        OutPutImpl outPut = new OutPutImpl(colleges);
        outPut.printCollege();
    }
}

计算机学院
Java专业
Python专业
信息工程学院
信息安全专业
网络安全专业

5.优缺点分析

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值