© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns:...
-
Upload
kristian-ball -
Category
Documents
-
view
212 -
download
0
Transcript of © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Mid-Level Design Patterns:...
1© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Mid-Level Design Mid-Level Design Patterns: Iteration Patterns: Iteration and Iteratorsand Iterators
2© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
ObjectivesObjectives
To introduce the problem of collection iteration
To present a case study about how to design an iteration mechanism
To introduce the Iterator design pattern as an object-oriented realization of the best iteration mechanism design alternative
To present the structure, behavior, and uses of the Iterator pattern
3© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
TopicsTopics
Collections and iteration Iteration mechanisms Iteration mechanism design
alternatives Selecting a design alternative Iteration mechanism robustness The Iterator pattern
4© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Collections and IterationCollections and Iteration
A collection is an object that holds or contains other objects.
Iteration over a collection or collection iteration is traversal and access of each element of a
collection.
A collection is an object that holds or contains other objects.
Iteration over a collection or collection iteration is traversal and access of each element of a
collection.
5© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iteration Iteration MechanismsMechanisms
An iteration mechanism is a language feature or a set of operations that allow clients to access each element of a collection.
For example:•Java and Visual Basic have for-loop
constructs that support iteration over collections
•Several Java collections have size() and get() operations that allow element access in a standard for-loop
6© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iteration Mechanism Iteration Mechanism OperationsOperations
Initialize—Prepare the collection for traversal
Access current element—Provide client access to the current element
Advance current element—Move on to the next element in the collection
Completion test—Determine whether traversal is complete
7© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Other Iteration Other Iteration Mechanism Mechanism RequirementsRequirements
Information hiding—The internal structure of the collection must not be exposed.
Multiple simultaneous iteration—It must be possible to do more than one iteration at a time.
Collection interface simplicity—The collection interface must not be cluttered up with iteration controls.
Flexibility—Clients should have flexibility during collection traversal.
8© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iteration Mechanism Iteration Mechanism Design Alternatives: Design Alternatives: ResidenceResidence
Iteration mechanism residence:• Programming language—As in Java or
Visual Basic Depends on the language
• Collection—A built-in iteration mechanism resides in the collection
• Iterator—An external entity housing the iteration mechanism An iterator is an entity that provides serial
access to each elements of an associated collection.
9© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iteration Mechanism Iteration Mechanism Design Alternatives: Design Alternatives: ControlControl
Iteration mechanism control:• External iteration control—The iteration
mechanism provides access to collection elements as directed by the client; the client calls the iteration control operations.
• Internal iteration control—The iteration mechanism accepts operations from clients that it applies to elements of the collection; the iteration mechanism calls the iteration control operations.
10© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iteration Mechanism Iteration Mechanism Design Alternatives: Design Alternatives: SummarySummary
Residence
Collection Iterator
Control
ExternalCollection with built-in external control
Iterator with external control
InternalCollection with built-in internal control
Iterator with internal control
11© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Built-In Internal Built-In Internal Control: Control: ImplementationImplementation
Collection
apply( op : Operation(Object) ) for each object o in collection: op( o )
printObject( o : Object ) { print( o )}…Collection c…c.apply( printObject )
12© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Built-In Internal Built-In Internal Control: EvaluationControl: Evaluation
Hides collection internals Does not complicate the collection interface
Multiple simultaneous iteration is not easy
Client has little control over iteration—no flexibility
13© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Built-In External Built-In External Control: Control: ImplementationImplementation
For each kind of iteration desired•Add the iteration control
operations (or their equivalents) to the collection
•Other operations may be needed to provide flexibility
14© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Built-In External Built-In External Control: EvaluationControl: Evaluation
Hides collection internals Greatly complicates the collection interface
Multiple simultaneous iteration is not easy
Client has control over iteration—adequate flexibility
15© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iterators with Internal Iterators with Internal ControlControl
Hides collection internals Does not unduly complicate the collection interface
Multiple simultaneous iteration is allowed
Client has little control over iteration—no flexibility
16© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iterators with External Iterators with External ControlControl
Hides collection internals Does not unduly complicate the
collection interface Multiple simultaneous iteration is
allowed Client has control over iteration—
adequate flexibility Iterators with external control is clearly
the best design alternative—this is the basis for the Iterator design pattern.
17© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Change During Change During IterationIteration
What should happen when a collection is changed during iteration?
Requirements for a coherent iteration mechanism specification:• Fault tolerance—The program should not
crash.• Iteration termination—Iteration should halt.• Complete traversal—Elements always present
should not be missed during traversal.• Single access—No element should be
accessed more than once.
18© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Robust Iteration Robust Iteration MechanismMechanism
A robust iteration mechanism is one that conforms to some coherent specification of behavior when its associated collection changes during iteration.
Making an iteration mechanism robust may be difficult or expensive.
19© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iterator PatternIterator Pattern
The Iterator pattern is an object-oriented design pattern
for externally controlled iterators.
The Iterator pattern is an object-oriented design pattern
for externally controlled iterators.
20© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
An AnalogyAn Analogy
Consider a warehouse full of items that a client must process one by one.
• Don’t allow clients into the warehouse (information hiding)
• Clerks are like iterators• Clerks can fetch each item for clients
(external control)• Clerks can be instructed by the client and
then process each element on their behalf (internal control)
21© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iterator StructureIterator Structure
«supplier»ConcreteCollection
«broker»ConcreteIterator
* 1
iterator() : Iterator
«interface»Collection
«interface»Iterator
Client reset()isDone() : booleangetCurrent() : Objectnext()
22© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Iterator BehaviorIterator Behavior
i:ConcreteIterator
:Client :ConcreteCollection
i=iterator()
reset()
e=getCurrent()
next()
done=isDone()
done=isDone()
X
create
[!done]
sd IteratorBehavior
loop
23© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
When to Use IteratorsWhen to Use Iterators
Whenever traversing any collection• Helps hide information• Helps decouple code from particular
collections (increases changeability)• Simplifies collection interfaces• Supports multiple concurrent iterations
Problems with iterators• Robustness
24© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary 1Summary 1
Collection iteration is an important task that can be realized by an iteration mechanism in many ways:
• Programming language feature• Built-in to the collection with internal or
external control• Iterator with internal or external control
If an iteration mechanism is not part of the programming language, the best design alternative is an iterator with external control.
25© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary 2Summary 2
The Iterator pattern is a model for an object-oriented implementation of an iterator with external control.
The Iterator pattern is well-known and widely implemented; it offers many advantages and should be the used whenever a collection is traversed.