Java Reference
In-Depth Information
Figure 2-1 shows the structure of the modified Observer pattern as it relates to the specific
classes within the Java libraries for event handling. The generic Subject participant in the
pattern manages a list (or lists) of generic Observer objects for each event that the subject can
generate. The Observer objects in the list must provide a specific interface through which the
Subject participant can notify them. When an event that the Observer objects are interested in
happens within the Subject participant, all the registered Observer objects are notified. In
the Java world, the specific interface for the Observer objects to implement must extend the
java.util.EventListener interface. The specific event the Subject participant must create
needs to extend the java.util.EventObject class.
Figure 2-1. The modified Observer pattern
To make this a little clearer, let's take a second look at the delegation-based event-handling
mechanism without all the design pattern terms. GUI components (and JavaBeans) manage
lists of listeners with a pair of methods for each listener type: add XXX Listener() and
remove XXX Listener() . When an event happens within the subject component, the component
notifies all registered listeners of the event. Any observer class interested in such an event
needs to register with the component an implementer of the appropriate interface. Then each
implementation is notified when the event happens. Figure 2-2 illustrates this sequence.
Note Some users like to call the event delegation model a publish-subscribe model, in which components
publish a set of available listeners for subscription, and others can subscribe to them.
 
Search WWH ::




Custom Search