设计模式 行为型模式 实例 -- 迭代器模式实例:迭代数组

Posted CodeJiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式 行为型模式 实例 -- 迭代器模式实例:迭代数组相关的知识,希望对你有一定的参考价值。

01:设计模式 行为型模式 – 迭代器模式(定义、结构、实例、优缺点分析)
02:迭代器模式实例
03:设计模式 行为型模式 – 迭代器模式 JDK源码解析:ArrayList

1. 迭代器模式实例


1.1 需求分析

一个数组中存在StudentTeacher两种对象,
现有两种访问需求:
一个是访问所有学生,一个是访问所有人员,
请用迭代器模式实现两种访问,并打印出人员的基本信息.

1.2 类图分析

类图分析:


1.3 代码实现


1.3.1 People、Student、Teacher

People:

package ex08.iterator.role;

/**
 * ClassName: People
 * Description: People类
 *
 * @author Tianjiao
 * @date 2021/11/16 12:30
 */
public class People 

Student:

package ex08.iterator.role;

/**
 * ClassName: Student
 * Description: 学生类
 *
 * @author Tianjiao
 * @date 2021/11/16 10:30
 */
public class Student extends People 
    private String stuName;
    private String stuNo;


    public String getStuName() 
        return stuName;
    

    public void setStuName(String stuName) 
        this.stuName = stuName;
    

    public String getStuNo() 
        return stuNo;
    

    public void setStuNo(String stuNo) 
        this.stuNo = stuNo;
    

    public Student(String stuName, String stuNo) 
        this.stuName = stuName;
        this.stuNo = stuNo;
    

    @Override
    public String toString() 
        return "Student" +
                "stuName='" + stuName + '\\'' +
                ", stuNo='" + stuNo + '\\'' +
                '';
    

Teacher:

package ex08.iterator.role;

/**
 * ClassName: Teacher
 * Description:
 *
 * @author Tianjiao
 * @date 2021/11/16 10:30
 */
public class Teacher extends People 
    private String teaName;
    private String teaNo;

    public String getTeaName() 
        return teaName;
    

    public void setTeaName(String teaName) 
        this.teaName = teaName;
    

    public String getTeaNo() 
        return teaNo;
    

    public void setTeaNo(String teaNo) 
        this.teaNo = teaNo;
    

    public Teacher(String teaName, String teaNo) 
        this.teaName = teaName;
        this.teaNo = teaNo;
    

    @Override
    public String toString() 
        return "Teacher" +
                "teaName='" + teaName + '\\'' +
                ", teaNo='" + teaNo + '\\'' +
                '';
    


1.3.2 PeopleAggregate : 抽象聚合角色

package ex08.iterator;

import ex08.iterator.role.People;

/**
 * ClassName: PeopleAggregate
 * Description: 抽象聚合角色:定义存储、添加、删除聚合元素以及创建迭代器对象的接口。
 *
 * @author Tianjiao
 * @date 2021/11/16 12:36
 */
public interface PeopleAggregate 

    //添加功能
    void addPeople(People people);

    //删除功能
    void removePeople(People people);

    //获取迭代器对象功能
    PeopleIterator getPeopleIterator();

    //获取迭代器对象功能
    PeopleIterator getStuIterator();


1.3.3 PeopleAggregateImpl:具体聚合角色

package ex08.iterator;

import ex08.iterator.role.People;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: PeopleAggregateImpl
 * Description:具体聚合角色:实现抽象聚合类,返回一个具体迭代器的实例。
 *
 * @author Tianjiao
 * @date 2021/11/16 12:36
 */
public class PeopleAggregateImpl implements PeopleAggregate 
    List<People> peopleList = new ArrayList<>();

    @Override
    public void addPeople(People people) 
        peopleList.add(people);
    

    @Override
    public void removePeople(People people) 
        peopleList.remove(people);
    

    @Override
    public PeopleIterator getPeopleIterator() 
        return new PeopleIteratorImpl(peopleList);
    

    @Override
    public PeopleIterator getStuIterator() 
        return new StudentIteratorImpl(peopleList);
    


1.3.4 PeopleIterator:抽象迭代器角色

package ex08.iterator;

import ex08.iterator.role.People;

/**
 * ClassName: PeopleIterator
 * Description: 抽象迭代器角色:定义访问和遍历聚合元素的接口,通常包含 hasNext()、next() 等方法。
 *
 * @author Tianjiao
 * @date 2021/11/16 12:26
 */
public interface PeopleIterator 
    //判断是否还有元素
    boolean hasNext();

    //获取下一个元素
    People next();


1.3.5 PeopleIteratorImpl、StudentIteratorImpl :具体迭代器角色

PeopleIteratorImpl:

package ex08.iterator;

import ex08.iterator.role.People;

import java.util.List;

/**
 * ClassName: PeopleIteratorImpl
 * Description: 具体迭代器角色:实现抽象迭代器接口中所定义的方法,完成对聚合对象的遍历,记录遍历的当前位置。
 *
 * @author Tianjiao
 * @date 2021/11/16 12:27
 */
public class PeopleIteratorImpl implements PeopleIterator 
    private final List<People> peopleList;
    private int position = 0;//用来记录遍历时的位置
    
    public PeopleIteratorImpl(List<People> peopleList) 
        this.peopleList = peopleList;
    

    @Override
    public boolean hasNext() 
        return position < peopleList.size();
    

    @Override
    public People next() 
        //从集合中获取指定位置的元素
        return peopleList.get(position++);
    

StudentIteratorImpl :

package ex08.iterator;

import ex08.iterator.role.People;
import ex08.iterator.role.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: StudentIterator
 * Description:
 *
 * @author Tianjiao
 * @date 2021/11/22 17:20
 */
public class StudentIteratorImpl implements PeopleIterator 
    List<People> studentList = new ArrayList<>();
    private int stuPosition = 0;//用来记录学生遍历时的位置

    public StudentIteratorImpl(List<People> peopleList) 
        for (People people : peopleList) 
            if (people instanceof Student) 
                studentList.add(people);
            
        
    

    @Override
    public boolean hasNext() 
        return stuPosition < studentList.size();
    

    @Override
    public People next() 
        //从集合中获取指定位置的元素
        return studentList.get(stuPosition++);
    


1.3.6 Client :客户端类

package ex08.iterator;

import ex08.iterator.role.People;
import ex08.iterator.role.Student;
import ex08.iterator.role.Teacher;

/**
 * ClassName: Client
 * Description:客户端类:测试类
 *
 * @author Tianjiao
 * @date 2021/11/16 12:40
 */
public class Client 
    public static void main(String[] args) 
        // 创建具体聚合类
        PeopleAggregateImpl peopleAggregate = new PeopleAggregateImpl();
        // 添加数据
        addData(peopleAggregate);

        //遍历聚合对象
        //1.获取迭代器对象
        PeopleIterator peopleIterator = peopleAggregate.getPeopleIterator();
        while (peopleIterator.hasNext()) 
            //3,获取元素
            People people = peopleIterator.next();
            System.out.println(people.toString());
        

        System.out.println("---------------------------------");
        PeopleIterator stuIterator = peopleAggregate.getStuIterator();
        while (stuIterator.hasNext()) 
            Student student = (Student) stuIterator.next();
            System.out.println(student.toString());
        
    

    public static void addData(PeopleAggregateImpl peopleAggregate) 
        // 添加学生
        peopleAggregate.addPeople(new Student("学生1", "11923020124"));
        peopleAggregate.addPeople(new Student("学生2", "11923020123"));
        peopleAggregate.addPeople(new Student("学生3", "11923020123"));
        // 添加老师
        peopleAggregate.addPeople(new Teacher("教师1", "11923020001"));
        peopleAggregate.addPeople(new Teacher("教师2", "11923020002"));
        peopleAggregate.addPeople(new Teacher("教师3", "11923020003"));
    


1.3.7 运行结果:



以上是关于设计模式 行为型模式 实例 -- 迭代器模式实例:迭代数组的主要内容,如果未能解决你的问题,请参考以下文章

设计模式 行为型模式 -- 迭代器模式 JDK源码解析:ArrayList

设计模式行为型迭代器模式

设计模式行为型迭代器模式

设计模式行为型迭代器模式

模式的分类

设计模式——行为型模式(责任链,命令,解释器,迭代器,中介者,备忘录,观察者,状态,空对象,策略,模板,访问者)