24.Iterator——迭代器模式

demo描述:学院、专业结点呈层级结构,现要遍历并输出各学院下的专业

 

demo代码:

迭代器,复则遍历专业:

public class ComputerColleageIterator implements Iterator {
    Department[] departments;
    //遍历的位置
    int position = 0;

    public ComputerColleageIterator(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;
    //索引
    int index = -1;

    public InfoColleageIterator(List 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);
    }

    //空实现
    public void remove() {}
}

学院接口,管理学院:

public interface Colleage {
    String getName();

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

    //返回一个迭代器,遍历
    Iterator createIterator();
}

具体学院,拥有专业:

public class ComputerColleage implements Colleage {
    Department[] departments;
    //保存当前数组的对象个数
    int numOfDepartment = 0;

    public ComputerColleage() {
        departments = new Department[5];
        addDepartment("java", "专业");
        addDepartment("php", "专业");
        addDepartment("大数据", "专业");
    }

    @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 ComputerColleageIterator(departments);
    }
}

public class InfoColleage implements Colleage {
    List departmentList;

    public InfoColleage() {
        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 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) {his.desc = desc;}
}

OutputImpl,输出被遍历的对象:

public class OutputImpl {
    //学院集合,list有自己的迭代器
    List colleageList;

    public OutputImpl(List collegeList) {
        this.colleageList = collegeList;
    }

    //遍历所有学院,调用printDepartment输出各个学院的专业
    // 学院本身用的java的list的Iterator
    public void printCollege(){
        //java的list已经实现了Iterator
        Iterator iterator = colleageList.iterator();
        while (iterator.hasNext()){
            Colleage college = iterator.next();
            System.out.println(college.getName());
            //得到院系下专业的迭代器
            printDepartment(college.createIterator());
        }
    }

    //输出:学院输出专业
    //学院没用list装着,也没有自己的迭代器,调用此方法的时候需要调接口方法创建迭代器
    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) {
        //创建学院
        List collegeList=new ArrayList();
        ComputerColleage computerColleage=new ComputerColleage();
        InfoColleage infoColleage= new InfoColleage();

        collegeList.add(computerColleage);
        collegeList.add(infoColleage);

        OutputImpl output = new OutputImpl(collegeList);
        output.printCollege();
    }
}

demo类图:

24.Iterator——迭代器模式_第1张图片

类图分析:迭代器是专门用来遍历的,它里面有层级关系,没多一个学院类就多一个相应的迭代器用于其遍历,迭代器使用了JDK自带的迭代器接口以实现对元素的遍历;两个学院用来不同的方式管理专业,其迭代器也不同

 

适用场景:集合元素用不同的方式实现,当遍历这些元素时要用多种遍历方式;要展示一组相似对象或遍历一组相同对象时用;每个聚合类都要一个迭代器,当类多了后不方便管理。

 

总结:迭代器模式提供了一种遍历集合元素的统一接口,用一致的方法遍历集合元素,不需要知道集合对象的底层表示,即不暴露内部结构;聚合类提供一个获得迭代器的方法,让聚合元素的存储与遍历分离,将遍历交给专门的类去做。与组合模式不同的是组合模式是用来存储层级结构的类的,迭代器模式是专门用于遍历的;两者相同之处是都具有层级结构。

你可能感兴趣的