According to GoF, Iterator Pattern is used "to access the elements of an aggregate object sequentially without exposing its underlying implementation".
The Iterator pattern is also known as Cursor.
In collection framework, we are now using Iterator that is preferred over Enumeration.
java.util.Iterator interface uses Iterator Design Pattern.
Advantage of Iterator Pattern
Usage of Iterator Pattern:
It is used:
Example of Iterator Pattern
Let's understand the example of iterator pattern pattern by the above UML diagram.
UML for Iterator Pattern:
Implementation of above UML
Create a Iterartor interface.
Create a Container interface.
Create a CollectionofNames class that will implement Container interface.
Create a IteratorPatternDemo class.