Iterator Design Pattern In Java
Join the DZone community and get the full member experience.Join For Free
Today, I will discuss a relatively simple and very commonly used behavioral design pattern called — Iterator Design Pattern. Iterator Pattern provides a simple way to iterate through the collection of objects.
Iterator Design Pattern
- The Iterator Design Pattern is one of twenty-three well known GoF design patterns provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Iterator Design Pattern provides an Iterator object to traverse a collection/container and access its member objects.
- Iterator Design Pattern is a relatively simple design pattern, which we use almost in every project.
- Few container requires an algorithm to access the member elements. Iterator Design Pattern decouples any such algorithm from the container.
- The algorithm can be written separately to use by the Iterator and hence can be use by any container which supports that kind of Iterator object.
- The Iterator Design Pattern provides flexible and reusable solution of traversing member objects of a container/collection. That make our collection object easier to implement, change, test and reuse.
- Iterator object also facilitate removing of member object while traversing the collection. So, it's one of the solution of ConcurrentModificationException.
Here, the aggregate is nothing but the container/collection which offers Iterator to traverse itself.
Suppose, we have a List of names as below:
Now to traverse the elements of it, we can use while loop like:
or by using for loop:
Here, in both cases, we need to check availability of container member element by using
hasNext() method before moving to next element by using
next() method. Otherwise we will get NoSuchElementException.
There is also a super easy way of doing same by using for-each loop:
For-each loops are tailor made and do not require checking collection boundaries while traversing.
Now, some examples to understand the implementation of the Iterator Pattern. I will use Java's Iterator interface in the example and will create only aggregate interface to get the instance of Iterator object.
Code for Container interface (deals with String containers since return type of the method is iterator of strings):
Now, suppose we have a NamesRepository class which implements Container interface as below:
Here, please note that I have created concrete implementation of Iterator interface as public static inner class of the repository class itself.
Now the Main program to execute and test the above code.
Actually, it is availability of tailor made for-each loop which makes the processing easiest. And to achieve that for-each uses implicit iterator object with the help of Iterable interface.
Now, one more example of Books Collection to understand the Iterator pattern.
Book Library Application Using Iterator Design Pattern
I think by now we are clear on the Iterator Pattern and its uses. By the example below, I will try to eliminate the doubts if any while implementing the Iterator Pattern.
Code for Book class:
Code for BookCollection interface (aggregate interface):
The method name can be like
getIterator() or simply
iterator(). Now, we have two different versions of book-collections.
- Variable number of books - BookLibrary.
- Fixed and limited number of books - BookStore.
Code for BookLibrary class:
Now the code for BookStore class:
Here, I am using explicit implementation of concrete Iterator class to demonstrate how we can write it. We can also use implicit Iterator object as we did in the BookLibrary class.
Now the code for BookIterator class which offers iterating over array of books:
Now, it's ready to execute and test the output. So, lets write a Main program:
And here's the output:
Now before I conclude this article, I like to discuss one more topic here.
This is included in Java 5 and is part of java.lang package. An object that implements this interface allows it to be the target of the for-each loop.
Let's see with the same Book Library example to understand how we can implement it.
All we need to do is: We have to make our BookStore and BookLibrary as sub-type of Iterable interface. Iterable interface will act as aggregator for us for Iterator Pattern.
And we have to implement only below method:
Please notice that its with generics just like Iterator interface.
Book Library Application Using Iterator Design Pattern (Iterable Interface)
There is no change in the Book class. So, will use the same. Also, there is no need to have BookCollection interface since Iterable will serve that purpose.
Now code for BookLibrary class:
Code for BookStore class:
Almost the same? Isn't it? And we are done! And you know what our previous code of Main will also work without any change. But, I like to show the simplicity of the code.
Now its time to write Main program to execute and test the output:
Well you have it! I hope you are clear with examples on Iterator Design Pattern.
Source Code can be found here: Iterator Design Pattern Source Code
Liked this article? Don't forget to press that like button. Happy coding!
Need more articles, please visit my profile: Brijesh Saxena
Opinions expressed by DZone contributors are their own.