|
| Events |
Introduction
In order to arrange several objects dependent from each other, you shouldn't use the old MVC Style and change over to the Events mechanism. Why? Methods like #changed and #update usually invoke too many unnecessary actions.
Attention
This Tutorial works in Squeak 3.4. Earlier versions have different interfaces. The principle is however the same.
Important Messages
- Object
- when:send:to:
- when:send:to:with:
- releaseActionMap
- triggerEvent:
- triggerEvent:with:
Example
Nehmen wir an, wir haben einen Lieferant, der Mehl transportiert und einen Bäcker, der erst backen kann wenn Mehl da ist. Da der Bäcker ein Workaholic ist, bäckt er immer, wenn er Mehl bekommt.
Let's say there is a supplier, that transports flour and a baker, who can only bake if he has flour. Since the baker is a Workaholic, he always bakes, if he gets flour.
| baker supplier |
baker := Baker new.
supplier := Supplier new.
baker when: #newFlour send: #bake to: self.
supplier transportFlourTo: baker.
baker triggerEvent: #newFlour.
Now one could say, that this can be done more simply as the supplier directly sends #bake to the baker. This is correct, but it would be one step towards optimizing. Assumed the baker still does further things when he gets new flour, the supplier shouldn't actually know anything of it. Thus we write:
| baker supplier |
baker := Baker new.
supplier := Supplier new.
baker
when: #newFlour send: #bake to: self;
when: #newFlour send: #cleanUpDepot to: self;
when: #newFlour send: #payBill to: self.
supplier transportFlourTo: baker
baker triggerEvent: #newFlour.
|
|
|