Download An object-oriented library for event-driven design

Transcript
2
THE POWER OF ABSTRACTION, REUSE AND SIMPLICITY
2 EVENT LIBRARY ESSENTIALS
The Event Library consists at its core of one class, EVENT_TYPE with a
feature publish for publishers and a feature subscribe for subscribers. The
library is written in Eiffel; so are the usage examples in this section.
First the subscriber side. Assume you want to ensure that any future
mouse click will cause execution of a certain procedure of your application
your_procedure (a, b: INTEGER)
passing to it the mouse coordinates as values for a and b. To obtain this effect,
simply subscribe the desired procedure to the event type mouse_click:
.
mouse_click subscribe (agent your_procedure)
/1/
The argument to subscribe is an “agent” expression; an agent in Eiffel is an
object representing a routine, here your_procedure.
In most cases this is all one needs to know on the subscriber side, for
example to produce a graphical application using a GUI library. An advantage
of the scheme is that it lets you start from an existing system and add an eventdriven scheme such as a GUI without writing any connecting code. You’ll just
reuse the existing routines directly, linking them to event types through agent
expressions as above. This extends to routines with extra arguments: assuming
other_procedure ( text: STRING; a, b: INTEGER; date: DATE )
you can still subscribe the procedure without any “glue code” through
.
mouse_click subscribe (agent other_procedure ("TEXT", ?, ?, Today)
/2/
where the question marks indicate the values to be filled in by the event. (The
agent in form /1/ can be written more explicitly as agent your_procedure (?, ?).)
So much for subscribers. The basic scheme for publishers is also
straightforward. To trigger a mouse click event, all the GUI library will do is
.
mouse_click publish ([x_position, y_position])
/3/
It is also the publisher’s responsibility to declare mouse_click and create the
corresponding object. It can take care of both through
mouse_click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]] is
once
create Result
end
/4/