Iterator Pattern


Most of the Object Oriented Languages nowadays supports this pattern through their core language infrastructure.

IteratorPattern1.gif

Challenge

You are working on a Name printing application. You have to deal with a Bank class with the following properties.

public class Bank
{
    public string ManagerName
    {
        get;
        set;
    }
    public string AccountantName
    {
        get;
        set;
    }
    public string CashierName
    {
        get;
        set;
    }
}

For printing the name the Print() method is called as following:

Print(_bank.Manager);
Print(_bank.Accountant);
Print(_bank.Cashier);

Now there is an addition of Branch type Bank to the above Bank object. Now the situation has become more complex. How to do a better approach?

Definition

"Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation"

Implementation

We can improve the above Challenge Situation using the Iterator pattern. Instead of calling Print() method each time, we can use an Iterator to get the names inside Bank Object.

We are introducing a new class named BankIterator which implements IEnumerable:

public class BankIterator : IEnumerable
{
    private IList _list = new ArrayList();
    public BankIterator(Bank bank)
    {
        _list.Add(bank.Manager);
        _list.Add(bank.Accountant);
        _list.Add(bank.Cashier);
    } 
    public IEnumerator GetEnumerator()
    {
        return _list.GetEnumerator();
    }
}

The class uses and ArrayList ito hold the Manager, Accountant, Cashier names. As it is inheriting IEnumerable it has to implement the GetEnumerator() method. Luckily we can send the list.GetEnumerator() for the same.

The new Print() method invocation looks like below:

BankIterator iterator = new BankIterator(_bank);
foreach (string name in iterator)
    Print(name);

This improves the situation in the following ways:

  • Easier Names access through Iteration
  • More flexibility in introducing new Name properties in the Bank class

Following is the snapshot of Class Diagrams:

IteratorPattern2.gif

Note

From C# 2.0 onwards the Iterator pattern is implemented using foreach. The foreach allows sequential accessing of the elements inside a collection. The IEnumerator interface is used to implement the Iterator pattern.

public interface IEnumerator
{
    object Current { get; } 
    bool MoveNext(); 
    void Reset();
}


While executing the foreach, first the Reset() method is called to position to the first element of the collection. Then through each iteration the MoveNext() method is called. The Current property returns the current object from the collection. The IEnumerator interface implementation of array, List, HashSet etc. enables us to iterate over them using the same foreach loop.

IteratorPattern3.gif

References

http://msdn.microsoft.com/en-us/library/dscyy5s0(v=vs.80).aspx

Summary

In this article we have explored the Iterator pattern with an example in C#. The pattern is already well implemented inside the language architecture of C#. We can extend the pattern usage in our custom classes too. The examples we discussed above are included in the source code attachment.