当前位置: 首页 > news >正文

设计模式(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;}@Overridepublic 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;}@Overridepublic boolean hasNext() {return position < list.size();}@Overridepublic 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<>();@Overridepublic void addStudent(Student stu) {list.add(stu);}@Overridepublic void removeStudent(Student stu) {list.remove(stu);}//获取迭代器对象@Overridepublic 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'}

http://www.lryc.cn/news/206277.html

相关文章:

  • Openssl数据安全传输平台011:秘钥协商服务端
  • 【23种设计模式】里氏替换原则
  • 嵌入式系统设计师考试笔记之操作系统基础复习笔记一
  • Unity开发之观察者模式(事件中心)
  • 16、window11+visual studio 2022+cuda+ffmpeg进行拉流和解码(RTX3050)
  • 【C++笔记】如何用检查TCP或UDP端口是否被占用
  • “华为杯”研究生数学建模竞赛2015年-【华为杯】D题:面向节能的单/多列车优化决策问题
  • 『第三章』雨燕栖息地:Swift 开发环境
  • elasticsearch-5.6.15集群部署,如何部署x-pack并添加安全认证
  • C++ list 模拟实现
  • Elasticsearch:使用 Open AI 和 Langchain 的 RAG - Retrieval Augmented Generation (三)
  • 主流电商平台价格如何高频监测
  • Spring关于注解的使用
  • 图像处理入门 1(Introduction to image processing)
  • 中国大模型开源创新与合作的新篇章 | 2023 CCF中国开源大会
  • vue项目切换页面白屏的解决方案
  • 5G vs 4G
  • 【JavaEE重点知识归纳】第11节:认识异常
  • 一键自助建站系统api版系统源码
  • 全国三维数字化创新设计大赛湖北赛区省赛成功举办
  • OpenCV #以图搜图:均值哈希算法(Average Hash Algorithm)原理与实验
  • 博通BCM575系列RDMA网卡驱动bnxt_re分析(一)
  • 集合总结-
  • 【知识串联】概率论中的值和量(随机变量/数字特征/参数估计)【考研向】【按概率论学习章节总结】
  • 上游服务不可用了,下游服务如何应对?
  • WebGL笔记:矩阵的变换之平移的实现
  • XTU-OJ 1187-Candy
  • 基于 nodejs+vue城市轨道交通线路查询系统mysql
  • 电商时代,VR全景如何解决实体店难做没流量?
  • 操作系统-浅谈CPU与内存