7 Patterns-442-12-by6

concretefactory1 createproducta createproductb

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t backward as well as forward traversals Support for traversal actions as distinct from traversal Hide implementation of composite objects (aggregate type) 77 Iterator pattern Name Iterator or Cursor Applicability Pattern Access aggregate object while hiding object s implementation (next operation indicates linked list, index indicates array, etc.) Support multiple traversal strategies for aggregate object -- forward vs. backward traversal, e.g., of parse tree -- support Miltering traversals Provide a uniform interface for set of different aggregate objects 78 Iterator pattern Pattern Pattern Iterator pattern Fundamentals of pattern structure: DeGine Iterator abstract class with protocol for a subset of concrete iterators DeGine a concrete iterator subclass for each type of aggregate object Each aggregate instance creates an instance of an appropriate iterator object Each aggregate instance keeps reference to corresponding iterator object 79 80 Key idea Separate aggregate structures from their traversal protocols Support: addition of traversal functionality, small interfaces for aggregate classes, multiple simultaneous traversals Pattern AbstractList CreateIterator() Count() Append(Item) Remove(Item) ... Iterator First() Next() IsDone() CurrentItem() Iterator pattern (cont Consequences Pattern d) 1. Support different kinds of traversal strategies for complex structures (e.g., abstract syntax trees) Client Structure of Iterator pattern ListIterator Simply change iterator instance associated with an aggregate (creating new concrete Iterator subclass if necessary) 2. Simplify aggregate s interface Gone are the traversal protocols 3. Multiple concurrent traversals possible Useful when multiple asynchronous processes share aggregate List SkipList SkipListIterator 81 82 Iterator pattern (cont Implementation issues Pattern d) Iterator pattern Implementation issues (cont d) 2. Who deGines the traversal algorithm? Iterator object is obvious choice Pattern 1. External vs. internal iterators External: the client controls the iteration by requesting next operation of iterator Advantage: Very Glexible (e.g., can t do logical equality or simple set operations easily with internal iterators) Internal: iterator controls iteration by applying an operation to all elements of aggregate (Giltering if necessary) Advantage: Easy to use, although full power would require passing function objects to it...
View Full Document

Ask a homework question - tutors are online