Master C#

How to use the iterator design pattern in C#

Take advantage of the iterator pattern to traverse aggregate objects without having to expose their underlying data structures

How to work with the iterator design pattern in C#
Thinkstock

Master C#

Show More

The iterator design pattern is a commonly used pattern that provides a very useful abstraction. The iterator pattern is used to access and traverse the elements of a collection without the need to understand or expose the underlying structure of the collection.

Iterator is a behavioral design pattern in which you use an iterator to enumerate the elements of the container, i.e. the collection, in a sequential manner. Behavioral design patterns are those that manage object collaboration and the delegation of responsibilities among objects.

One of the advantages of the iterator pattern is that it allows you to modify the collection implementation without having to change the way the collection is accessed from the outside. As per the Dofactory, the Gang of Four defines the iterator design pattern as follows:

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

In this article we will walk through a simple implementation of the iterator design pattern using C#. The participants in a typical implementation of the iterator pattern include the following:

  1. Iterator. This is typically an interface (or an abstract class) that specifies the interface for traversing the elements of the container.
  2. ConcreteIterator. This is a class that implements the Iterator interface.
  3. Aggregate. This is typically an abstract collection that defines the interface for instantiating the iterator.
  4. ConcreteAggregate. This is a class that implements the Aggregate and returns an instance of the concrete iterator.

Create a collection 

Let’s get started. First create a console application project in Visual Studio. Then create the following class named Author.

public class Author
    {
        private string firstName, lastName;
        public Author(string firstName, string lastName)
        {
            this.firstName = firstName;
            this.lastName = lastName;
        }
        public string Name
        {
            get { return this.firstName + “\t”+ this.lastName; }
        }
    }

Note that the Author class represents the collection that we want to store inside a container and later iterate on. The Author class contains a parameter constructor that accepts two arguments, i.e., the first name and last name of the author. The Name property of the Author class returns the full name of an author.

Create the abstract container interface

Here is the abstract container interface named IAbstractContainer. This interface contains the declaration of the CreateIterator() method.

public interface IAbstractContainer
    {
        Iterator CreateIterator();
    }

Implement the container

The Container class extends the IAbstractContainer interface and implements the CreateIterator() method. It also contains a property called Count that returns the count of the items in the Author collection. There is also an indexer. We’ll use the indexer to retrieve data from the collection later.

public class Container : IAbstractContainer
    {
        private List<Author> authors = new List<Author>();
        public Iterator CreateIterator()
        {
            return new Iterator(this);
        }
        public int Count
        {
            get { return authors.Count; }
        }
        public Author this[int index]
        {
            get { return authors[index]; }
            set { authors.Add(value); }
        }
    }

Create the abstract iterator

Next on our list of types to implement is the abstract iterator. The IAbstractIterator interface contains the declaration of the operations that the concrete iterator class will implement.

public interface IAbstractIterator
    {
        Author FirstItem();
        Author NextItem();
        bool IsDone { get; }
    }

Implement the iterator

The Iterator class extends the IAbstractIterator interface. Iterator is used to encapsulate the implementation of the iteration on the collection. Note how it makes use of an index to track the iteration process.

public class Iterator : IAbstractIterator
    {
        private Container aggregate;
        private int currentIndex = 0;
        private int increment = 1;
        public Iterator(Container container)
        {
            aggregate = container;
        }
        public Author FirstItem()
        {           
           currentIndex = 0;
           return aggregate[currentIndex];
        }
        public Author NextItem()
        {
            currentIndex += increment;
            if (!IsDone)
                return aggregate[currentIndex] as Author;
           return null;
        }
        public bool IsDone
        {
            get
            {
                if (currentIndex >= aggregate.Count)
                    return true;
                return false;
            }          
        }
    }

Iterate over the elements of the collection

Lastly, we will create an instance of the container, store objects in the container, and then iterate over the elements of the collection. In the code snippet given below, note how we create five instances of the Author class, assign values to the firstName and lastName properties, and then create an instance of the Iterator by calling the CreateIterator() method on the container instance.

static void Main(string[] args)
        {
            Container container = new Container();
            container[0] = new Author(“Joydip”, “Kanjilal”);
            container[1] = new Author(“Michael”, “Stevens”);
            container[2] = new Author(“Steve”, “Jones”);
            container[3] = new Author(“Steve”, “Smith”);
            container[4] = new Author(“Jack”, “Gibbs”);
            Iterator iterator = container.CreateIterator();
            for (Author item = iterator.FirstItem(); !iterator.IsDone; item = iterator.NextItem())
            {
                Console.WriteLine(item.Name);
            }
            Console.ReadKey();
        }

Copyright © 2018 IDG Communications, Inc.