observer, strategy, decorator, prototype,

28
Observer, Strategy, Decorator, Prototype,

Upload: branxton

Post on 06-Jan-2016

32 views

Category:

Documents


3 download

DESCRIPTION

Observer, Strategy, Decorator, Prototype,. Observer Pattern. Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects propagate information without depending on each other much. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Observer, Strategy, Decorator, Prototype,

Observer, Strategy, Decorator, Prototype,

Page 2: Observer, Strategy, Decorator, Prototype,

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

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:

Registrationsubject addDependent: observer

Notificationself changed. self changed: #value

Updatedefine update: aSymbol

Page 5: Observer, Strategy, Decorator, Prototype,

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,

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,

Basic classes

Dog

bark / move

Person

Door

open / close / isOpen

Object

addDependent:

changed:

update:

Page 8: Observer, Strategy, Decorator, Prototype,

Collaborations

PersonDoor

openclose

Dog

bark listenwatch

Page 9: Observer, Strategy, Decorator, Prototype,

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,

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,

Dooropened <Boolean>

open

opened := true.

self changed: #open

Page 12: Observer, Strategy, Decorator, Prototype,

Door

close

opened := false.

self changed: #close

isOpen

^opened

Page 13: Observer, Strategy, Decorator, Prototype,

DogcurrentState :: #sleeping, #waiting, #outside

bark

currentState := #waiting.

self changed: #bark

door: aDoor

door := aDoor.

door addDependent: self

Page 14: Observer, Strategy, Decorator, Prototype,

Dog

goOut

currentState := #outside.

door removeDependent: self.

self changed: #move

Page 15: Observer, Strategy, Decorator, Prototype,

Dogupdate: aSymbol

(currentState == #waiting) & (aSymbol == #open)

ifTrue: [self goOut ]

Page 16: Observer, Strategy, Decorator, Prototype,

Person

dog: aDog

dog := aDog.

dog addDependent: self

Page 17: Observer, Strategy, Decorator, Prototype,

Person

update: aSymbol

aSymbol == #bark

ifTrue: [door open].

aSymbol == #move

ifTrue: [door close]

Page 18: Observer, Strategy, Decorator, Prototype,

Watcher

instance variable: name <String>update: aSymbol

Transcript show: subjectName; show: ' '; show: aSymbol; cr

name: aStringname := aString

Page 19: Observer, Strategy, Decorator, Prototype,

| 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,

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,

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,

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,

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,

Advantage of Observer Pattern

Easy to add new observers.

Coupling between observer and subject is abstract.

Page 25: Observer, Strategy, Decorator, Prototype,

Disadvantage of Observer Pattern

Often hard to understand relationships between objects in system.

Sometimes inefficient.

Page 26: Observer, Strategy, Decorator, Prototype,

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,

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,

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