What is the Iterator Pattern?

Updated Apr 28, 2026

Short answer

A behavioral pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

Deep explanation

It extracts the traversal behavior into a separate iterator object. The iterator encapsulates the traversal details (current position, end of sequence), allowing clients to iterate over different collections using the same standard interface.

Real-world example

Iterating over a complex binary tree structure in a flat foreach loop without exposing the tree nodes to the consumer.

Common mistakes

  • Implementing custom iterators when modern languages provide built-in generators (e.g., `yield` in Python/C#/JS).

Follow-up questions

  • How do modern languages natively implement this?

More Design Patterns interview questions

View all →