Click here to Skip to main content
13,295,828 members (54,197 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 25 Apr 2011

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

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

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>();
        while (!iterator.IsDone())
        return list;

    void IAggregate<t>.AddItem(T 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
        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)

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


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Bill SerGio, Infomercial King4-May-11 9:14
memberBill SerGio, Infomercial King4-May-11 9:14 
GeneralRe: My vote of 1 [modified] Pin
John Simmons / outlaw programmer4-May-11 10:21
mvpJohn Simmons / outlaw programmer4-May-11 10:21 
GeneralRe: My vote of 1 Pin
_Maxxx_4-May-11 15:00
member_Maxxx_4-May-11 15:00 
GeneralRe: My vote of 1 Pin
JOAT-MON4-May-11 17:46
memberJOAT-MON4-May-11 17:46 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid