Sponsored Link •
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
Event, such as
Step 2. Define listener interfaces
java.util.EventListenerand contains a method declaration for each event (of that category) that will trigger an information propagation from the event generator to its listeners.
Eventin the event category class name. For example, the listener interface for
TelephoneEventthat was triggered by the phone ringing would be named
voidand 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)
Listenerin the listener interface name with
Adapter. For example, the adapter class for
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
TelephoneEventwould be named
voidin 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
Step 5. Define listener objects