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.

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"