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

设计模式 - 行为型模式考点篇:迭代器模式(概述 | 案例实现 | 优缺点 | 使用场景)

目录

一、行为型模式

一句话概括行为型模式

1.1、迭代器模式

1.1.1、概述 

1.1.2、案例实现

1.1.3、优缺点

1.1.4、使用场景


一、行为型模式


一句话概括行为型模式

行为型模式:类或对象间如何交互、如何划分职责,从而更好的完成任务.

1.1、迭代器模式

1.1.1、概述 

提供一个聚合对象,内部通过迭代器来访问聚合对象中的一系列数据,而不暴露聚合对象的内部实现.

例如,现在有一个班级的学生(包装在一个 List 容器中的聚合元素),我需要按照学号拿到每一个学生,此时就需要把遍历这个班级的学生(List 容器)交给迭代器完成.

迭代器模式主要包含以下角色:

  • 抽象迭代器:定义访问和遍历聚合元素的接口,通常包含 hasNext()、next() 等方法.
  • 具体迭代器:实现抽象迭代器接口中定义的方法,完成聚合对象的遍历,记录遍历的当前位置.
  • 抽象聚合:定义存储、添加、删除聚合元素以及创建迭代器对象接口.
  • 具体聚合:实现抽象聚合类,返回一个具体的迭代器实例.

1.1.2、案例实现

实现上述学生案例.

/*** 学生类*/
public class Student {private String name;private int id;public Student() {}public Student(String name, int id) {this.name = name;this.id = id;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", id=" + id +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}
}
/*** 抽象迭代器: 学生迭代器接口*/
public interface StudentIterator {boolean hasNext();Student next();}
/*** 具体迭代器: 学生迭代器*/
public class StudentIteratorImpl implements StudentIterator{private List<Student> list;private int position;public StudentIteratorImpl(List<Student> list) {this.list = list;}@Overridepublic boolean hasNext() {return position < list.size();}@Overridepublic Student next() {Student current = list.get(position);position++;return current;}}
/*** 抽象聚合: 学生聚合接口*/
public interface StudentAggregation {void addStudent(Student student);void removeStudent(Student student);StudentIterator getStudentIterator();}
/*** 具体聚合: 学生聚合*/
public class StudentAggregationImpl implements StudentAggregation{private List<Student> list = new ArrayList<>();@Overridepublic void addStudent(Student student) {list.add(student);}@Overridepublic void removeStudent(Student student) {list.remove(student);}@Overridepublic StudentIterator getStudentIterator() {return new StudentIteratorImpl(list);}}
public class Client {public static void main(String[] args) {StudentAggregationImpl aggregation = new StudentAggregationImpl();aggregation.addStudent(new Student("曹操", 1));aggregation.addStudent(new Student("诸葛亮", 2));aggregation.addStudent(new Student("赵云", 3));StudentIterator studentIterator = aggregation.getStudentIterator();while(studentIterator.hasNext()) {Student student = studentIterator.next();System.out.println(student);}}}

执行结果如下:

1.1.3、优缺点

优点:

定义多种遍历方式:支持不同方式遍历一个聚合对象,可以在同一个聚合对象上顶一个多种遍历方式.

满足开闭原则:引入抽象层,增加新的聚合类和迭代器,都无需修改原有代码.

缺点:

增加了类的个数,一定程度上增加了系统复杂度.

1.1.4、使用场景

  1. 当需要为聚合对象提供多种遍历方式.
  2. 当需要为遍历不同的聚合结构提供一个统一的接口时.
  3. 当访问的聚合对象的内容无需要暴露其内部实现细节.

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

相关文章:

  • Spark任务优化分析
  • 最新数据库流行度最新排名(每月更新)
  • Python:如何在一个月内学会爬取大规模数据
  • K8S云计算系列-(4)
  • 【Mybatis源码】IDEA中Mybatis源码环境搭建
  • VUE如何使得大屏自适应的几种方法?
  • API接口安全运营研究(内附官方开发平台api接口接入方式)
  • 信钰证券:股票交易费用计算方法?
  • 通过js获取用户网络ip地址
  • 微信小程序wxml使用过滤器
  • 内网渗透面试问题
  • Go语言函数进阶:值传递、引用传递、函数式编程
  • 数据结构 堆——详细动画图解,形象理解
  • 使用pymodbus进行modbus-TCP通信
  • 2. redis常见数据类型
  • 多测师肖sir_高级金牌讲师_python之结构语句005
  • 用3-8译码器实现全减器
  • 招投标系统简介 企业电子招投标采购系统源码之电子招投标系统 —降低企业采购成本
  • Linux Centos7 下使用yum安装的nginx平滑升级
  • C/S架构学习之多线程实现TCP并发服务器
  • iPhone手机记笔记工具选择用哪个
  • MyBatis动态SQL(if、choose、when和otherwise)标签
  • idea将jar包deploy到本地仓库
  • 麻省理工学院与Meta AI共同开发StreamingLLM框架,实现语言模型无限处理长度
  • 记录 K8S 挂了的解决经过
  • Flink---11、状态管理(按键分区状态(值状态、列表状态、Map状态、归约状态、聚合状态)算子状态(列表状态、广播状态))
  • Vue3中使用tinymce全功能演示,包括开源功能
  • There was an error committing your changes: File could not be edited
  • 10月9日,每日信息差
  • 【软考设计师】S01 数据结构 E01 线性结构 P01 线性表