The standard implementataion of the observer pattern to be found in Smalltalk would be
the #changed
/#update
mechanism.
It goes like this:
subject addDependent: anObserver.
subject changed.
and then anObserver
gets send #update
:
MyObservingObject>>update
"I got called in a #changed chain"
super update.
self doUpdatingStuff
You can have finer control using #changed:
and #update:
(note the colon):
subject addDependent: anObserver.
subject changed: subject.
and
MyObservingObject>>update: anObject
"I got called in a #changed: chain"
super update: anObject.
self doUpdatingStuffWith: anObject
However, it is commonly found to use a symbol to indicate what changed:
subject addDependent: anObserver.
subject changed: #myNumbers.
and
MyObservingObject>>update: anObject
"I got called in a #changed: chain"
anObject == #myNumbers ifTrue: [
self doUpdatingStuffForNumbers.
^ self "inhibits the super"].
super update: anObject.
When you look at Squeak or Pharo, you'll find at least three other Observer implementations:
- The event handling for Morphic (see
Morph>>#on:send:to:
) - A similar, more general event handling mechanism, see
Object>>#when:send:to:
andObject>>#triggerEvent:
- the Announcements framework, encapsulating messages between subject and observer in classes.
You can find a comparison of these at the Signals project, another implementation, but inspired by Qt.