Overview
The Iterator design pattern is a behavioral pattern that provides a way to access elements of an aggregate object sequentially without exposing underlying representation. Here an aggregate is understood as an object that holds collections of other objects. For example, inventory aggregate holds collection of products. This pattern defines a common interface for iterating over various types of collections, including lists, trees, and other data structures. It allows developers to create generic algorithms that can work with any collection type, providing a way to decouple the algorithm implementation from the collection implementation. This allows for flexibility. You can change the collection type that holds objects, and you do not need to change the algorithm that iterates over those objects. This is possible since the algorithm depends on Iterator abstraction instead of concrete details.
More on this pattern…
To read more on this and the other patterns, get “GoF Design Patterns Distilled” book from Amazon or Leanpub: