3

How can I create my own events in Smalltalk? I am basically looking for some software events that can be triggered when some particular event happens.

2 Answers 2

8

In classic Smalltalk (supported by Squeak as a direct derivative of the original XEROX Parc image), you have a basic publish/subscribe model. Look for "event" in the protocol browser for Object instances.

In modern Smalltalk, you can use catch/throw user-defined events using the Exception class and its subclasses. See the class documentation for Exception for details.

You can also select the Announcements framework, available in Squeaksource, as a more flexible version of the classic event framework.

2

To recap, events are based on the Observer Pattern where a subject has dependents observing selected events on it.

This creates a relationship of loose coupling among them.

In Squeak or Pharo in some method that knows both, the subject and the observer, you'd do it like this:

Observation

elevatorPanel when: #openDoorClicked send: #onOpenDoorClicked to: elevator

Event Triggering

On the other hand, with self being elevatorPanel:

self triggerEvent: #openDoorClicked

And you'll have elevator receiving the onOpenDoorClicked message.

Similarly, you can do it with arguments:

elevatorPanel when: #floorSelected: send: #onFloorSelected: to: elevator with: floorNumber

In which case you can trigger in two ways, first

self triggerEvent: #floorSelected:

Wich will make the elevator observer instance to receive the onFloorSelected: message with floorNumber as the argument.

And second, overriding that value at the triggering time

self triggerEvent: #floorSelected: with: aFresherFloorValue

In which case you'll also have elevator receiving the onFloorSelected: but with aFresherFloorValue instead of the floorValue captured during the observation setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.