设计模式行为模式之迭代子模式

    xiaoxiao2026-08-04  4

    1 基础知识

    1.1 标准定义

    迭代子模式标准定义:提供一种方法顺序访问一个聚合对象 中各个元素,而又不需暴露该对象的内部表示。

    1.2 分析和说明

    迭代子模式 称迭代器模式,属于对象行为型模式。迭代子模式可以顺序访问一个聚集中的元素而不必暴露聚集的内部表象。多个对象聚在一起形成的总体称之为聚集,聚集对象是能够包容一组对象的容器对象。迭代子模式将迭代逻辑封装到一个独立的子对象中,从而与聚集本身隔开。迭代子模式简化了聚集的界面。每一个聚集对象都可以有一个或一个以上的迭代子对象,每一个迭代子的迭代状态可以是彼此独立的。迭代算法可以独立于聚集角色变化。

    迭代子模式角色包括迭代子角色,具体迭代子角色,聚集角色,具体聚集角色和客户端角色。

    抽象迭代子(Iterator)角色:此抽象角色定义出遍历元素所需的接口。

    具体迭代子角色:此角色实现了抽象迭代子接口,并保持迭代过程中的游标位置。

    聚集角色:此抽象角色给出创建迭代子(Iterator)对象的接口。

    具体聚集角色:实现了创建迭代子(Iterator)对象的接口,返回一个合适的具体迭代子实例。

    客户端角色:持有对聚集及其迭代子对象的引用,调用迭代子对象的迭代接口,也有可能用过迭代子操作聚集元素的增加和删除。

    2 应用场景举例

    比如公司想统计一下所有员工中有硕士文凭的人数和他们的姓名。可以把所有员工都在放到一个集合中,然后一个一个地询问是否是硕士,这样就可以获得由多少个硕士了。

    可以把Iterator抽象类理解为抽象迭代子角色。ImplementIterator类是具体迭代子角色。EmployeeCollection类是具体聚集角色,其类图如下:ImplementIterator类实现Iterator接口并关联EmployeeCollection。Employee类聚合EmployeeCollection类,即EmployeeCollection包容多个Employee.

    3 Java的实现程序代码

    Java程序实现主要包括Iterator抽象类文件,ImplimentIerator类文件,EmployeeCollection类文件和Employee类文件

    public abstract class Iterator { public Employee next(){return null;} public Employee previous(){return null;} }

    public class ImplementIterator extends Iterator{ private EmployeeCollection employeeCollection; private int currentIndex; public ImplementIterator(EmployeeCollection collection){ this.employeeCollection = collection; currentIndex = collection.getEmployeeMax()-1; } public Employee next(){ if(currentIndex ==-1){ return null; } return employeeCollection.getEmployee(currentIndex--); } }

    public class EmployeeCollection { List<Employee> employeeList = new ArrayList<Employee>(); private int employeeMax= 0; public EmployeeCollection addEmployee(Employee employee){ employeeList.add(employee); employeeMax++; return this; } public Employee getEmployee(int i){ return employeeList.get(i); } public int getEmployeeMax(){ return employeeMax; } public void setEmployeeMax(int employeeMax){ this.employeeMax = employeeMax; } }

    public class Employee { private String employeeName; private String eduation; public Employee(String name, String eduation){ this.employeeName = name; this.eduation = eduation; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEduation() { return eduation; } public void setEduation(String eduation) { this.eduation = eduation; } }

    迭代子模式测试代码为:

    public class Client { public static void main(String[] args){ Employee employee; Employee employee1= new Employee("小王","学士"); Employee employee2= new Employee("小张","学士"); Employee employee3= new Employee("小刘","硕士"); Employee employee4= new Employee("小黄","博士"); Employee employee5= new Employee("小马","硕士"); EmployeeCollection employeeCollection = new EmployeeCollection(); employeeCollection.addEmployee(employee1).addEmployee(employee2); employeeCollection.addEmployee(employee3).addEmployee(employee4); employeeCollection.addEmployee(employee5); ImplementIterator iterator = new ImplementIterator(employeeCollection); do{ employee = iterator.next(); if(employee !=null){ if(employee.getEduation().equals("硕士")){ System.out.print(employee.getEmployeeName()+";"); } } }while(employee != null); } }

    转载请注明原文地址: https://ju.6miu.com/read-1310855.html
    最新回复(0)