Iterator PatternAccording 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 PatternLet's understand the example of iterator pattern pattern by the above UML diagram. UML for Iterator Pattern:Implementation of above UMLStep 1Create a Iterartor interface. Step 2Create a Container interface. Step 3Create a CollectionofNames class that will implement Container interface. File: CollectionofNames.java Step 4Create a IteratorPatternDemo class. File: IteratorPatternDemo.java OutputNext TopicMediator Design Pattern in Java |