Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Iterator Design Pattern

Iterator Design Pattern

23 Aug 2022
Intermediate
32.6K Views
3 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Iterator Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is iterator pattern and how is it work?

What is Iterator Design Pattern?

Iterator Design Pattern provides a way to access the elements of a collection object in a sequential manner without knowing its underlying structure.

This pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc.

Iterator Design pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Iterator Design pattern is given below:

Iterator Design Pattern C#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. Client

    This is the class that contains an object collection and uses the Next operation of the iterator to retrieve items from the aggregate in an appropriate sequence.

  2. Iterator

    This is an interface that defines operations for accessing the collection elements in a sequence.

  3. ConcreteIterator

    This is a class that implements the Iterator interface.

  4. Aggregate

    This is an interface which defines an operation to create an iterator.

  5. ConcreteAggregate

    This is a class that implements an Aggregate interface.

C# - Implementation Code

public class Client
{
 public void UseIterator()
 {
 ConcreteAggregate aggr = new ConcreteAggregate();
 aggr.Add("One");
 aggr.Add("Two");
 aggr.Add("Three");
 aggr.Add("Four");
 aggr.Add("Five");

 Iterator iterator = aggr.CreateIterator();
 while (iterator.Next())
 {
 string item = (string)iterator.Current;
 Console.WriteLine(item);
 }
 }
}

public interface Aggregate
{
 Iterator CreateIterator();
}

public class ConcreteAggregate : Aggregate
{
 private ArrayList items = new ArrayList();

 public Iterator CreateIterator()
 {
 return new ConcreteIterator(this);
 }

 public object this[int index]
 {
 get { return items[index]; }
 }

 public int Count
 {
 get { return items.Count; }
 }

 public void Add(object o)
 {
 items.Add(o);
 }
}
public interface Iterator
{
 object Current { get; }
 bool Next();

}

public class ConcreteIterator : Iterator
{
 private ConcreteAggregate aggregate;
 int index;

 public ConcreteIterator(ConcreteAggregate aggregate)
 {
 this.aggregate = aggregate;
 index = -1;
 }

 public bool Next()
 {
 index++;
 return index < aggregate.Count;
 }

 public object Current
 {
 get
 {
 if (index < aggregate.Count)
 return aggregate[index];
 else
 throw new InvalidOperationException();
 }
 }
}


Real Life Example:

Real Life Example of Iterator Design Pattern C#

When to use it?

  1. Allows accessing the elements of a collection object in a sequential manner without knowing its underlying structure.

  2. Multiple or concurrent iterations are required over collections elements.

  3. Provides a uniform interface for accessing the collection elements.

Read More Articles Related to Design Patterns
What do you think?

I hope you will enjoy the Iterator Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this