Search code examples
observer-patternsmalltalkpharo

How do Announcements relate to the classic Smalltalk-80 dependent/change/update mechanism?


With the Announcements framework available in Pharo Smalltalk, is there still a reason to use the classic Smalltalk-80 dependent/change/update mechanism?


Solution

  • While the basic principle is the same, the Announcements framework is more flexible and IMHO more elegant. For example, you can define a hierarchy of Announcements and handle them pretty much as you handle exceptions. Assuming Ann2 is a subclass of Ann1, you can:

    anObject when: Ann1 do: [...]
    

    the block will capture both classes of announcements. Another cool thing is that you can define a handler for a set of announcements instead of a single one. Assuming that Ann3 and Ann4 are announcements, you can:

    anObject when: Ann3, Ann4 do: [...]
    

    and the block will be evaluated when any of those announcements are triggered. Finally, since your events are now objects, you can delegate behavior to them, which is really nice. There are a set of posts by Vassili Bykov on the topic that you may find interesting.

    Going back to the original question: the only "drawback" I can think of is that you have to go and define a class instead of just using a symbol. But honestly the effort is so low compared to what you gain with Announcements that for me is a no-brainer. I really can't think of any reason not to go with Announcements.

    HTH