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.

Problem

Consumers depend on collection internals and must be rewritten whenever storage or traversal order changes.

Rule

Expose traversal state and next-item behavior through an iterator contract rather than revealing collection representation.

Run
  1. COLLECTION creates an iterator for the requested traversal policy.
  2. CONSUMER asks whether another item exists and requests it when ready.
  3. ITERATOR advances internal position without exposing storage details.
Output

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.