Behavioral pattern · Visual logic
Iterator Pattern: Traverse Without Exposing Storage
Iterator provides a standard way to traverse a collection while hiding whether the underlying structure is an array, tree, stream, or remote page.
Consumers depend on collection internals and must be rewritten whenever storage or traversal order changes.
Expose traversal state and next-item behavior through an iterator contract rather than revealing collection representation.
- COLLECTION creates an iterator for the requested traversal policy.
- CONSUMER asks whether another item exists and requests it when ready.
- ITERATOR advances internal position without exposing storage details.
Consumers process items consistently while collections remain free to change representation or traversal strategy.
Run it in the real world
A feed renderer consumes the same iterator interface for an in-memory list, paginated API results, or a lazily streamed archive.
Go deeper
This is an original explanation and example inspired by Head First Design Patterns. The book covers the complete pattern, design principles, and implementation detail.
Retail links are not affiliate links yet. If that changes, this page will clearly disclose it.