|
|
|
Advertisement
|
The solution
The solution is to implement an event delegation mechanism between the
information provider (the event generator) and the
recipients (the listeners).
Here's a step-by-step outline of Java's idiomatic solution to this problem:
Step 1. Define event category classes
java.util.EventObject.
Event, such as
TelephoneEvent.
Step 2. Define listener interfaces
java.util.EventListener and contains a method declaration
for each event (of that category) that will trigger an information
propagation from the event generator to its listeners.
Listener for Event
in the event category class name. For example, the listener interface
for TelephoneEvent would be TelephoneListener.
TelephoneEvent
that was triggered by the phone ringing would be named telephoneRang().
void and take one parameter,
a reference to an instance of the appropriate event category class. For
example, the full signature of the telephoneRang() method
would be:
void telephoneRang(TelephoneEvent e);
Step 3. Define adapter classes (optional)
Listener in the listener
interface name with Adapter. For example, the adapter class
for TelephoneListener would be TelephoneAdapter.
Step 4. Define the observable class
add<listener-interface-name>() and the remove method remove<listener-interface-name> (). For example, the listener add and remove
methods for a TelephoneEvent would be named addTelephoneListener() and
removeTelephoneListener().
void in the event generator's class that fires
(propagates) the event.
fire<listener-method-name>. For example, the name of the event propagator method for the event propagated via the telephoneRang() method of TelephoneListener would be fireTelephoneRang().
Step 5. Define listener objects
|
Sponsored Links
|