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
- Iterator Interface: Defines methods for traversing a collection (e.g.,
next()
,hasNext()
). - Concrete Iterator: Implements the iterator interface for a specific collection.
- Collection Interface: Declares a method to return an iterator object.
- Concrete Collection: Implements the collection interface and returns a concrete iterator.