Manish
Manish

Reputation: 1769

Event Handling in Smalltalk(squeak)

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.

Upvotes: 3

Views: 969

Answers (2)

Sebastian Sastre
Sebastian Sastre

Reputation: 2192

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.

Upvotes: 2

Randal Schwartz
Randal Schwartz

Reputation: 44220

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.

Upvotes: 8

Related Questions