![Page 1: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/1.jpg)
Observer, Strategy, Decorator, Prototype,
![Page 2: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/2.jpg)
Observer Pattern
Dependence mechanism / publish-subscribe / event handler / constraints / broadcast
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.
![Page 3: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/3.jpg)
Observer Pattern
ObserverObserver
update:update:
SubjectSubject
addDependent: addDependent: removeDependent: removeDependent: changed:changed:
ValueHolderValueHoldervalue:value:valuevalue
TextViewTextView
update:update:
observer/observer/dependentdependent
modelmodel
![Page 4: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/4.jpg)
Observer Pattern:
Registrationsubject addDependent: observer
Notificationself changed. self changed: #value
Updatedefine update: aSymbol
![Page 5: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/5.jpg)
Notification
Object notifies dependents when information changes by sending itself a #changed message.
changed: anArg
self dependents
do: [:each | each update: anArg]
![Page 6: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/6.jpg)
Problem
A man and dog are in the room. When the dog wants to go out, he barks. When the man hears the dog barking, he opens the door. When the dog wants to go out and the door is open, he leaves.
![Page 7: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/7.jpg)
Basic classes
Dog
bark / move
Person
Door
open / close / isOpen
Object
addDependent:
changed:
update:
![Page 8: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/8.jpg)
Collaborations
PersonDoor
openclose
Dog
bark listenwatch
![Page 9: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/9.jpg)
Dynamic ModelRecord order of events, interaction between objects.Record order of events, interaction between objects.
DogDog PersonPerson DoorDoor
Sequence diagramSequence diagram
barkbarkopenopennotifynotify
go thru doorgo thru door
notifynotifycloseclose
registerregisterregisterregister
unregisterunregister
notifynotify
![Page 10: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/10.jpg)
A Script
| person door dog |door := Door new close.dog := Dog new.dog door: door.person := Person new.person door: door; dog: dog.dog bark.
![Page 11: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/11.jpg)
Dooropened <Boolean>
open
opened := true.
self changed: #open
![Page 12: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/12.jpg)
Door
close
opened := false.
self changed: #close
isOpen
^opened
![Page 13: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/13.jpg)
DogcurrentState :: #sleeping, #waiting, #outside
bark
currentState := #waiting.
self changed: #bark
door: aDoor
door := aDoor.
door addDependent: self
![Page 14: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/14.jpg)
Dog
goOut
currentState := #outside.
door removeDependent: self.
self changed: #move
![Page 15: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/15.jpg)
Dogupdate: aSymbol
(currentState == #waiting) & (aSymbol == #open)
ifTrue: [self goOut ]
![Page 16: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/16.jpg)
Person
dog: aDog
dog := aDog.
dog addDependent: self
![Page 17: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/17.jpg)
Person
update: aSymbol
aSymbol == #bark
ifTrue: [door open].
aSymbol == #move
ifTrue: [door close]
![Page 18: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/18.jpg)
Watcher
instance variable: name <String>update: aSymbol
Transcript show: subjectName; show: ' '; show: aSymbol; cr
name: aStringname := aString
![Page 19: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/19.jpg)
| person door dog |door := Door new close.door addDependent:
(Watcher new name: 'door').dog := Dog new.door addDependent: dog.dog addDependent:
(Watcher new name: 'Fido').person := Person new.person door: door; dog: dog.dog bark.
![Page 20: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/20.jpg)
Improvements
Creating method
(have class method return initialized object)
Compose method
(Watcher on: door name: 'door')
(door watcherNamed: 'door')
![Page 21: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/21.jpg)
Model and Memory Management
Dog allInstancesreports a lot of dogs! Garbage
collection doesn't help.Object uses a global dictionary to store
dependents. Subject must "release" its observers/dependents.
![Page 22: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/22.jpg)
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.
![Page 23: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/23.jpg)
If you are not using Model then after the script says
dog bark.
add the messages
dog release.
door release.
person release
![Page 24: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/24.jpg)
Advantage of Observer Pattern
Easy to add new observers.
Coupling between observer and subject is abstract.
![Page 25: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/25.jpg)
Disadvantage of Observer Pattern
Often hard to understand relationships between objects in system.
Sometimes inefficient.
![Page 26: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/26.jpg)
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.
![Page 27: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/27.jpg)
Bird
update: aSymbol
(aSymbol == #open) &
(self isCaged not)
ifTrue: [self flyOut]
Script must now create a bird and make it depend on the door.
![Page 28: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects](https://reader036.vdocument.in/reader036/viewer/2022062308/56649f435503460f94c64393/html5/thumbnails/28.jpg)
Summary
Observer is an important patternUsed in a UI to make reusable componentsReduces coupling by increasing abstractionAbstraction makes programs easier to change, harder to understandOften requires a “script” to set up dependencies