Introduction to the Iterator design pattern

No.of Views797
Bookmarked1 times
Downloads 
Votes0
By  Chinna Srihari   On  23 Oct 2010 03:10:09
Tag : Design Patterns , General
Sequentially access the elements of a collection.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 
What is Iterator Design pattern?
 
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
 
 
 Please refer to the attached doc the images
 
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);
 }

Hope this helps

Download Sample

Download source files -32 kb

 
Sign Up to vote for this article
 
About Author
 
Chinna Srihari
Occupation-Software Engineer
Company-
Member Type-Junior
Location-India
Joined date-17 Jun 2010
Home Page-
Blog Page-
Having 12 + years of IT experience, 2.5 years as Technical Management experience and 1.5 year of onsite coordinator experience and rest 8+ years of technical activities including Project Leader, Tech Lead, Designer, PQL, and Developer experience in Quality Management System (CMM-level 5, SEI-CMM-level 5).
 
 
Other popularSectionarticles
    This is a quite simple design pattern, let me tell you one thing there are many design patterns that they are implicitly singletons. I rather recommend to understanding this one first before going through other patterns.
    Published Date : 21/Oct/2010
    As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
    Published Date : 21/Oct/2010
    The Command pattern creates distance between the client that requests an operation and the object that can perform it.
    Published Date : 20/Sep/2010
    Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
    Published Date : 22/Oct/2010
    A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.
    Published Date : 22/Oct/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top