Please enable Javascript to correctly display the contents on Dot Net Tricks!

Iterator Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 2,370   
Updated On : 23 Feb 2017
 

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 share what is iterator pattern and how is it work?

What is Iterator Design Pattern?

This pattern is provides a way to access the elements of a collection object in 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 collections and uses 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 Iterator interface.

  4. Aggregate

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

  5. ConcreteAggregate

    This is a class that implements 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 to access the elements of a collection object in 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.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
6 JUL
ASP.NET Core with Angular4 Development (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/T

Know More
5 JUL
NodeJS with Angular4 Development (Online)

08:00 PM-09:30 PM IST(+5.30 GMT)M/W/F

Know More
1 JUL
ASP.NET Core with Angular4 Development (Classroom)

3:00PM 5:00PM Weekend

Know More
1 JUL
Xamarin Forms : Build Cross-platform Apps (Classroom)

3:00 PM 5:00 PM Weekend

Know More
30 JUN
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5.30 GMT) MON/WED/FRI

Know More
24 JUN
ASP.NET MVC with Angular4 Development (Classroom)

8:00 AM-10:00 PM Weekend

Know More
24 JUN
ASP.NET MVC with Angular4 Development (Online)

03:30 PM-05:30 PM IST(+5.30 GMT) Weekend

Know More
24 JUN
NodeJS with Angular4 Development (Classroom)

10:00 AM-12:00 PM Weekend

Know More
23 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

Know More
23 JUN
Xamarin Forms : Build Cross-platform Apps (Online)

9:30PM- 11:00PM IST(+5:30GMT) M/W/F

20 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST(+5:30 GMT)T/W/T

6 JUN
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)(TT)

3 JUN
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:30 PM-7:00 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+