面向对象设计模式:行为型模式之迭代器模式
一、迭代器模式,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

- Active Iterator (External Iterator): The client 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");}}

相关文章:
面向对象设计模式:行为型模式之迭代器模式
一、迭代器模式,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. 提供一种按顺序访问聚合对象的元素而不公开其基…...
如何快速在企业网盘中找到想要的文件
现在越来越多的企业采用企业网盘来存储文档和资料,而且现在市面上的企业网盘各种各样。在使用企业网盘过程中,很多用户会问到企业网盘中如何快速搜索文件的问题。但是无论是“标签”功能还是普通的“关键词搜索”功能,都是单层级的࿰…...
香橙派5使用NPU加速yolov5的实时视频推理(二)
三、将best.onnx转为RKNN格式 这一步就需要我们进入到Ubuntu20.04系统中了,我的Ubuntu系统中已经下载好了anaconda,使用anaconda的好处就是可以方便的安装一些库,而且还可以利用conda来配置虚拟环境,做到环境与环境之间相互独立。…...
算法练习-二分查找(一)
算法练习-二分查找 1 代码实现 1.1 非递归实现 public int bsearch(int[] a, int n, int value) {int low 0;int high n - 1;while (low < high) {int mid (low high) / 2;if (a[mid] value) {return mid;} else if (a[mid] < value) {low mid 1} else {high …...
通用业务平台设计(五):预警平台建设
前言 在上家公司,随着业务的不断拓展(从支持单个国家单个主体演变成支持多个国家多个主体),对预警的诉求越来越紧迫;如何保障业务的稳定性那?预警可以帮我们提前甄别风险,从而让我们可以在风险来临前将其消灭ÿ…...
Windows openssl-1.1.1d vs2017编译
工具: 1. perl(https://strawberryperl.com/) 2. nasm(https://nasm.us/) 3. openssl源码(https://www.openssl.org/) 可以自己去下载 或者我的网盘提供下载: 链接:…...
【深蓝学院】手写VIO第2章--IMU传感器--笔记
0. 内容 1. 旋转运动学 角速度的推导: 左ω∧\omega^{\wedge}ω∧,而ω\omegaω是在z轴方向运动,θ′[0,0,1]T\theta^{\prime}[0,0,1]^Tθ′[0,0,1]T 两边取模后得到结论: 线速度大小半径 * 角速度大小 其中,对旋转矩…...
网络基础(二)之HTTP与HTTPS
应用层 再谈 "协议" 协议是一种 "约定". socket api的接口, 在读写数据时, 都是按 "字符串" 的方式来发送接收的. 如果我们要传输一些"结构化的数据" 怎么办呢? 为什么要转换呢? 如果我们将struct message里面的信息…...
Python每日一练(20230306)
目录 1. 翻转二叉树 ★★ 2. 最长公共前缀 ★★ 3. 2的幂 ★ 1. 翻转二叉树 翻转一棵二叉树。 示例 1: 输入: 4/ \2 7/ \ / \ 1 3 6 9 输出: 4/ \7 2/ \ / \ 9 6 3 1示例 2: 输入: 1…...
C/C++每日一练(20230305)
目录 1. 整数分解 ☆ 2. 二叉树的最小深度 ★★ 3. 找x ★★ 1. 整数分解 输入一个正整数,将其按7进制位分解为各乘式的累加和。 示例 1: 输入:49 输出:497^2示例 2: 输入:720 输出:720…...
SAS字典的应用
数据字典中常用信息检索DICTIONARY.COLUMNS、DICTIONARY.TABLES以及DICTIONARY.MEMBERS等字典表的内容。在编程实践中,如何以SAS字典表来提高效率。 1、DICTIONARY.COLUMNS 对于当前SAS任务的全部数据集,表格DICTIONARY.COLUMNS包含了诸如变量的名称、类…...
Mysql中的函数和触发器
函数函数是什么?多用于查询语句,实现了某种功能;用途与存储过程不同,但语法是类似的;函数语法create function 函数名([参数列表]) returns 数据类型 begin DECLARE 变量; sql 语句; return 值; end; 设置函…...
分布式架构之(Zookeeper原理)
Zookeeper是一个典型的分布式数据一致性的结局方案,分布式应用程序可以基于它实现注入数据发布、订阅、负载均衡、命名服务、分布式协调/通知、集群管理、Master选举、分布式锁和分布式队列等功能, Zookeeper可以保证如下分布式一致性特性: 顺…...
Java框架学习 | MyBatis
问题导向学习MyBatis 为什么要有MyBatis框架? 避免Java开发者直接使用 JDBC重复做数据库操作,同时更便捷地实现想要的数据库相关功能,让Java专注于开发业务。 MyBatis框架如何实现该目的? MyBatis是半自动化持久层ORM框架&#x…...
Cookie+Session详解
文章目录批量删除会话技术简介CookieCookie 查看Cookie 的删除Cookie 使用页面获取 cookie 信息cookie 特点Sessionsession 的使用Session 登录权限验证过滤器简介过滤器的使用WebFilter 注解过滤放行登录权限验证批量删除 servlet 类 dao 层 会话技术 简介 在计算机领域…...
CAPL脚本要注意区分elcount和strlen求数组长度的区别,不然要吃大亏
🍅 我是蚂蚁小兵,专注于车载诊断领域,尤其擅长于对CANoe工具的使用🍅 寻找组织 ,答疑解惑,摸鱼聊天,博客源码,点击加入👉【相亲相爱一家人】🍅 玩转CANoe&…...
CSS常用选择器
目录 1.CSS是什么 2.CSS的三种写法 2.1内部样式 2.2内联样式 2.3外部样式 3.CSS选择器 3.1标签选择器 3.2类选择器(更好的选择) 3.3ID选择器 3.4后代选择器 3.5子选择器 3.6并集选择器 3.7伪类选择器(复合选择器的特殊用法) 1.CSS是什么 CSS全称Cascding Style Sh…...
Registry与DGC的攻击利用
0x01 2022-02-03写的一篇文章。 0x02 Registry Registry指的是RMI的注册表,攻击的目标是注册表所在的机器,一般注册表和RMI Server在同一个机器上,特殊情况下也会在不同机器上。 在我们通过LocateRegistry#getRegistry获取到目标开启的注…...
赛道持续降温!又一家自动驾驶公司裁员,市值曾超50亿美元
从去年下半年开始,自动驾驶赛道的裁员、倒闭风潮盛行。 本周,美股卡车自动驾驶上市公司Embark Trucks(EMBK)宣布将裁员70%,同时大幅缩减业务。“痛苦可能还没有结束,”公司首席执行官Alex Rodrigues在给员…...
路径规划 | 图解动态A*(D*)算法(附ROS C++/Python/Matlab仿真)
目录0 专栏介绍1 什么是D*算法?2 D*算法核心概念一览3 D*算法流程图4 步步图解:算法实例5 算法仿真与实现5.1 ROS C实现5.2 Python实现0 专栏介绍 🔥附C/Python/Matlab全套代码🔥课程设计、毕业设计、创新竞赛必备!详…...
近期 GitHub 上爆火的 34 个极具潜力的开源项目
Coasts GitHub 链接:https://github.com/coast-guard/coasts 一款为 Git 工作区打造的本地主机服务隔离与编排工具,由前 Y Combinator 创始人开发。将自主智能体的主机全访问权限这一安全风险规避,智能体可在容器化主机内创建环境、运行服务…...
大海捞针:从海量真实世界5G-A基站数据中追踪无人机
大家读完觉得有帮助记得关注和 点赞!!! 摘要 无人机在日常生活中的潜在应用使得对其监控变得至关重要。然而,现有的无人机监控系统通常依赖于摄像头、激光雷达或雷达,这些系统的感知范围有限或部署成本高昂࿰…...
OpenClaw故障模拟:Qwen3-14b_int4_awq异常输入处理与恢复机制
OpenClaw故障模拟:Qwen3-14b_int4_awq异常输入处理与恢复机制 1. 为什么需要主动制造故障 去年冬天的一个深夜,我的OpenClaw自动化流程突然中断了。当时它正在帮我整理一批技术文档,却在处理某个特殊字符时直接"卡死"。这次经历让…...
热门AI命理工具盘点:星座、运势、排盘工具一次看
很多朋友对传统命理文化感兴趣,却怕找不对专业靠谱的工具,今天我们就整理了10款不同方向的AI命理相关工具,涵盖星座、面相、运势测算、专业排盘等不同需求,大家可以按需选择。 一、专业命理首选:天府 Agent 链接&#…...
气电版通用自动分选机:圆柱电芯测试分选的精准之选
在新能源产业蓬勃发展的当下,圆柱电芯作为重要的储能元件,其生产过程中的质量把控至关重要。内阻和电压作为衡量电芯性能的关键指标,直接关系到电芯的使用寿命、充放电效率以及安全性。气电版通用自动分选机凭借其卓越的性能和精准的分选能力…...
Linux系统学习:38张思维导图构建核心知识体系
1. Linux学习思维导图概述作为一名从嵌入式开发转战云计算的老兵,我深知系统化学习Linux的重要性。最近整理硬盘时翻出一套珍藏多年的学习资料——38张涵盖Linux核心知识体系的思维导图,这些图纸曾帮助我顺利通过RHCE认证,也指导过团队新人快…...
OpenClaw未来展望:Qwen3-14B与本地自动化的5个进化方向
OpenClaw未来展望:Qwen3-14B与本地自动化的5个进化方向 1. 从工具到伙伴:OpenClaw的现状与定位 去年冬天,当我第一次在本地MacBook上部署OpenClaw时,它还是个需要手动配置JSON文件才能调用本地模型的"半成品"。如今看…...
Cursor Pro功能突破解决方案:基于cursor-free-vip的完整技术指南
Cursor Pro功能突破解决方案:基于cursor-free-vip的完整技术指南 【免费下载链接】cursor-free-vip [Support 0.45](Multi Language 多语言)自动注册 Cursor Ai ,自动重置机器ID , 免费升级使用Pro 功能: Youve reache…...
探索偏心轮飞剪的 Codesys 程序奥秘:基于偏心轮加滑块机构
偏心轮 飞剪 电子凸轮 codesys程序源码 适用于偏心轮加滑块机构 在自动化控制领域,偏心轮飞剪系统凭借其独特的运动特性和高效的切割能力,在众多生产场景中发挥着关键作用。今天咱们就深入探讨基于偏心轮加滑块机构的偏心轮飞剪的 Codesys 程序源码&…...
