Category Archives: Iterator Pattern

Iterator Pattern – Design Pattern

The Iterator Pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

The iterator pattern allows traversal of elements of an aggregate without exposing the underlying implementation.

It also places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be.

one thing to notice here

Aggregate or Collection means just a group of objects, they might be stored in very different data structures like lists, arrays, hashmaps, vectors etc.

This pattern comes in the scenario when we have a different kind of collections, and we need to traverse over them in a different class, class separate from implementing collections data.

below is diagram explaining iterator pattern.

iterator-pattern

iterator-pattern