Observer Pattern - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Observer Pattern

Description:

Let objects propagate information without depending on each ... If bird is not in cage and door opens, bird flies out. Bird. update: aSymbol (aSymbol == #open) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 34
Provided by: RalphJ4
Category:

less

Transcript and Presenter's Notes

Title: Observer Pattern


1
Observer 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.

2
Observer Pattern
observer/ dependent
Subject addDependent removeDependent changed
Observer update
model
ValueHolder value value
TextView update
3
Observer Pattern
  • Registration
  • Notification
  • Update

4
Registration
  • Observer registers itself as a dependent of
    subject.
  • subject addDependent observer

5
Notification
  • Object notifies dependents when information
    changes by sending itself a changed message.
  • changed anArg
  • self dependents
  • do each each update anArg

6
Update
  • Dependent can define what it does in response to
    changes by defining the update method.

7
Observer 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.
8
Dynamic 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
9
Basic classes
Object addDependent changed update
  • Dog
  • bark / move
  • Person
  • Door
  • open / close / isOpen

10
Collaborations
Dog bark
watch
listen
Person
Door openclose
11
Door
  • opened ltBooleangt
  • open
  • opened true.
  • self changed open

12
Door
  • close
  • opened false.
  • self changed close
  • isOpen
  • opened

13
Dog
  • currentState sleeping, waiting, outside
  • bark
  • currentState waiting.
  • self changed bark

14
Dog
  • goOut
  • currentState outside.
  • self changed move

15
Dog
  • update aSymbol
  • (currentState waiting) (aSymbol open)
  • ifTrue door removeDependent self.
  • self goOut

16
Person
  • dog aDog
  • dog aDog.
  • dog addDependent self

17
Person
  • update aSymbol
  • aSymbol bark
  • ifTrue door open.
  • aSymbol move
  • ifTrue door close

18
A Script
  • person door dog
  • door Door new initialize.
  • dog Dog new door door.
  • person Person new.
  • person door door dog dog.
  • dog bark.

19
Watcher
  • 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.

21
Improvements
  • Creating method
  • (have class method return initialized object)
  • Compose method
  • (Watcher on door name 'door')
  • (door watcherNamed 'door')

22
Model 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.

23
Make 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

25
Advantage of Observer Pattern
  • Easy to add new observers.
  • Coupling between observer and subject is abstract.

26
Disadvantage of Observer Pattern
  • Often hard to understand relationships between
    objects in system.
  • Sometimes inefficient.

27
Adding 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.

28
Bird
  • update aSymbol
  • (aSymbol open)
  • (self isCaged not)
  • ifTrue self flyOut
  • Script must now create a bird and make it depend
    on the door.

29
ValueModels
  • 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.

30
Old Way
  • PolygonFigure with position and color
  • position aGeometry
  • position aGeometry.
  • self changed position
  • color aColor
  • color aColor
  • self changed color

31
Client
  • update aSymbol
  • aSymbol color ifTrue self colorChanged

32
New Way
  • PolygonFigure with positionHolder and colorHolder
  • position aGeometry
  • self positionHolder value aGeometry
  • color
  • colorHolder value

33
New Way
  • Instead of depending on PolygonFigure, client
    uses
  • aFigure colorHolder onChangeSend colorChanged
    to self
Write a Comment
User Comments (0)
About PowerShow.com