Daily Patterns #17
-
Upload
vladislav-bauer -
Category
Documents
-
view
216 -
download
0
Transcript of Daily Patterns #17
-
7/31/2019 Daily Patterns #17
1/3
Iterator design pattern...
Intent
1. Provide a way to access theelements of an aggregate objectsequentially without exposing its
underlying representation.
2. The C++ and Java standard libraryabstraction that makes it possible todecouple collection classes and
algorithms.
3. Promote to full object statusthe traversal of a collection.
4. Polymorphic traversal
Problem
Need to abstract the traversal ofwildly different data structures sothat algorithms can be defined thatare capable of interfacing with each
transparently.
An aggregate object such as a list
should give you a way to access itselements without exposing its internalstructure. Moreover, you might want
to traverse the list in different ways,depending on what you need to
accomplish. But you probably dontwant to bloat the List interface withoperations for different traversals,even if you could anticipate the ones
youll require. You might also need tohave more than one traversal pending
on the same list. And, providing auniform interface for traversing many
types of aggregate objects (i.e.polymorphic iteration) might be
valuable.
The Iterator pattern lets you do allthis. The key idea is to take theresponsibility for access and
traversal out of the aggregate
object and put it into an Iteratorobject that defines a standardtraversal protocol.
The Iterator abstraction isfundamental to an emerging
technology called genericprogramming. This strategyseeks to explicitly separatethe notion of algorithm
from that of datastructure. The motivation isto: promote component-based development, boost
productivity, and reduceconfiguration management.
As an example, if you wantedto support four data
structures (array, binarytree, linked list, and hash
table) and three algorithms(sort, find, and merge), a
traditional approach wouldrequire four times threepermutations to developand maintain. Whereas, ageneric programmingapproach would only
require four plus threeconfiguration items.
http://www.youtube.co
m/watch?v=uY8n7hIivMU
http://www.youtube.com/watch?v=uY8n7hIivMUhttp://www.youtube.com/watch?v=uY8n7hIivMUhttp://www.youtube.com/watch?v=uY8n7hIivMUhttp://www.youtube.com/watch?v=uY8n7hIivMU -
7/31/2019 Daily Patterns #17
2/3
The Client uses theCollection class public
interface directly. Butaccess to the Collections
elements is encapsulatedbehind the additional level ofabstraction called Iterator.
Each Collection derivedclass knows which Iteratorderived class to create andreturn. After that, the Client
relies on the interfacedefined in the Iterator base
class.
The Iterator provides ways toaccess elements of an aggregate
object sequentially withoutexposing the underlying structure
of the object. Files areaggregate objects. In office
settings where access to files ismade through administrative orsecretarial staff, the Iterator
pattern is demonstrated with thesecretary acting as the Iterator.Several television comedy skitshave been developed around thepremise of an executive trying tounderstand the secretarys filingsystem. To the executive, the filingsystem is confusing and illogical,
but the secretary is able toaccess files quickly and
efficiently.
On early television sets, a dial was usedto change channels. When channel surfing,the viewer was required to move the dial
through each channel position, regardlessof whether or not that channel had
reception. On modern television sets, anext and previous button are used. Whenthe viewer selects the next button, thenext tuned channel will be displayed.
Consider watching television in a hotelroom in a strange city. When surfingthrough channels, the channel number is
not important, but the programming is. Ifthe programming on one channel is not ofinterest, the viewer can request the next
channel, without knowing its number.
-
7/31/2019 Daily Patterns #17
3/3
1. Add a create_iterator() methodto the collection class, and
grant the iterator classprivileged access.
2. Design an iterator class that
can encapsulate traversal of thecollection class.
3. Clients ask the collectionobject to create an iterator
object.
4. Clients use the first(), is_done(),next(), and current_item() protocol
to access the elements of thecollection class.
1. The abstract syntax tree ofInterpreter is a Composite (therefore
Iterator and Visitor are alsoapplicable).
2. Iterator can traverse a Composite.
Visitor can apply an operation over aComposite.
3. Polymorphic Iterators rely on FactoryMethods to instantiate the appropriate
Iterator subclass.
4. Memento is often used in conjunctionwith Iterator. An Iterator can use aMemento to capture the state of aniteration. The Iterator stores the
Memento internally.
Java has an Iterator interface that the Collections should implement in orderto traverse the elements of the collection.
Iterator iter = list.iterator();
Introduced in the Java JDK 1.2 release, the java.util.Iterator interface allowsthe iteration of container classes.
Each Iterator provides a next() and hasNext() method, and may optionallysupport a remove() method. Iterators are created by the correspondingcontainer class, typically by a method named iterator().
The next() method advances the iterator and returns the value pointed to by theiterator. The first element is obtained upon the first call to next().
To determine when all the elements in the container have been visited thehasNext() test method is used.
To show that hasNext() can be called repeatedly, we use it to insert commasbetween the elements but not after the last element.
For collection types which support it, the remove() method of the iteratorremoves the most recently visited element from the container while keeping the
iterator usable. Adding or removing elements by calling the methods ofcontainer (also from the same thread) makes the iterator unusable.