28
Observer, Strategy, Decorator, Prototype,

Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects

Embed Size (px)

Citation preview

Page 1: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects

Observer, Strategy, Decorator, Prototype,

Page 2: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects

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

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

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

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

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

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

Collaborations

PersonDoor

openclose

Dog

bark listenwatch

Page 9: Observer, Strategy, Decorator, Prototype,. Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects

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

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

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

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

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

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

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

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

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

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

| 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

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

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

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

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

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

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

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

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

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