Click here to Skip to main content
Click here to Skip to main content

Iterator Design Pattern

, 4 May 2011
Rate this:
Please Sign up or sign in to vote.
Iterator Design Pattern clearly explained with example

You can see this and other great articles on design patterns here.

The Iterator Design Pattern allows you abstract out the details of traversing collections. For example, you may different types of collections in your applications, such as an array, a linked list, or a generic dictionary. For whichever the types of collections you have, you will need to traverse, or iterate through the items in the collections.  The actual implementation on how to traverse different types of collections will be different, yet the client code(calling code) should not be concerned about the details of the implementations. The iterator pattern helps you to hide such details and provide a generic interface for the client to traverse different types of collections.

Let's look at the UML of the iterator pattern first, then we will look at some code to see the benefit that it brings. Below is the UML of the Iterator Design Pattern:

  • The IIterator interface defines all the methods needed to traverse the collection.                                 
  • The ConcreteIterator class implements the IIterator interface and has the actual implementations on how to traverse the collection.
  • The IAggregate interface defines the methods for the client. The methods that it defines allows the client code not to be bothered with the details on how the collection is traversed. It has the GetAll method that the client can call.
  • The ConcreteAggregate class implements the IAggregate interface and is the class that creates the ConcreteIterator.

With the iterator pattern in place, the client code(calling code) can just be:

IAggregate a = new ConcreteAggregate();

List list = a.GetAll();  //gets the entire collection

Notice that the client code accesses only the IAggregate interface and does not need to know how the collection is traversed. 

 

Below are the implementation code and the output of the iterator design pattern. Notice that the client code does not need  know how the collection is traversed, the ConcreteAggregate class simply creates the ConcreteIterator class and accesses the methods using the IIterator interface:

class Program
{
    static void Main(string[] args)
    {
        IAggregate<string> aggregate = new ConcreteAggregate<string>();

        aggregate.AddItem("Apple");    //add sample data
        aggregate.AddItem("Orange");
        aggregate.AddItem("Banana");
        aggregate.AddItem("Plum");

        //iterate through the collection using IAggregate only
        foreach (string i in aggregate.GetAll())
            Console.WriteLine(i);
    }
}

public interface IAggregate<t>
{
    IIterator<t> CreateIterator();
    List<t> GetAll();
    void AddItem(T item);
}

public interface IIterator<t>
{
    T First();
    T Next();
    T CurrentItem();
    bool IsDone();
    void AddItem(T item);
}

public class ConcreteAggregate<t> : IAggregate<t>
{
    private IIterator<t> iterator;

    public ConcreteAggregate()
    {
        (this as IAggregate<t>).CreateIterator();  //create the iterator
    }

    IIterator<t> IAggregate<t>.CreateIterator()
    {
        //create iterator if not already done
        if (iterator == null)
            iterator = new ConcreteIterator<t>(this);
        return iterator;
    }

    List<t> IAggregate<t>.GetAll()
    {
        List<t> list = new List<t>();
        list.Add(iterator.First());
        while (!iterator.IsDone())
        {
            list.Add(iterator.Next());
        }
        return list;
    }

    void IAggregate<t>.AddItem(T item)
    {
        iterator.AddItem(item);
    }
}

public class ConcreteIterator<t> : IIterator<t>
{
    private IAggregate<t> aggregate;

    private List<t> collection = new List<t>();  //the actual collection you are traversing
    private int pointer = 0;  //keeps track of the current position

    public ConcreteIterator(IAggregate<t> i)
    {
        aggregate = i;
    }

    T IIterator<t>.First()
    {
        //move pointer to the first element in the aggregate and return it
        pointer = 0;
        return collection[pointer];
    }

    T IIterator<t>.Next()
    {
        //move pointer to the next element in the aggregate and return it
        pointer++;
        return collection[pointer];
    }

    T IIterator<t>.CurrentItem()
    {
        //return the element that the pointer is pointing to
        return collection[pointer];
    }

    bool IIterator<t>.IsDone()
    {
        //return true if pointer is pointing to the last element, else return false
        return pointer == collection.Count - 1;
    }

    void IIterator<t>.AddItem(T item)
    {
        collection.Add(item);
    }
}

Liked this article? You can see this and other great articles on design patterns here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DevLake

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 8:14 
Look all your example are the simple examples found in any textbook and you posting them one after another is spamming in my opnion
GeneralRe: My vote of 1 [modified] PinmvpJohn Simmons / outlaw programmer4-May-11 9:21 
GeneralRe: My vote of 1 Pinmember_Maxxx_4-May-11 14:00 
GeneralRe: My vote of 1 PinmemberJOAT-MON4-May-11 16:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid