其他分享
首页 > 其他分享> > 一篇文章掌握迭代器模式

一篇文章掌握迭代器模式

作者:互联网

一.案例引入

具体描述

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

二.迭代器模式

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.优缺点分析

在这里插入图片描述

标签:String,迭代,departments,name,文章,Department,desc,public,一篇
来源: https://blog.csdn.net/afdafvdaa/article/details/113921058