迭代器模式(Iterator Pattern)
迭代器模式是一种行为设计模式, 让你能在不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素。
迭代器模式结构
迭代器 (Iterator) 接口声明了遍历集合所需的操作: 获取下一个元素、 获取当前位置和重新开始迭代等。
具体迭代器 (Concrete Iterators) 实现遍历集合的一种特定算法。 迭代器对象必须跟踪自身遍历的进度。 这使得多个迭代器可以相互独立地遍历同一集合。
集合 (Collection) 接口声明一个或多个方法来获取与集合兼容的迭代器。 请注意, 返回方法的类型必须被声明为迭代器接口, 因此具体集合可以返回各种不同种类的迭代器。
具体集合 (Concrete Collections) 会在客户端请求迭代器时返回一个特定的具体迭代器类实体。 你可能会琢磨, 剩下的集合代码在什么地方呢? 不用担心, 它也会在同一个类中。 只是这些细节对于实际模式来说并不重要, 所以我们将其省略了而已。
客户端 (Client) 通过集合和迭代器的接口与两者进行交互。 这样一来客户端无需与具体类进行耦合, 允许同一客户端代码使用各种不同的集合和迭代器。
客户端通常不会自行创建迭代器, 而是会从集合中获取。 但在特定情况下, 客户端可以直接创建一个迭代器 (例如当客户端需要自定义特殊迭代器时)。
迭代器模式优缺点
✅单一职责原则。 通过将体积庞大的遍历算法代码抽取为独立的类, 你可对客户端代码和集合进行整理。 ✅开闭原则。 你可实现新型的集合和迭代器并将其传递给现有代码, 无需修改现有代码。 ✅你可以并行遍历同一集合, 因为每个迭代器对象都包含其自身的遍历状态。 ✅相似的, 你可以暂停遍历并在需要时继续。 ❎如果你的程序只与简单的集合进行交互, 应用该模式可能会矫枉过正。 ❎对于某些特殊集合, 使用迭代器可能比直接遍历的效率低。
与其他模式的关系
你可以使用迭代器模式来遍历组合模式树。
你可以同时使用工厂方法模式和迭代器来让子类集合返回不同类型的迭代器, 并使得迭代器与集合相匹配。
你可以同时使用备忘录模式和迭代器来获取当前迭代器的状态, 并且在需要的时候进行回滚。
可以同时使用访问者模式和迭代器来遍历复杂数据结构, 并对其中的元素执行所需操作, 即使这些元素所属的类完全不同。
概念示例
interface iterator<T> {
// Return the current element.
current(): T;
// Return the current element and move forward to next element.
next(): T;
// Checks if current position is valid.
valid(): boolean;
}
interface Aggregator {
getIterator(): iterator<string>;
}
class MyIterator implements iterator<string> {
private collection: GoodsCollection;
private position: number = 0;
private reverse: boolean = false;
constructor(collection: GoodsCollection, reverse: boolean = false) {
this.collection = collection;
this.reverse = reverse;
if (reverse) {
this.position = collection.getCount() - 1;
}
}
public current(): string {
return this.collection.getItems()[this.position];
}
public next(): string {
const item = this.collection.getItems()[this.position];
this.position += this.reverse ? -1 : 1;
return item;
}
public valid(): boolean {
if (this.reverse) {
return this.position >= 0;
}
return this.position < this.collection.getCount();
}
}
class GoodsCollection implements Aggregator {
private items: string[] = [];
public getItems(): string[] {
return this.items;
}
public getCount(): number {
return this.items.length;
}
public addItem(item: string): void {
this.items.push(item);
}
public getIterator(): iterator<string> {
return new MyIterator(this);
}
public getReverseIterator(): iterator<string> {
return new MyIterator(this, true);
}
}
const client2 = function () {
const collection = new GoodsCollection();
collection.addItem('item1');
collection.addItem('item2');
collection.addItem('item3');
collection.addItem('item4');
collection.addItem('item5');
collection.addItem('item6');
console.log('Straight traversal:');
const iterator = collection.getIterator();
while (iterator.valid()) {
console.log(iterator.next());
}
console.log('');
console.log('Reverse traversal:');
const reverseIterator = collection.getReverseIterator();
while (reverseIterator.valid()) {
console.log(reverseIterator.next());
}
}()output
Straight traversal:
item1
item2
item3
item4
item5
item6
Reverse traversal:
item6
item5
item4
item3
item2
item1