设计模式(16)迭代器模式

一、介绍:

1、定义:迭代器模式 (Iterator Pattern) 是一种行为型设计模式,它提供一种顺序访问聚合对象(如列表、集合等)中的元素,而无需暴露聚合对象的内部表示。迭代器模式将遍历逻辑封装在一个迭代器对象中,使得我们可以使用统一的方式遍历不同类型的聚合对象,同时也可以简化客户端代码。

2、组成:

(1)抽象聚合 (Aggregate) :定义存储、添加、删除聚合元素以及创建迭代器对象的接口。

(2)具体聚合 (ConcreteAggregate) :实现抽象聚合类,返回一个具体迭代器的实例。

(3)抽象迭代器 (Iterator) :定义访问和遍历聚合元素的接口,通常包含 hasNext()、next() 等方法。

(4)具体迭代器 (Concretelterator) :实现抽象迭代器接口中所定义的方法,完成对聚合对象的遍历,记录遍历的当前位置。

二、demo:

1、

复制代码
package com.demo.ddq.dto;

public class Student {
    private String name;
    private String number;

    public Student() {
    }

    public Student(String name, String number) {
        this.name = name;
        this.number = number;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", number='" + number + '\'' +
                '}';
    }

   /**省略所有set、get方法*/
}

(1)抽象迭代器

复制代码
//抽象迭代器角色接口
public interface StudentIterator {
    //判断是否还有元素
    boolean hasNext();

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

(2)具体迭代器

复制代码
//具体迭代器角色类
public class StudentIteratorImpl implements StudentIterator{

    private List<Student> list;
    //用来记录遍历时的位置
    private int position = 0;

    public StudentIteratorImpl(List<Student> list) {
        this.list = list;
    }

    @Override
    public boolean hasNext() {
        return position < list.size();
    }

    @Override
    public Student next() {
        //从集合中或者去指定位置的元素
        Student currentStudent = list.get(position);
        position++;
        return currentStudent;
    }
}

(3)抽象聚合:

复制代码
//抽象聚合(容器)角色接口
public interface StudentAggregate {
    //添加学生
    void addStudent(Student stu);

    //删除学生
    void removeStudent(Student stu);

    //获取迭代器对象
    StudentIterator getStudentIterator();
}

(4)具体聚合:

复制代码
public class StudentAggregateImpl implements StudentAggregate{

    private List<Student> list = new ArrayList<>();

    @Override
    public void addStudent(Student stu) {
        list.add(stu);
    }

    @Override
    public void removeStudent(Student stu) {
        list.remove(stu);
    }

    //获取迭代器对象
    @Override
    public StudentIterator getStudentIterator() {
        return new StudentIteratorImpl(list);
    }
}

客户端:

复制代码
public class Test {
    public static void main(String[] args) {
        //创建聚合(容器)对象
        StudentAggregate aggregate = new StudentAggregateImpl();
        Student student1 = new Student("张三", "1001");
        Student student2 = new Student("李四", "1002");
        Student student3 = new Student("王五", "1003");
        Student student4 = new Student("钱七", "1004");
        //添加元素
        aggregate.addStudent(student1);
        aggregate.addStudent(student2);
        aggregate.addStudent(student3);
        aggregate.addStudent(student4);

        //删除元素
        aggregate.removeStudent(student3);

        //遍历聚合对象
        // 1.获取迭代器对象
        StudentIterator iterator = aggregate.getStudentIterator();
        // 2.遍历
        while (iterator.hasNext()) {
            // 3.获取元素
            Student student = iterator.next();
            System.out.println(student.toString());
        }
    }
}

输出:
Student{name='张三', number='1001'}
Student{name='李四', number='1002'}
Student{name='钱七', number='1004'}
相关推荐
wudl5566几秒前
Agent 设计模式全面分析
设计模式
__万波__1 分钟前
二十三种设计模式(四)--原型模式
java·设计模式·原型模式
4***g8946 分钟前
Java进阶-SpringCloud设计模式-工厂模式的设计与详解
java·spring cloud·设计模式
__万波__6 分钟前
二十三种设计模式(五)--建造者模式
java·设计模式·建造者模式
北郭guo6 分钟前
Java设计模式 【理论+代码实现】 让你从小白到大佬的蜕变
java·开发语言·设计模式
执笔论英雄7 小时前
Slime异步原理(单例设计模式)4
开发语言·python·设计模式
执笔论英雄9 小时前
Slime异步原理(单例设计模式)5
设计模式
未可知7779 小时前
软件设计师(上午题4)、面向对象、uml、设计模式
设计模式·职场和发展·uml
执笔论英雄9 小时前
【RL】Slime异步原理(单例设计模式)6
人工智能·设计模式
da_vinci_x9 小时前
PS 结构参考 + Firefly:零建模量产 2.5D 等轴游戏资产
人工智能·游戏·设计模式·prompt·aigc·技术美术·游戏美术