面向对象设计模式:行为型模式之迭代器模式
一、迭代器模式,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全套代码🔥课程设计、毕业设计、创新竞赛必备!详…...
Java如何权衡是使用无序的数组还是有序的数组
在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)
概述 在 Swift 开发语言中,各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过,在涉及到多个子类派生于基类进行多态模拟的场景下,…...
蓝桥杯 2024 15届国赛 A组 儿童节快乐
P10576 [蓝桥杯 2024 国 A] 儿童节快乐 题目描述 五彩斑斓的气球在蓝天下悠然飘荡,轻快的音乐在耳边持续回荡,小朋友们手牵着手一同畅快欢笑。在这样一片安乐祥和的氛围下,六一来了。 今天是六一儿童节,小蓝老师为了让大家在节…...

cf2117E
原题链接:https://codeforces.com/contest/2117/problem/E 题目背景: 给定两个数组a,b,可以执行多次以下操作:选择 i (1 < i < n - 1),并设置 或,也可以在执行上述操作前执行一次删除任意 和 。求…...
Java多线程实现之Callable接口深度解析
Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

DIY|Mac 搭建 ESP-IDF 开发环境及编译小智 AI
前一阵子在百度 AI 开发者大会上,看到基于小智 AI DIY 玩具的演示,感觉有点意思,想着自己也来试试。 如果只是想烧录现成的固件,乐鑫官方除了提供了 Windows 版本的 Flash 下载工具 之外,还提供了基于网页版的 ESP LA…...

Module Federation 和 Native Federation 的比较
前言 Module Federation 是 Webpack 5 引入的微前端架构方案,允许不同独立构建的应用在运行时动态共享模块。 Native Federation 是 Angular 官方基于 Module Federation 理念实现的专为 Angular 优化的微前端方案。 概念解析 Module Federation (模块联邦) Modul…...

Ascend NPU上适配Step-Audio模型
1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统,支持多语言对话(如 中文,英文,日语),语音情感(如 开心,悲伤)&#x…...
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数
高效线程安全的单例模式:Python 中的懒加载与自定义初始化参数 在软件开发中,单例模式(Singleton Pattern)是一种常见的设计模式,确保一个类仅有一个实例,并提供一个全局访问点。在多线程环境下,实现单例模式时需要注意线程安全问题,以防止多个线程同时创建实例,导致…...
在树莓派上添加音频输入设备的几种方法
在树莓派上添加音频输入设备可以通过以下步骤完成,具体方法取决于设备类型(如USB麦克风、3.5mm接口麦克风或HDMI音频输入)。以下是详细指南: 1. 连接音频输入设备 USB麦克风/声卡:直接插入树莓派的USB接口。3.5mm麦克…...