3 Iterator (Behavioral) Design Pattern

The Iterator Design Pattern is a behavioral design pattern that provides a way to access elements of a collection sequentially without exposing the underlying representation (e.g., array, list, tree).

Intent

  • Decouple the collection from the algorithm used to traverse it.
  • Provide a unified interface for traversing different types of collections.

Key Components

  1. Iterator Interface: Defines methods for traversing a collection (e.g., next(), hasNext()).
  2. Concrete Iterator: Implements the iterator interface for a specific collection.
  3. Collection Interface: Declares a method to return an iterator object.
  4. Concrete Collection: Implements the collection interface and returns a concrete iterator.