Professional Documents
Culture Documents
Reactor Design Patterns: Command and Observer: © 2007 Pearson Education, Inc. Publishing As Pearson Addison-Wesley
Reactor Design Patterns: Command and Observer: © 2007 Pearson Education, Inc. Publishing As Pearson Addison-Wesley
Objectives
To present the structure, behavior,
and characteristics of reactor
patterns
To compare and contrast the uses of
reactor patterns and event-driven
architectures in event-driven design
To present the Command pattern
To present the Observer pattern
2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
Event-driven design
Reactor patterns
Function objects
The Command pattern
The Observer pattern
Event-Driven Design
Event-driven design is an approach to
program design that focuses on events to
which a program must react.
An event is a significant occurrence
Contrasts with stepwise refinement
Target
notifies
Reactor
Behavioral Phases
Setup PhaseThe Client registers the
Reactor with the Target.
Client interacts with the Target
:Target
r:Reactor
register(r)
stimulus()
Operational
Phase
notify()
Differences
Level of abstraction
Event dispatcher completely decouples targets
and reactors
Event dispatchers are complex and harder to
use
Event dispatch may damage performance
2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Function Objects
In some languages operations can be
treated as values (stored, passed, etc.).
This is not usually true in object-oriented
languagesuse objects to encapsulate
operations.
A function object or functor is an object
that encapsulates an operation; the
encapsulating class is a function class.
10
InsertionSort
BubbleSort
SelectionSort
HeapSort
Quicksort
11
12
13
14
ConcreteCommand
15
sd CommandBehavior
:Client
Setup
Phase
:Invoker
c:Command
register(c)
stimulus()
Operational
Phase
execute()
16
JButton
notifies
interface
ActionListener
actionPerformed( ActionEvent e )
AnActionListener
17
18
19
20
Observer
Notification operation
21
register ( o : Observer )
unregister( o : Observer )
notifyObservers()
ConcreteSubject
notifies
*
interface
Observer
update( s : Subject)
ConcreteObserver
22
s:ConcreteSubject
o1:ConcreteObserver
o2:ConcreteObserver
register(o1)
Setup
Phase
register(o2)
stimulus()
notifyObservers()
update(s)
Operational
Phase
getState()
update(s)
getState()
23
Clock
getTime() : int
getDay() : Day
notifies
*
interface
Observer
update( s : Subject )
IrrigationControl
AutoCycle
UserInterface
24
25
Summary 1
Reactor patterns use a Reactor to which a
Client delegates responsibility for
responding to events in a Target.
Reactor patterns help realize event-driven
designs in a cheaper and easier way than
event-driven architectures at the expense
of slightly higher component coupling.
The reactor patterns help decouple targets
from their both clients and reactors.
26
Summary 2
The Command pattern uses a function
object as a reactor; the function object
encapsulates the reaction and can be used
for other purposes as well.
The Observer pattern has a subject with
which observers register; the subject then
notifies its observers of changes, and the
observers query to subject to determine
how to react.
27