Download An object-oriented library for event-driven design

Transcript
3
§2 EVENT LIBRARY ESSENTIALS
Class EVENT_TYPE is generic; the parameter TUPLE [INTEGER, INTEGER]
indicates that a mouse click produces event data consisting of two integers,
representing the mouse coordinates, collected into a two-element “tuple”.
Since mouse_click just represents an ordinary object — an instance of
class EVENT_TYPE — the instruction that creates it could appear anywhere.
One possibility, as shown, is to put it in a “once function” defining mouse_click.
A once function is executed the first time it’s called, whenever that is, the same
result being returned by every subsequent call. This language mechanism
addresses the issue of providing initialization operations without breaking the
decentralized architecture of well-designed O-O systems. Here it creates the
mouse click object when first needed, and retains it for the rest of the execution.
The scheme as described covers global events: the subscriber call /1/
subscribes your_procedure to any mouse click anywhere. Instead we may want to
let subscribers select events in a given graphical element such as a button. We simply
turn mouse_click into a feature of class BUTTON, so that subscriber calls will be
.
.
your_button mouse_click subscribe (agent your_procedure)
.
/5/
perhaps clearer as your_button_click subscribe (agent your_procedure), retaining
the original form /1/ with your_button_click set to your_button mouse_click.
What we have just seen defines, for the majority of applications, the
user’s manual of the Event Library:
•
On the publisher side, declare and create the event type object; trigger a
corresponding event, when desired, by calling publish.
•
On the subscriber side, call subscribe with an agent for the desired routine.
Only one class is involved, EVENT_TYPE; there is no need to define specific
classes for each event type (mouse click, mouse movement etc.) as, for
example, in the .NET model studied below — although you can do so if you
wish by introducing descendants of EVENT_TYPE that specialize the event
data. There is also no need for the publishers or the subscribers to inherit from
any particular classes, such as the abstract classes SUBJECT and OBSERVER
of the Observer Pattern, also studied below.
Section 7 will describe some of the more specialized features of the
library. As is often the case when the basic design of a library uses a small
number of abstractions tailored to the problem, it is possible to add specialpurpose facilities without disturbing users who need only the basics.
To understand the rationale behind this design, we will now step back to
examine the general issues of event-driven computation, and some previously
proposed solutions.
.