知识卡片
迭代器模式的定义与四个角色
迭代器提供逐项访问聚合元素的方式,而不让使用方依赖内部表示。抽象/具体容器负责元素管理及创建遍历入口,抽象/具体迭代器负责访问契约和进度。
不同遍历策略可通过不同实现提供,但仍须定义顺序、终止和修改政策。图忠实采用本书自定义Iterator签名,boolean remove不是JDK接口;代码Aggregate.remove递归误调用、游标边界也不完整,未当可用实现。
结构图:
classDiagram
class Iterator {
<<interface>>
+next() Object
+hasNext() boolean
+remove() boolean
}
class ConcreteIterator {
+ConcreteIterator(Vector vector)
+next() Object
+hasNext() boolean
+remove() boolean
}
class Aggregate {
<<interface>>
+add(Object object) void
+remove(Object object) void
+iterator() Iterator
}
class ConcreteAggregate {
+add(Object object) void
+remove(Object object) void
+iterator() Iterator
}
Iterator <|.. ConcreteIterator
Aggregate <|.. ConcreteAggregate
ConcreteAggregate ..> ConcreteIterator : 创建游标
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 20.2 迭代器模式的定义 · 查看原书依据
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.(它提供一种方法访问
参考来源
- 位置:《设计模式之禅(第2版)》20.2 迭代器模式的定义(来源项目book-sources,源文件:extracted/设计模式之禅(第2版)/_epub-src/text/part0091.html)
- 结论依据:原文定义、示例及取舍支撑本卡;已知过强表述在正文限定。
- 原始内容:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.(它提供一种方法访问