Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Iterator Pattern

Posted in Design Patterns
This post has been read 2877 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Iterator Pattern. You can read about rest of the patterns from the following links

  1. Strategy Pattern
  2. State Pattern
  3. Template Method Pattern
  4. Chain of Responsibility Pattern
  5. Command Pattern

You can read about the Structural patterns from here.
You can read about the Creational patterns from here.

The Iterator patterns deals with providing an easy way to access elements of a collection sequentially. To implement an iterator in C#, we can implement either IEnumerator or the IEnumerable interfaces.  IEnumerator interface has 2 methods GetEnumerator and Reset which we would have to implement and an Current object which is used to maintain the position. The easier way of doing it is to implement the IEnumerable interface and implement the GetEnumerator method for it. In the following example I am using the yield return statement in the GetEnumerator method which saves us from maintaining the state.

    1 namespace PatternsConsole

    2 {

    3     class IteratorPattern

    4     {

    5         class Collection<T> : IEnumerable<T>

    6         {

    7             private readonly List<T> items = new List<T>();

    8 

    9             public void Add( T t )

   10             {

   11                 items.Add( t );

   12             }

   13 

   14             public IEnumerator<T> GetEnumerator()

   15             {

   16                 for ( int i = 0; i < Count; i++ )

   17                 {

   18                     yield return items[i];

   19                 }

   20             }

   21 

   22             public int Count

   23             {

   24                 get { return items.Count; }

   25             }

   26 

   27             IEnumerator IEnumerable.GetEnumerator()

   28             {

   29                 return GetEnumerator();

   30             }

   31         }

   32 

   33         class Person

   34         {

   35             public string Name { get; private set; }

   36 

   37             public Person( string name )

   38             {

   39                 Name = name;

   40             }

   41         }

   42 

   43         private static void Main()

   44         {

   45             var people = new Collection<Person>

   46                              {

   47                                  new Person("Person 1"),

   48                                  new Person("Person 2"),

   49                                  new Person("Person 3"),

   50                                  new Person("Person 4"),

   51                                  new Person("Person 5")

   52                              };

   53 

   54             foreach ( var person in people )

   55             {

   56                 Console.WriteLine( person.Name );

   57             }

   58 

   59             // Output :

   60             // Person 1

   61             // Person 2

   62             // Person 3

   63             // Person 4

   64             // Person 5

   65 

   66             Console.ReadLine();

   67         }

   68     }

   69 }

We can implement our iterator when we want iterations over a collection or we have many ways of traversing it or when there are many collections for traversing.

In my next post I will be writing about the Mediator Pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll