Iterator Design Pattern Jim Fawcett CSE 776 Design
![Iterator Design Pattern Jim Fawcett CSE 776 – Design Patterns Fall 2014 Iterator Design Pattern Jim Fawcett CSE 776 – Design Patterns Fall 2014](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-1.jpg)
![Intent • Provide a way to access elements of an aggregate object without exposing Intent • Provide a way to access elements of an aggregate object without exposing](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-2.jpg)
![Motivation • Abstract the traversal of different data structures so that algorithms can interface Motivation • Abstract the traversal of different data structures so that algorithms can interface](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-3.jpg)
![Motivation • Instance of List class is traversed with a List. Iterator • List. Motivation • Instance of List class is traversed with a List. Iterator • List.](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-4.jpg)
![Forces • Separating traversal mechanism from the aggregate object lets us define iterators for Forces • Separating traversal mechanism from the aggregate object lets us define iterators for](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-5.jpg)
![Polymorphic Iteration • We want the code using iteration to be independent of the Polymorphic Iteration • We want the code using iteration to be independent of the](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-6.jpg)
![Motivation Structure • Create. Iterator() is a factory method • We use it to Motivation Structure • Create. Iterator() is a factory method • We use it to](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-7.jpg)
![Applicability • Use the Iterator Pattern to: • Access an aggregate object’s contents without Applicability • Use the Iterator Pattern to: • Access an aggregate object’s contents without](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-8.jpg)
![Structure Structure](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-9.jpg)
![Participants • Iterator • Defines interface for accessing and traversing elements • Concrete. Iterator Participants • Iterator • Defines interface for accessing and traversing elements • Concrete. Iterator](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-10.jpg)
![Collaborations • Concrete. Iterator keeps track of the current object in the aggregate • Collaborations • Concrete. Iterator keeps track of the current object in the aggregate •](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-11.jpg)
![Consequences • Supports variations in the traversal of an aggregate • Simplifies the Aggregate Consequences • Supports variations in the traversal of an aggregate • Simplifies the Aggregate](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-12.jpg)
![Implementation • Who controls iteration? • Client – successive steps • Aggregate – DFS Implementation • Who controls iteration? • Client – successive steps • Aggregate – DFS](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-13.jpg)
![Sample Code • Iterator Skeleton • STL Iterator Examples • . Net Iterator Examples Sample Code • Iterator Skeleton • STL Iterator Examples • . Net Iterator Examples](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-14.jpg)
![Known Uses • C++ • STL <vector>, <list>, <string>, <iostreams>, … • Arrays using Known Uses • C++ • STL <vector>, <list>, <string>, <iostreams>, … • Arrays using](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-15.jpg)
- Slides: 15
![Iterator Design Pattern Jim Fawcett CSE 776 Design Patterns Fall 2014 Iterator Design Pattern Jim Fawcett CSE 776 – Design Patterns Fall 2014](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-1.jpg)
Iterator Design Pattern Jim Fawcett CSE 776 – Design Patterns Fall 2014
![Intent Provide a way to access elements of an aggregate object without exposing Intent • Provide a way to access elements of an aggregate object without exposing](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-2.jpg)
Intent • Provide a way to access elements of an aggregate object without exposing its underlying representation • Traverse many different data structures in a uniform way • Place “bookmarks” in a large collection of data
![Motivation Abstract the traversal of different data structures so that algorithms can interface Motivation • Abstract the traversal of different data structures so that algorithms can interface](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-3.jpg)
Motivation • Abstract the traversal of different data structures so that algorithms can interface with each transparently
![Motivation Instance of List class is traversed with a List Iterator List Motivation • Instance of List class is traversed with a List. Iterator • List.](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-4.jpg)
Motivation • Instance of List class is traversed with a List. Iterator • List. Iterator provides an interface for accessing List’s elements • An iterator is responsible for keeping track of the current element
![Forces Separating traversal mechanism from the aggregate object lets us define iterators for Forces • Separating traversal mechanism from the aggregate object lets us define iterators for](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-5.jpg)
Forces • Separating traversal mechanism from the aggregate object lets us define iterators for different traversal policies without enumerating them in List interface • Forward iteration, reverse iteration, preorder, postorder, … • The iterator and aggregate are coupled • Iterator needs to know aggregate structure • Aggregate may need to give iterator special permissions, e. g. , friend status in C++
![Polymorphic Iteration We want the code using iteration to be independent of the Polymorphic Iteration • We want the code using iteration to be independent of the](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-6.jpg)
Polymorphic Iteration • We want the code using iteration to be independent of the type of aggregate being used. • C++ Standard Template Library (STL) uses, by convention, a standard interface for iteration based on pointer syntax: • ++iter, *iter, iter->fun(arg) • . Net provides IEnumerator interface • Current property • Move. Next, Reset methods
![Motivation Structure Create Iterator is a factory method We use it to Motivation Structure • Create. Iterator() is a factory method • We use it to](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-7.jpg)
Motivation Structure • Create. Iterator() is a factory method • We use it to let client ask for an iterator appropriate for the aggregate being used • . Net uses the IEnumerable. Get. Enumerator() • C++ uses Container: : Iterator
![Applicability Use the Iterator Pattern to Access an aggregate objects contents without Applicability • Use the Iterator Pattern to: • Access an aggregate object’s contents without](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-8.jpg)
Applicability • Use the Iterator Pattern to: • Access an aggregate object’s contents without exposing its internal representation • Support multiple traversals of aggregate’s objects • Provide a uniform interface for traversing different aggregate structures
![Structure Structure](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-9.jpg)
Structure
![Participants Iterator Defines interface for accessing and traversing elements Concrete Iterator Participants • Iterator • Defines interface for accessing and traversing elements • Concrete. Iterator](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-10.jpg)
Participants • Iterator • Defines interface for accessing and traversing elements • Concrete. Iterator • Implements the Iterator interface • Keeps track of current position in traversal • Aggregate • Defines interface for creating an Iterator object • Concrete. Aggregate • Implements the Iterator creation interface to return an instance of its associated Concrete. Iterator
![Collaborations Concrete Iterator keeps track of the current object in the aggregate Collaborations • Concrete. Iterator keeps track of the current object in the aggregate •](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-11.jpg)
Collaborations • Concrete. Iterator keeps track of the current object in the aggregate • Makes current element accessible to client • Computes the succeeding object in traversal
![Consequences Supports variations in the traversal of an aggregate Simplifies the Aggregate Consequences • Supports variations in the traversal of an aggregate • Simplifies the Aggregate](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-12.jpg)
Consequences • Supports variations in the traversal of an aggregate • Simplifies the Aggregate interface • More than one traversal can be pending on an aggregate
![Implementation Who controls iteration Client successive steps Aggregate DFS Implementation • Who controls iteration? • Client – successive steps • Aggregate – DFS](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-13.jpg)
Implementation • Who controls iteration? • Client – successive steps • Aggregate – DFS on Tree • Who defines the traversal algorithm? • Preorder, postorder • How robust is the iterator? • Additional Iterator operations • Using polymorphic iterators in C++ • Iterators may need priviledged access • Iterators for composites • Null iterators
![Sample Code Iterator Skeleton STL Iterator Examples Net Iterator Examples Sample Code • Iterator Skeleton • STL Iterator Examples • . Net Iterator Examples](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-14.jpg)
Sample Code • Iterator Skeleton • STL Iterator Examples • . Net Iterator Examples
![Known Uses C STL vector list string iostreams Arrays using Known Uses • C++ • STL <vector>, <list>, <string>, <iostreams>, … • Arrays using](https://slidetodoc.com/presentation_image_h2/3150cc7573c2698c6efe5ef39f1b9ac8/image-15.jpg)
Known Uses • C++ • STL <vector>, <list>, <string>, <iostreams>, … • Arrays using native pointers • . Net • System. Collections, System. Collections. Generic • Java • List. Iterator<E>, XMLEvent. Reader