Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Iterator Design Pattern

, 4 May 2011 CPOL
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

Comments and Discussions

GeneralMy vote of 1 PinmemberBill SerGio, Infomercial King4-May-11 9:14 
GeneralRe: My vote of 1 [modified] PinmvpJohn Simmons / outlaw programmer4-May-11 10:21 
Pot, meet Kettle, another black cooking utensil...
bool irony = (pot.color == kettle.color);
It's a blog entry, not an article.
Apology to my man adoring fans - I'm running kinda low on clever euphimisims today. I was ill-prepared for the day's events.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
modified on Wednesday, May 4, 2011 3:32 PM

GeneralRe: My vote of 1 Pinmember_Maxxx_4-May-11 15:00 
GeneralRe: My vote of 1 PinmemberJOAT-MON4-May-11 17: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 | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 4 May 2011
Article Copyright 2011 by DevLake
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid