Title: Observer Pattern
1Observer Pattern
- Dependence mechanism / publish-subscribe /
constraints / broadcast / change-update - Let objects propagate information without
depending on each other much. - Define a one-to-many dependency between objects
so that when one object changes state, all its
dependents are notified and updated automatically.
2Observer Pattern
observer/ dependent
Subject addDependent removeDependent changed
Observer update
model
ValueHolder value value
TextView update
3Observer Pattern
- Registration
- Notification
- Update
4Registration
- Observer registers itself as a dependent of
subject. - subject addDependent observer
5Notification
- Object notifies dependents when information
changes by sending itself a changed message. - changed anArg
- self dependents
- do each each update anArg
6Update
- Dependent can define what it does in response to
changes by defining the update method.
7Observer Pattern
Intent Define a one-to-many dependency between
objects so that when one object changes state,
all its dependents are notified and updated
automatically.
8Dynamic Model
Record order of events, interaction between
objects.
Event Trace
State Machine
Dog
Person
Door
register
Need to Leave do register with door do bark at
person
bark
open
register
notify
notify
go thru door
Leaving do unregister do go through door do
notify dependents
unregister
notify
close
9Basic classes
Object addDependent changed update
- Dog
- bark / move
- Person
- Door
- open / close / isOpen
10Collaborations
Dog bark
watch
listen
Person
Door openclose
11Door
- opened ltBooleangt
- open
- opened true.
- self changed open
12Door
- close
- opened false.
- self changed close
- isOpen
- opened
13Dog
- currentState sleeping, waiting, outside
- bark
- currentState waiting.
- self changed bark
14Dog
- goOut
- currentState outside.
- self changed move
15Dog
- update aSymbol
- (currentState waiting) (aSymbol open)
- ifTrue door removeDependent self.
- self goOut
16Person
- dog aDog
- dog aDog.
- dog addDependent self
17Person
- update aSymbol
- aSymbol bark
- ifTrue door open.
- aSymbol move
- ifTrue door close
18A Script
- person door dog
- door Door new initialize.
- dog Dog new door door.
- person Person new.
- person door door dog dog.
- dog bark.
19Watcher
- instance variable name ltStringgt
- update aSymbol
- Transcript show subjectName show ' ' show
aSymbol cr - name aString
- name aString
20- person door dog
- door Door new initialize.
- door addDependent
- (Watcher new name 'door').
- dog Dog new door door.
- dog addDependent
- (Watcher new name 'Fido').
- person Person new.
- person door door dog dog.
- dog bark.
21Improvements
- Creating method
- (have class method return initialized object)
- Compose method
- (Watcher on door name 'door')
- (door watcherNamed 'door')
22Model and Memory Management
- Dog allInstances
- reports a lot of dogs! Garbage collection
doesn't help. - Object uses a global dictionary to store
dependents. Subject must "release" its
observers/dependents.
23Make Dog a subclass of Model
- Model uses an instance variable to store
dependents. It does not have to release its
observers. - Subclasses of Model cause less problems with
garbage collection. - Class that has dependents should be subclass of
Model.
24-
- If you are not using Model then after the script
says - dog bark.
- add the messages
- dog release.
- door release.
- person release
25Advantage of Observer Pattern
- Easy to add new observers.
- Coupling between observer and subject is abstract.
26Disadvantage of Observer Pattern
- Often hard to understand relationships between
objects in system. - Sometimes inefficient.
27Adding New Observer
- Suppose room also has a bird, which is usually in
a cage. - If bird is not in cage and door opens, bird flies
out.
28Bird
- update aSymbol
- (aSymbol open)
- (self isCaged not)
- ifTrue self flyOut
- Script must now create a bird and make it depend
on the door.
29ValueModels
- Instead of depending on an aspect, depend on
component model. - ValueModel - a model with two messages, value and
value - onChangeSend aSymbol to anObject
- Arrange to send aSymbol to anObject when the
reciever changes.
30Old Way
- PolygonFigure with position and color
- position aGeometry
- position aGeometry.
- self changed position
- color aColor
- color aColor
- self changed color
31Client
- update aSymbol
- aSymbol color ifTrue self colorChanged
32New Way
- PolygonFigure with positionHolder and colorHolder
- position aGeometry
- self positionHolder value aGeometry
- color
- colorHolder value
33New Way
- Instead of depending on PolygonFigure, client
uses - aFigure colorHolder onChangeSend colorChanged
to self