Iterator Design Pattern in Java With Examples

Iterator design pattern in one of the behavioral pattern. Iterator pattern is used to provide a standard way to traverse through a group of Objects. Iterator pattern is widely used in Java Collection Framework. Iterator interface provides methods for traversing through a collection.

Iterator Design Pattern

According to GoF, iterator design pattern intent is:

Provides a way to access the elements of an aggregate object without exposing its underlying represenation.

Iterator pattern is not only about traversing through a collection, we can provide different kind of iterators based on our requirements.

Iterator design pattern hides the actual implementation of traversal through the collection and client programs just use iterator methods.

Iterator Pattern Example

Let’s understand iterator pattern with a simple example. Suppose we have a list of Radio channels and the client program want to traverse through them one by one or based on the type of channel. For example some client programs are only interested in English channels and want to process only them, they don’t want to process other types of channels.

So we can provide a collection of channels to the client and let them write the logic to traverse through the channels and decide whether to process them. But this solution has lots of issues such as client has to come up with the logic for traversal. We can’t make sure that client logic is correct. Furthermore if the number of client grows then it will become very hard to maintain.

Here we can use Iterator pattern and provide iteration based on type of channel. We should make sure that client program can access the list of channels only through the iterator.

The first part of implementation is to define the contract for our collection and iterator interfaces.

ChannelTypeEnum is java enum that defines all the different types of channels.

Channel is a simple POJO class that has attributes frequency and channel type.

ChannelCollection interface defines the contract for our collection class implementation. Notice that there are methods to add and remove a channel but there is no method that returns the list of channels. ChannelCollection has a method that returns the iterator for traversal. ChannelIterator interface defines following methods;

Now our base interface and core classes are ready, let’s proceed with the implementation of collection class and iterator.

Notice the inner class implementation of iterator interface so that the implementation can’t be used by any other collection. Same approach is followed by collection classes also and all of them have inner class implementation of Iterator interface.

Let’s write a simple iterator pattern test program to use our collection and iterator to traverse through the collection of channels.

When I run above program, it produces following output;

Iterator Design Pattern Important Points

  • Iterator pattern is useful when you want to provide a standard way to iterate over a collection and hide the implementation logic from client program.
  • The logic for iteration is embedded in the collection itself and it helps client program to iterate over them easily.

Iterator Design Pattern in JDK

We all know that Collection framework Iterator is the best example of iterator pattern implementation but do you know that java.util.Scanner class also Implements Iterator interface. Read this post to learn about Java Scanner Class.

That’s all for iterator design pattern, I hope it’s helpful and easy to understand.

By admin

Leave a Reply

%d bloggers like this: