Design Pattern | Iterator Pattern
What Is the Iterator Pattern?
- Iterate means to repeat something. Iterator means one who repeats.
- The Iterator pattern is a method for sequentially accessing elements of an aggregate.
- In GoF design patterns, it is classified as a behavioral design pattern.
Suppose there is a certain kind of data collection. It includes features for manipulating and searching the data.
If search functionality is developed mixed together with other functionality, coupling between classes increases and code becomes hard to read.
The Iterator pattern makes the search or traversal functionality reusable.
Iterator Pattern Example Program
This example puts students into a class and displays each student’s name in order.
Class Diagram

The example defines an Iterator interface for scanning elements sequentially, an aggregate interface that can create iterators, a student class, a class that stores students, and an iterator implementation that traverses that class.
Advantages
Traversal logic is separated from the aggregate object. The client can scan elements without knowing the internal representation, and the collection can change internally without changing traversal code.