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

面向对象设计模式:行为型模式之迭代器模式

一、迭代器模式,Iterator Pattern

aka:Cursor Pattern

1.1 Intent 意图

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
提供一种按顺序访问聚合对象的元素而不公开其基础表示形式的方法。

1.2 Motivation 动机

  • An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. 聚合对象(如列表)应该提供一种访问其元素而不公开其内部结构的方法.
  • The key idea in iterator pattern is to take the responsibility for access and traversal out of the list object and put it into an iterator object. 迭代器模式中的关键思想是将访问和遍历的责任从列表对象中取出,并将其放入迭代器对象中.
  • Separating the traversal mechanism from the List object lets us define iterators for different traversal policies without enumerating them in the List interface. 将遍历机制与 List 对象分开,使我们可以为不同的遍历策略定义迭代器,而无需在List 接口中枚举它们.

1.3 Applicability 适用性

  • To access an aggregate object’s contents without exposing its internal representation. 要访问聚合对象的内容而不公开其内部表示形式.
  • To support multiple traversals of aggregate objects. 以支持聚合对象的多次遍历.
  • To provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration). 提供遍历不同聚合结构的统一接口(即支持多态迭代).

1.4 迭代器模式类图

在这里插入图片描述

  • ConcreteIterator 定义在哪?

    • Public Iterator: Concrete iterator is defined as a class independent from aggregate. 定义为独立于聚合类的类-外部迭代器类
    • Private Iterator: Concrete iterator is defined as a inner class in the aggregate. 定义为在聚合类内部的类-内部私有迭代器类
  • 谁控制迭代?

    • Active Iterator (External Iterator): The client controls the iteration
      在这里插入图片描述
    • Passive Iterator (Internal Iterator): The iterator controls the iteration
      在这里插入图片描述
  • Who defines the traversal algorithm

    • The aggregate might define the traversal algorithm and use the iterator to store just the state of the iteration (cursor), it points to the current position in the aggregate
    • The iterator is responsible for the traversal algorithm, then it’s easy to use different iteration algorithms on the same aggregate, and it can also be easier to reuse the same algorithm on different aggregates.

1.5 迭代器鲁棒性

  • How robust is the iterator? 迭代器鲁棒性如何?

    • It can be dangerous to modify an aggregate while you’re traversing it.
    • Copied Iterator
      • A simple solution is to copy the aggregate and traverse the copy, but that’s too expensive to do in general.
    • Robust iterator
      • Ensures that insertions and removals won’t affect traversal, and it does it without copying the aggregate. On insertion or removal, the aggregate either adjusts the internal state of iterators it has produced, or it maintains information internally to ensure proper traversal.
  • Static Iterator and Dynamic Iterator:静态迭代器和动态迭代器

    • Static Iterator: A copied iterator which contains a snapshot of the aggregate when iterator is created. New changes are invisible to the traversal approach.
    • Dynamic Iterator: Dynamic Iterator is opposited to the static one. Any changes to the aggregate are allowed and available when traversing the aggregate. Completely Dynamic Iterator is not easy to be implemented.
  • Fail-Fast in Java:Java 中的快速失败

    • Fail-fast is a property of a system or module with respect to its response to failures. 快速故障是系统或模块关于其对故障响应的一个特性.
    • A fail-fast system is designed to immediately report at its interface any failure or condition that is likely to lead to failure.
    • Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly-flawed process.
    • Fail-fast Iterator throws an exception when the aggregate is changed during iteration.
    • Fail-fast 与 并发修改异常 ConcurrentModificationException
import java.util.ArrayList;
import java.util.Iterator;public class FailFastTest {public static void main(String[] args) {ArrayList<String> arrayList = new ArrayList<String>();arrayList.add("haha");arrayList.add("hehe");arrayList.add("xixi");Iterator<String> iterator = arrayList.iterator();//create a new thread to traverse using iterator.	new Thread(){public void run() {while(iterator.hasNext()){System.out.println(iterator.next());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}.start();//main thread try to change the aggregate: //Fail-fast Iterator throws an exceptiontry {Thread.sleep(1500);} catch (InterruptedException e) {e.printStackTrace();}arrayList.add("wawa");}}

在这里插入图片描述

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

相关文章:

  • 如何快速在企业网盘中找到想要的文件
  • 香橙派5使用NPU加速yolov5的实时视频推理(二)
  • 算法练习-二分查找(一)
  • 通用业务平台设计(五):预警平台建设
  • Windows openssl-1.1.1d vs2017编译
  • 【深蓝学院】手写VIO第2章--IMU传感器--笔记
  • 网络基础(二)之HTTP与HTTPS
  • Python每日一练(20230306)
  • C/C++每日一练(20230305)
  • SAS字典的应用
  • Mysql中的函数和触发器
  • 分布式架构之(Zookeeper原理)
  • Java框架学习 | MyBatis
  • Cookie+Session详解
  • CAPL脚本要注意区分elcount和strlen求数组长度的区别,不然要吃大亏
  • CSS常用选择器
  • Registry与DGC的攻击利用
  • 赛道持续降温!又一家自动驾驶公司裁员,市值曾超50亿美元
  • 路径规划 | 图解动态A*(D*)算法(附ROS C++/Python/Matlab仿真)
  • GraphCut、最大流最小割定理
  • Word文档的密码忘记了怎么办?
  • Java分布式事务(二)
  • 游戏项目中的程序化生成(PCG):算法之外的问题与问题
  • 【C++】位图+哈希切割+布隆过滤器
  • python实现网络游戏NPC任务脚本引擎(带限时任务功能)
  • C语言的原子操作(待完善)
  • JavaScript Boolean 布尔对象
  • 删除链表元素相关的练习
  • 3DEXPERIENCE Works 成为了中科赛凌实现科技克隆环境的催化剂
  • 少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(选择题)2022年12月