Lecture_14_-_Iterator by AbdulAleem5


More Info
									Software Design and Architecture
Behavioral Design Patterns

                             Muhammad Nasir

   Intent
       Provide a way to access the elements of an
        aggregate object sequentially without
        exposing its underlying presentation.
   Also Known As
Iterator - Motivation
   An aggregate object such as a linked list or Hash
    Table should allow a way to traverse its elements
    without exposing its internal structure.
       Aggregate Object is also called Collection.
   It should allow different traversal methods
   It should allow multiple traversals to be in progress
   But, we really do not want to add all these methods
    in the aggregate object.
   Separating the traversal mechanism from the List
    object lets us define iterators for different traversal
    policies without changing the collection objects.
Iterator - Motivation
   Example I
    List list = new List();
    ListIterator iterator = new ListIterator(list);
    while (!iterator.IsDone())
        Object item = iterator.CurrentItem();
        // Code here to process item.
Iterator - Applicability

   Use the Iterator pattern…
       to access an aggregate object's contents without
        exposing its Internal representation.
       to support multiple traversals of aggregate
       to provide a uniform interface for traversing
        different Aggregate structures (that is, to support
        polymorphic iteration).
Iterator - Structure
Iterator - Participants
   Iterator
       Defines an interface for accessing and traversing
   ConcreteIterator
       Implements the Iterator interface
       Keeps track of the current position in the traversal
   Aggregate
       Defines an interface for creating an Iterator object (a
        factory method!)
   ConcreteAggregate
       Implements the Iterator creation interface to return an
        instance of the proper ConcreteIterator
Iterator - Collaborations
   A ConcreteIterator keeps track of the current object
    in the aggregate and can compute the succeeding
    object in the traversal.
Iterator - Consequences

   Simplifies the interface of the Aggregate by
    not polluting it with traversal methods
   Supports multiple, concurrent traversals
   Supports variant traversal techniques
Iterator – Related Patterns

   Factory Method - Polymorphic iterators
    use factory methods to instantiate the
    appropriate iterator subclass.
   Composite - Iterators are often used to
    recursively traverse composite structures.
The End

   Thanks for listening
   Questions would be appreciated…

To top