This article explains Iterator design pattern in java with UML class diagram. It then takes an example scenario in java and explains it with class diagram and code.
Introduction: Iterator Design Pattern is a behavioral design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a behavioral design pattern, the Iterator pattern deals with how objects of the designed system interact with each other.
What is Iterator Design Pattern: Iterator design pattern implements the logic for sequential access of a collection of data outside of the collection(aka an aggregate) itself. This decouples the iteration logic from the collection implementation.
Defining an abstract interface for an iterator makes it possible to have multiple ways of iterating the same collection. Lets say we have a collection representing a binary tree. We can then have multiple concrete implementations of the same iterator abstraction accessing the elements in the tree using different traversal logic like in-order,pre-order and post-order. The binary tree collection remains the same but using Iterator pattern we access it in multiple ways without altering and/or directly accessing the binary tree implementation itself.
Scenarios in which Iterator Design Pattern can be used: Given below are the scenarios in which an iterator pattern implementation is ideal for accessing elements of a collection-
- Collection’s internal implementation is to be hidden: When the collection to be iterated does not want to expose its internal structure/representation to all the accessors of its data. The knowledge of internal structure of the collection is then restricted to the iterator. The accessors directly do not access the collection.
- Multiple ways of traversing the collection are possible: As explained with the binary tree example above, multiple implementations of the iterator abstraction enable the accessors to traverse the collection in multiple ways.
- Iteration logic is decoupled from the collection’s internal structure: This scenario refers to polymorphic iteration. Lets say the collection being accessed itself has an abstract interface which multiple types of collection implementations possible.
For our binary tree example lets say we have two variations of binary trees – balanced and AVL. We define a common interface named BinaryTree and then define two concrete implementations of BinaryTree named – BalancedBinaryTree & AVLBinaryTree. The iterator abstraction would then use the interface BinaryTree to access the concrete binary tree collection implementation which could be any of the two types. This is an example of polymorphic iteration.
Class Diagram for Iterator Design Pattern
Explanation of Iterator Design Pattern’s Class Diagram
- Aggregate is the base interface for the family of collections which are to be iterated.It defines the method createIterator() in which all aggregates instantiate and return their own iterators.
- ConcreteAggregate is the concrete subclass of aggregate. It overrides createIterator().
- Iterator is the base interface for the family of iterators which traverse over the Aggregate family of collections.
- Iterator has four methods –
- firstItem() resets the iterator to point to the first item in the collection.
- nextItem() moves the iterator forward to next item in the collection.
- isOver() returns a boolean indicating whether the collection is fully traversed or not.
- getCurrentItem() returns the item to which the iterator is currently pointing.
- ConcreteIterator is a concrete instance of Iterator interface. It stores internally the concrete aggregate’s instance over which it has to iterate. This concrete aggregate instance is returned to it by the concrete aggregate itself.
Iterator Design Pattern Example Implementation in Java-Class Diagram
Code for the classes shown in Java Example’s Class Diagram
The Java class diagram above depicts Iterator Design pattern implemented for traversing a Sequence interface which has two concrete classes – ListSequence & LinkedListSequence. Lets quickly go through whats there in Java’s example’s class diagram & corresponding code –
Sequenceis the base interface for a collection-type of data structure.
LinkedListSequenceare concrete implementations of
Sequence. They hold their data in instance of
SequenceIteratoris the base interface for family of iterators capable of traversing Sequences.
LinkedListSequenceIteratorare concrete subclasses of
SequenceIteratorresponsible for iterating over
Clientcreates an instance of
ListSequence, initializes it with an
ArrayListwith few values.
Clientthen creates an instance of
ListIteratorfrom it. Similary,
Clientcreates instances of
LinkedListSequence, populates it with a
LinkedListinstance & creates an instance of
Clientuses a generic method called
printIteratorElements()which takes as input param all
SequenceIterators and prints their contents.
Summary: In the above tutorial we understood what is Iterator design pattern and the main scenarios in which it is applicable. We then looked at the UML class diagram for Iterator Design Pattern & its explanation, a Java Use Case implementing Iterator pattern with its class diagram and code for the classes shown in the class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Iterator design pattern.