Iterator Pattern 18Dec, 2012

In this article I’ll try to explain about one of the most common patterns, named Iterator Pattern. I am sure that every one of us must have used this pattern in our everyday programming tasks, either knowingly or unknowingly. Once we are through this article, we’ll realize how many times in a day we use this pattern in doing our normal tasks.

Let’s start by understanding the intent of the Iterator pattern. Consider the example of a TV. We have different channels on different frequencies. So, we have a collection of Channels that we want to traverse. So, this collection is our Aggregate object that we would like to iterate through.So, we need a way to iterate through each channel, which is provided by the buttons on the TV console or the remote control. So, these two places act as iterator objects to iterate through the channels.

So, we can say that Iterator pattern provides a way to iterate through the items in a collection without knowing the internal structure of the collection.

It is very important to understand that it does not matter what the internal structure of the Collection is. It can be an Array , A List , a hashtable or any other kind of collection. The iterator provides the way to easily traverse through each item in that collection.

Now that you have an overview of the Pattern, lets have a look at all the participants in a more formal way.

The Aggregate: This is an interface which defines a way to create the Iterator.

The ConcreteAggregate: This is the Concrete Collection containing the items and a method to create the concrete iterator.

The Iterator: This is the interface that defines a way to access and traverse the elements.

The ConcreteIterator: This is the concrete iterator

Lets understand each one individually as these can be confusing in starting for some but will make sense when we see an example.

So, the Aggregate is a generic collection say TV. Now, it defines a method to Create the Iterator object which will be implemented by the Concrete Aggregate. Something like GetChannelIterator.

interface TV
{
Public ChannelIterator GetChannelIterator ();
}

The ConcreteAggregate is a more Concrete Class say OLD TV, Color TV or LCD TV. It will have the Channel Collection and a way to Create the Concrete Iterator that knows the way to traverse its channel.
The Old Style TV might have a Round Dial that revolves 360 degrees and traverses both tuned and untuned channels but New TVs like LCD TV will have a different channel iterator with next and previous buttons.

Class OldTV implements TV
{
private List< String > ChannelCollection;
private ChannelIterator iterator;

public OldTV ()
{
iterator = new OldStyleDialIterator(); //The LCD TV will return an instance of different Concrete Iterator say NewStyleNextPrevIterator()
}

Public ChannelIterator GetChannelIterator ()
{
return iterator;
}
}

The iterator is an interface that defines way of traversals. One can argue on what are the the least number of methods that should be defined in an iterator but generally, it can have the methods to Move to next and Previous items, find whether we have reached the end of the collection and find the current item in the collection.

interface ChannelIterator
{
void Next();
bool HasNext();
object CurrentItem();
}

And the Concrete Iterator is the implementation of these methods.

Class NewStyleNextPrevIterator implements ChannelIterator
{
private List<String> channels;
private int currentPos = 0;

public boolean hasNext()
{
if(currentPos + 1 < channels.size())
{
return true;
}
return false;
}
public void Next()
{
currentPos++;
}
public String currentItem()
{
return channels.get(currentPos);
}
}

So, I hope I have been able to make sense of all the technical jargon and explain the concept in simple words.

Posted by: Amritpal Singh / In: Design Patterns and Tagged ,
Cam