[英]Event Handling in Smalltalk(squeak)
How can I create my own events in Smalltalk?如何在 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.在经典的 Smalltalk(由 Squeak 作为原始 XEROX Parc 图像的直接衍生版本支持)中,您有一个基本的发布/订阅模型。 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.在现代 Smalltalk 中,您可以使用 Exception 类及其子类来使用 catch/throw 用户定义的事件。 See the class documentation for Exception for details.
有关详细信息,请参阅 Exception 的类文档。
You can also select the Announcements framework, available in Squeaksource, as a more flexible version of the classic event framework.您还可以选择 Squeaksource 中提供的公告框架,作为经典事件框架的更灵活版本。
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:在 Squeak 或 Pharo 中,以某种同时了解主体和观察者的方法,你会这样做:
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.您将让
elevator
接收onOpenDoorClicked
消息。
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. Wich 将使
elevator
观察者实例接收以floorNumber
作为参数的onFloorSelected:
消息。
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.在这种情况下,您还将让
elevator
接收onFloorSelected:
但使用一个aFresherFloorValue
而不是在观察设置期间捕获的floorValue
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.