15. iterative mode

Iterator pattern

First, the iterator pattern

1. Basic Introduction

1) If we set elements are implemented in different ways, there is an array, as well as a collection of java classes, etc., when the client to traverse the collection of elements necessary to use a variety of ways to traverse, but also exposed the internal structural elements, It can be considered solved using an iterative mode.

2) to provide a uniform interface through the collection element, consistent way through the collection element, the collection need not know the underlying representation of the object.

2. The principle of class diagram

1) Iterator: iterator interface, the system is provided, meaning hasNext, next, remove

2) Concretelterator: specific iterator class, the management iteration

3) Aggregate: a unified interface to the polymerization, the polymerization and the specific client Decoupling

4) ConcreteAggreage: a set of specific polymerization holding the object, and to provide a method that returns an iterator, which iterator can iterate through the collection

5) Client: The client, through the Iterator and subclasses dependent Aggregate

Second, the iterative mode Application Examples

Write a program to show the structure of a school faculties: To demonstrate the school's faculty in a page composition, a school more than college more than a college department.

1. code implementation

public class Client {
    public static void main(String[] args) {
        //创建学院
        List<College> collegeList = new ArrayList<College>();

        ComputerCollege computerCollege = new ComputerCollege();
        InfoCollege infoCollege = new InfoCollege();
        
        collegeList.add(computerCollege);
        //collegeList.add(infoCollege);
        
        OutPutImpl outPutImpl = new OutPutImpl(collegeList);
        outPutImpl.printCollege();
    }
}
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("大数据专业", " 大数据专业 ");
        
    }
    @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 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 InfoColleageIterator(departmentList);
    }
}
/**
 * 迭代器
 */
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<Department> departmentList; // 信息工程学院是以List方式存放系
    int index = -1;//索引
    
    public InfoColleageIterator(List<Department> 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 OutPutImpl {
    //学院集合
    List<College> collegeList;

    public OutPutImpl(List<College> collegeList) {
        this.collegeList = collegeList;
    }
    //遍历所有学院,然后调用printDepartment 输出各个学院的系
    public void printCollege() {
        //从collegeList 取出所有学院, Java 中的 List 已经实现Iterator
        Iterator<College> iterator = collegeList.iterator();
        
        while(iterator.hasNext()) {
            //取出一个学院
            College college = iterator.next();
            System.out.println("=== "+college.getName() +"=====" );
            printDepartment(college.createIterator()); //得到对应迭代器
        }
    }

    //输出 学院输出 系
    public void printDepartment(Iterator iterator) {
        while(iterator.hasNext()) {
            Department d = (Department)iterator.next();
            System.out.println(d.getName());
        }
    }
}

Third, the iterator pattern Notes

1) to provide a unified approach to traverse the object, the client regardless of the type of aggregate.

2) Mode for using an iterator when to show a set of similar objects, or objects traversing the same set.

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11333808.html