SIGN UP MEMBER LOGIN:    
ARTICLE

What is Iterator Design Pattern?

Posted by Srihari Chinna Articles | Design & Architecture October 25, 2010
The Iterator pattern provides a way of accessing elements of a collection sequentially, without knowing how the collection is structured. As an extension, the pattern allows for filtering elements in a variety of ways as they are generated.
Reader Level:

Sequentially access the elements of a collection.

The Iterator pattern provides a way of accessing elements of a collection sequentially, without knowing how the collection is structured. As an extension, the pattern allows for filtering elements in a variety of ways as they are generated.

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

Design

The concept of iterators and enumerators (also called generators) has been around for a long time. Enumerators are responsible for producing the next element in a sequence defined by certain criteria. Such a sequence is said to be enumerable. Examples might be the next prime number or the next product order sorted by date. The iterator is the means by which we cycle through this sequence of elements from beginning to end.

UML Diagram

image1.gif

C# support for the pattern

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class People : IEnumerable
    {
        private Person[] _person;

        public People(Person[] person)
        {
            _person = new Person[person.Length];

            for (int i = 0; i < person.Length; i++)
                _person[i] = person[i];
        }

        public IEnumerator GetEnumerator()
        {
            foreach (Person p in _person)
                yield return p; //Yield return value to the Enumerator object.
        }
    }

       static void Main(string[] args)
      {
            Person[] peopleArray = new Person[3]
                {
            new Person(){ FirstName= "Chinna",  LastName = "Srihari"},
            new Person(){ FirstName= "Chinna",  LastName="Sushma"},
            new Person() { FirstName= "Chinna", LastName= "Lohetha"},
            };

            People p = new People(peopleArray);

            foreach (Person p1 in p)
                Console.WriteLine(p1.FirstName + ", " + p1.LastName);
 }

Login to add your contents and source code to this article
share this article :
post comment
 

good one man..

Posted by Suthish Nair Oct 25, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor