Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 ยท Testing and Patterns

Announcements and Observers

Learn the Pharo Announcements framework: Announcer, Announcement classes, subscriptions, unsubscribing and weak references, and how it replaces the older dependents mechanism. Runnable JavaScript stand-in included.

When something happens in one object, other objects often need to know: a model changes, a button is pressed, a download finishes. The Observer pattern decouples them. The subject does not know who listens. In Pharo the standard framework for this is Announcements. A subject owns an Announcer, sends it Announcement objects, and subscribers register blocks for the kinds of announcement they care about.

This lesson shows the real API (read-only, since Pharo is not available here) and then builds a small announcer in JavaScript that you can run, change and test.

The parts

  • An Announcement is an object describing what happened. You create a subclass per kind of event and give it the data it carries.
  • An Announcer keeps subscriptions and delivers announcements to them.
  • A subscription pairs an announcement class (matched by isKindOf:) with a block and an optional subscriber.
SMALLTALK
Announcement subclass: #BalanceChanged
    instanceVariableNames: 'oldBalance newBalance'
    package: 'Bank'.

BalanceChanged class >> from: anOld to: aNew
    ^ self new setOld: anOld new: aNew

Object subclass: #Account
    instanceVariableNames: 'balance announcer'
    package: 'Bank'.

Account >> announcer
    ^ announcer ifNil: [ announcer := Announcer new ]

Account >> deposit: amount
    | old |
    old := balance.
    balance := balance + amount.
    self announcer announce: (BalanceChanged from: old to: balance)

A subscriber registers with when:do::

SMALLTALK
account announcer
    when: BalanceChanged
    do: [ :ann | Transcript show: 'now ', ann newBalance printString; cr ].

when:do: matches announcements that are instances of BalanceChanged or of its subclasses. That means you can subscribe to a broad class (AccountEvent) and receive every kind below it. Use when:send:to: to call a method on an object, and unsubscribe: to remove all subscriptions of a subscriber:

SMALLTALK
account announcer when: BalanceChanged send: #balanceChanged: to: self.
account announcer unsubscribe: self.

Why announcements beat the older dependents mechanism

Classic Smalltalk put changed: and update: in Object, so any object could be a dependent. It worked, but one update: method received every kind of change and had to test the argument. Announcements give each event its own class and each subscriber its own block, so there is no big conditional, the event carries typed data, and you can search the system for every sender and subscriber of an event class.

Delivery rules

  • Delivery is synchronous by default: announce: returns after all blocks ran, in subscription order.
  • An exception in a subscriber propagates to the announcer, so keep subscribers small and safe, or wrap them.
  • Subscriptions are weak when you use weak variants (for example weak when:do:), so a forgotten subscriber can be garbage collected. A subscription holding a strong block reference may keep objects alive; always unsubscribe views when they close.
  • Announcing from inside a subscriber is allowed but can create loops. Guard against re-entrancy.

A runnable announcer

The following JavaScript mirrors the design: Announcement classes, an Announcer with when, announce and unsubscribe, and kind-of matching with instanceof.

JS Stand-in: an Announcer

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The audit subscriber registered for the base class AccountEvent, so it hears every event below it. After unsubscribe(view), only the audit line prints.

Using it in an interface

A typical model/view split uses announcements as the glue. The model never imports the view, and the view subscribes when it opens and unsubscribes when it closes.

JS Stand-in: a model and a view

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The same pattern shows up in the GUI tools of Morphic and Spec and in testing code: you can subscribe a test to an announcer and assert that a particular event was sent, as in SUnit.

flowchart LR
  M["Account<br/>subject"] -->|"announce: BalanceChanged"| A["Announcer"]
  A -->|"isKindOf: match"| V["View block"]
  A -->|"isKindOf: match"| L["Audit block"]
  V -. "unsubscribe on close" .-> A

The subject only knows the announcer. Subscribers come and go.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does an Announcer decide which subscribers receive an announcement?

    A subscription to a class also receives instances of its subclasses.
  2. 2. Why are announcements preferred over update: in the dependents mechanism?

    Typed events and per-event blocks remove the update: switch and are searchable.
  3. 3. What should a view do when it closes?

    Otherwise the announcer keeps the view alive and may still call it.
  4. 4. Is announce: asynchronous by default?

    Delivery is synchronous in subscription order.

Summary

  • An Announcer delivers Announcement objects to subscribers registered with when:do: or when:send:to:.
  • Subscriptions match by class hierarchy, so you can listen to a family of events.
  • Delivery is synchronous; unsubscribe views when they close to avoid leaks and ghost updates.
  • Announcements replace the generic changed:/update: mechanism with typed events.
  • The Smalltalk code is read-only; the announcer is a JavaScript stand-in.

Next: Seaside: Web Components and Callbacks