Iterator Pattern


  • The Iterator pattern provides a way of accessing elements of a collection sequentially, without knowing how the collection is structured.
  • As an extension, the pattern allows for filtering elements in a variety of ways as they are generated.

Class diagram: (see Iterator Pattern code review)

Iterator Pattern class diagram of sample code download


Use the Iterator pattern when you are iterating over a collection and one of these conditions holds:

  • There are various ways of traversing it (several enumerators).
  • There are different collections for the same kind of traversing.
  • Different filters and orderings might apply.