Take advantage of the iterator pattern to traverse aggregate objects without having to expose their underlying data structures Credit: Thinkstock 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: Iterator. This is typically an interface (or an abstract class) that specifies the interface for traversing the elements of the container. ConcreteIterator. This is a class that implements the Iterator interface. Aggregate. This is typically an abstract collection that defines the interface for instantiating the iterator. 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(); } Related content analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft rebrands Azure AI Studio to Azure AI Foundry The toolkit for building generative AI applications has been packaged with new updates to form the Azure AI Foundry service. By Anirban Ghoshal Nov 19, 2024 4 mins Microsoft Azure Generative AI Development Tools feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development news JetBrains IDEs ease debugging for Kubernetes apps Version 2024.3 updates to IntelliJ, PyCharm, WebStorm, and other JetBrains IDEs streamline remote debugging of Kubernetes microservices and much more. By Paul Krill Nov 14, 2024 3 mins Integrated Development Environments Java Python Resources Videos