Iterator模式提供了一种顺序遍历集合的方式,而不暴露其内部结构。它是如此基础,以至于它被内置在大多数现代编程语言中(for-of循环、iterators、generators)。
Iterator模式的作用
ITERATOR → access elements of a collection SEQUENTIALLY without exposing its internals:
→ provides a standard way to traverse (next element, has more?)
→ the collection's internal structure (array, tree, linked list) is HIDDEN
→ decouples traversal logic from the collection
→ "iterate without caring how the collection is structured"
内置于现代语言中
( item collection) { }
* () { ( i = ; i < n; i++) i; }
( i ()) .(i);
