Iterator Design Pattern


Published on

Presentation on the Iterator Design Pattern.
Code is in C#

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Iterator Design Pattern

    1. 1. - Varun Arora Varun Arora | |
    2. 2. Intent <ul><li>Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. </li></ul><ul><li>Also known as “Cursor”. </li></ul>Varun Arora | |
    3. 3. Motivation <ul><li>The Iterator pattern provides a client with a way to access the elements of an aggregate object sequentially without having to know the underlying representation. </li></ul><ul><li>Iterator also provides a way to define special Iterator classes that perform unique processing and return only specific elements of the data collection. </li></ul><ul><li>The Iterator is especially useful because it provides the client with a common interface so it doesn't need to know anything about the underling data structure. </li></ul>Varun Arora | |
    4. 4. Applicability <ul><li>When do we use the Iterator Patern? </li></ul><ul><li>to access an aggregate object's contents without exposing its internal representation. </li></ul><ul><li>to support multiple traversals of aggregate objects. </li></ul><ul><li>to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration). </li></ul><ul><li>Usgae of Iterator is high. </li></ul>Varun Arora | |
    5. 5. Structure Varun Arora | |
    6. 6. Participating Objects and Classes <ul><li>The classes and/or objects participating in this pattern are: </li></ul><ul><li>Iterator    (AbstractIterator) </li></ul><ul><ul><li>defines an interface for accessing and traversing elements. </li></ul></ul><ul><li>ConcreteIterator    (Iterator) </li></ul><ul><ul><li>implements the Iterator interface. </li></ul></ul><ul><ul><li>keeps track of the current position in the traversal of the aggregate. </li></ul></ul><ul><li>Aggregate    (AbstractCollection) </li></ul><ul><ul><li>defines an interface for creating an Iterator object </li></ul></ul><ul><li>ConcreteAggregate    (Collection) </li></ul><ul><ul><li>implements the Iterator creation interface to return an instance of the proper ConcreteIterator </li></ul></ul>Varun Arora | |
    7. 7. Collaborations <ul><li>A ConcreteIterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal </li></ul>Varun Arora | |
    8. 8. Consequence <ul><li>Iterator supports variations of traversal of the same Collection (depth-first, breadth-first traversal of Trees for exp) </li></ul><ul><li>Iterators simplify the Collection interface </li></ul><ul><li>Iterators permit more than one traversal to operate simultaneously on the Collection - this is fine for read-only traversal but can be dangerous if there are writes or removals </li></ul>Varun Arora | |
    9. 9. implementation <ul><li>Iterators that emit references to the elements of the collection permit more general application behavior than Iterators that operate on elements internally These two cases are referred to as External Iterator and Internal Iterator respectively) </li></ul><ul><li>Robust Iterator ensures that insertions and removals do not disrupt or invalidate the traversal thus permitting complete freedom to operate multiple traversals simultaneously on the same collection </li></ul>Varun Arora | |
    10. 10. implementation <ul><li>Heap-allocated Iterators must be deleted by Clients (where there is no garbage collection.) It is possible to use a stack-allocated (local scope) Proxy Iterator holding and operating on the 'server' heap-allocated Iterator. Then if the proxy destructor method destroys the server Iterator the deletion is accomplished as soon as the Proxy goes out of scope </li></ul><ul><li>Sometimes an Iterator (being created inside the Collection) will gain special access priveleges </li></ul>Varun Arora | |
    11. 11. Known uses <ul><li>Iterators are common in object-oriented systems. Most collection class libraries offer iterators in one form or another. </li></ul><ul><li>Iterators don't have to be defined as explicitly in Smalltalk. The standard collection classes (Bag, Set, Dictionary, OrderedCollection, String, etc.) define an internal iterator method do:, which takes a block(i.e., closure) as an argument. Each element in the collection is bound to the local variable in the block;then the block is executed </li></ul><ul><li>Polymorphic iterators and the cleanup Proxy described earlier are provided by the ET++ container </li></ul>Varun Arora | |
    12. 12. Related patterns <ul><li>Composite - uses an Iterator to hide complexities and varities of methods of traversal Factory Method used tocreate the particular Iterator for a specific ConcreteCollection subclass </li></ul><ul><li>Memento is often used by an Iterator to keep track of the state of the traversal such as tree level and ancestor Stack during Tree traversal for example Visitor uses an Iterator to traverse the Collection of nodes it visits. </li></ul>Varun Arora | |
    13. 13. Example Code <ul><li>// Iterator pattern -- Real World example </li></ul><ul><li>using System; using System.Collections; namespace DoFactory.GangOfFour.Iterator.RealWorld {   // MainApp test application    class MainApp   {     static void Main()     {       // Build a collection        Collection collection = new Collection();       collection[0] = new Item(&quot;Item 0&quot;);       collection[1] = new Item(&quot;Item 1&quot;);       collection[2] = new Item(&quot;Item 2&quot;);       collection[3] = new Item(&quot;Item 3&quot;);       collection[4] = new Item(&quot;Item 4&quot;);       collection[5] = new Item(&quot;Item 5&quot;);       collection[6] = new Item(&quot;Item 6&quot;);       collection[7] = new Item(&quot;Item 7&quot;);       collection[8] = new Item(&quot;Item 8&quot;);       // Create iterator        Iterator iterator = new Iterator(collection);       // Skip every other item        iterator.Step = 2;       Console.WriteLine(&quot;Iterating over collection:&quot;);       for(Item item = iterator.First();          !iterator.IsDone; item = iterator.Next())       {         Console.WriteLine(item.Name);       }       // Wait for user        Console.Read();     }   }   class Item   {     string name;     // Constructor      public Item(string name)     { = name;     }     // Property      public string Name     {       get{ return name; }     }   }   // &quot;Aggregate&quot;    interface IAbstractCollection   {     Iterator CreateIterator();   }   // &quot;ConcreteAggregate&quot;    class Collection : IAbstractCollection   {     private ArrayList items = new ArrayList();     public Iterator CreateIterator()     {       return new Iterator(this);     }     // Property      public int Count     {       get{ return items.Count; }     }          // Indexer      public object this[int index]     {       get{ return items[index]; }       set{ items.Add(value); }     }   }   // &quot;Iterator&quot;    interface IAbstractIterator   {     Item First();     Item Next();     bool IsDone{ get; }     Item CurrentItem{ get; }   }   // &quot;ConcreteIterator&quot;    class Iterator : IAbstractIterator   {     private Collection collection;     private int current = 0;     private int step = 1;     // Constructor      public Iterator(Collection collection)     {       this.collection = collection;     }     public Item First()     {       current = 0;       return collection[current] as Item;     }     public Item Next()     {       current += step;       if (!IsDone)         return collection[current] as Item;       else         return null;     }     // Properties      public int Step     {       get{ return step; }       set{ step = value; }     }     public Item CurrentItem     {       get       {         return collection[current] as Item;       }     }     public bool IsDone     {       get       {         return current >= collection.Count ? true : false;       }     }   } } </li></ul>Varun Arora | |
    14. 14. Thank you Varun Arora | |