面向对象设计模式:行为型模式之迭代器模式
一、迭代器模式,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全套代码🔥课程设计、毕业设计、创新竞赛必备!详…...
HTML 语义化
目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案: 语义化标签: <header>:页头<nav>:导航<main>:主要内容<article>&#x…...
CTF show Web 红包题第六弹
提示 1.不是SQL注入 2.需要找关键源码 思路 进入页面发现是一个登录框,很难让人不联想到SQL注入,但提示都说了不是SQL注入,所以就不往这方面想了 先查看一下网页源码,发现一段JavaScript代码,有一个关键类ctfs…...
电脑插入多块移动硬盘后经常出现卡顿和蓝屏
当电脑在插入多块移动硬盘后频繁出现卡顿和蓝屏问题时,可能涉及硬件资源冲突、驱动兼容性、供电不足或系统设置等多方面原因。以下是逐步排查和解决方案: 1. 检查电源供电问题 问题原因:多块移动硬盘同时运行可能导致USB接口供电不足&#x…...
[Java恶补day16] 238.除自身以外数组的乘积
给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 请 不要使用除法,且在 O(n) 时间复杂度…...
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南
精益数据分析(97/126):邮件营销与用户参与度的关键指标优化指南 在数字化营销时代,邮件列表效度、用户参与度和网站性能等指标往往决定着创业公司的增长成败。今天,我们将深入解析邮件打开率、网站可用性、页面参与时…...
蓝桥杯3498 01串的熵
问题描述 对于一个长度为 23333333的 01 串, 如果其信息熵为 11625907.5798, 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次? #include<iostream> #include<cmath> using namespace std;int n 23333333;int main() {//枚举 0 出现的次数//因…...
GC1808高性能24位立体声音频ADC芯片解析
1. 芯片概述 GC1808是一款24位立体声音频模数转换器(ADC),支持8kHz~96kHz采样率,集成Δ-Σ调制器、数字抗混叠滤波器和高通滤波器,适用于高保真音频采集场景。 2. 核心特性 高精度:24位分辨率,…...
Android第十三次面试总结(四大 组件基础)
Activity生命周期和四大启动模式详解 一、Activity 生命周期 Activity 的生命周期由一系列回调方法组成,用于管理其创建、可见性、焦点和销毁过程。以下是核心方法及其调用时机: onCreate() 调用时机:Activity 首次创建时调用。…...
视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)
前言: 最近在做行为检测相关的模型,用的是时空图卷积网络(STGCN),但原有kinetic-400数据集数据质量较低,需要进行细粒度的标注,同时粗略搜了下已有开源工具基本都集中于图像分割这块,…...
【从零学习JVM|第三篇】类的生命周期(高频面试题)
前言: 在Java编程中,类的生命周期是指类从被加载到内存中开始,到被卸载出内存为止的整个过程。了解类的生命周期对于理解Java程序的运行机制以及性能优化非常重要。本文会深入探寻类的生命周期,让读者对此有深刻印象。 目录 …...
