Title: Chapter 9 Behavioral Design Patterns
1Chapter 9Behavioral Design Patterns
2Iterator
- Design Purpose
- Provide a way to access the elements of an
aggregate object sequentially without exposing
its underlying representation - Design Pattern Summary
- Encapsulate the iteration in a class pointing (in
effect) to an element of the aggregate
3Iterator
Interface Iterator // Iterator "points" to
first item void first() // true if
iterator "points" past the last item boolean
isDone() // Causes the iterator to point to
its next item void next() // Return the item
pointed to by the iterator Object
currentItem()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
4Using Iterator Functions
/ To perform desiredOperation() on items in
the container according to the iteration (order)
i / for(i.first() !i.isDone()
i.next()) desiredOperation(i.currentItem())
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
5Iterator Example Setup Code
// Suppose that we have iterators for forward
and // backward order we can re-use
print_employees() List employees new
List() ForwardListIterator fwd new
ForwardListIterator(employees) ReverseListIterato
r bckwd new ReverseListIterator(employees) /
/ print from front to back client.print_employees
(fwd) // print from back to front
client.print_employees(bckwd)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
6Iterator Class Model
Client
Iterator first() next() isDone() currentItem()
Aggregate createIterator() Append() Remove()
ConcreteAggregate createIterator()
ConcreteIterator
Return new ConcreteIterator()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
7Mediator
- Design Purpose
- Avoid references between dependent objects.
- Design Pattern Summary
- Capture mutual behavior in a separate class.
8The Mediator Class Model
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
9Mediator Sequence Diagrams
1. Initiation by ConcreteMediator
2. Initiation on a ConcreteColleague
10Key Concept Mediator
-- to capture mutual behavior without direct
dependency.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
11Observer
- Design Purpose
- Arrange for a set of objects to be affected by a
single object. - Design Pattern Summary
- The single object aggregates the set, calling a
method with a fixed name on each member.
12Observer Design Pattern
Client part
Server part
Client of this system
1
Source notify()
Observer update()
1..n
observers
2
for each of observers o.update()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
13Observer Design Pattern
Observer update()
Source notify()
Client
1
1..n
2
for all Observers o o.update()
ConcreteObserver observerState update()
ConcreteSource state
3
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
14Observer in the Java API
Observable notifyObservers()
Observer update( Observable, Object )
MyConcreteObserver observerState update()
MyObservable
subject
Developer Class
Java API Class
Key
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
15Key Concept Observer
-- to keep a set of objects up to date with the
state of a designated object.
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
16Command
- Design Purpose
- Increase flexibility in calling for a service
e.g., allow undo-able operations. - Design Pattern Summary
- Capture operations as classes.
17The Command Design Pattern
Client
Command execute()
replaced
Action1Command execute()
Target1 action1()
Target2 action2()
Action2Command execute()
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
18Command Example
MenuItem handleClick()
Command execute()
command
document.cut()
command.execute()
CutCommand execute()
document
Document cut() copy()
document.copy()
CopyCommand execute()
document
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
19Sequence Diagram for Command Example
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
20Key Concept Command
-- to avoid calling a method directly (e.g., so
as to record or intercept it).
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.
21Summary of Behavioral Patterns
- Iterator visits members of a collection
- Mediator captures behavior among peer objects
- Observer updates objects affected by a single
object - Command captures function flexibly (e.g.
undo-able)
Adapted from Software Design From Programming to
Architecture by Eric J. Braude (Wiley 2003), with
permission.