Iterator Pattern
The Iterator Pattern is a behavioral design pattern that provides a standard way to sequentially access the elements of a collection without exposing its underlying representation, letting client code traverse arrays, lists, trees, or…
Definition
The Iterator Pattern is a behavioral design pattern that provides a standard way to sequentially access the elements of a collection without exposing its underlying representation, letting client code traverse arrays, lists, trees, or other structures through a uniform interface.
Overview
The Iterator Pattern separates the traversal logic for a collection from the collection's own implementation, providing a dedicated iterator object that exposes a small, uniform interface — typically something like `hasNext()`/`next()` or a language-level iteration protocol — so client code can walk through elements one at a time without knowing or caring whether the underlying structure is an array, a linked list, a hash map, or a tree. This decoupling means the same client loop code can work across many different collection types, and it also allows multiple independent traversals of the same collection to happen concurrently, since each iterator can maintain its own position/state separately from the collection itself. The pattern is so fundamental to modern programming that most languages bake it directly into the language or standard library rather than requiring developers to hand-roll it: Java's `Iterator` interface and enhanced for-loop, Python's iterator protocol (`__iter__`/`__next__`) and generators, C++'s standard library iterators, and JavaScript's iterable protocol (`Symbol.iterator`) and `for...of` loops are all first-class language implementations of the Iterator Pattern. This ubiquity is itself evidence of the pattern's value: virtually every collection type in every modern standard library — arrays, sets, maps, trees, streams — exposes a consistent way to be iterated, letting generic algorithms (sorting, filtering, mapping) operate over any of them without special-casing each collection's internal structure. Beyond simple linear traversal, the Iterator Pattern generalizes well to more complex structures: tree iterators can implement depth-first or breadth-first traversal strategies behind the same `next()` interface, and lazy/generator-based iterators (like Python generators or JavaScript generator functions) let traversal logic produce elements on demand rather than materializing an entire collection in memory upfront, which is essential for iterating over infinite sequences or very large datasets streamed from disk or network. This lazy evaluation capability is one of the most practically important extensions of the base pattern in modern software.
Key Concepts
- Provides a uniform interface for traversing a collection's elements sequentially
- Hides the collection's internal representation from client traversal code
- Supports multiple independent, simultaneous traversals of the same collection
- Built directly into most modern languages (Java Iterator, Python __iter__, JS Symbol.iterator)
- Generalizes to tree traversal strategies (depth-first, breadth-first)
- Enables lazy, on-demand iteration via generators for large/infinite sequences
- Decouples generic algorithms from specific collection implementations
- Foundational to language-level for-each / for...of / enhanced-for constructs