开发者

Event Handling in Smalltalk(squeak)

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜