Java Reference
In-Depth Information
Using Property Change Listeners As Observers
Besides the basic event-delegation mechanism, the JavaBeans framework introduced yet
another incarnation of the Observer design pattern, this time through the property change
listener. The PropertyChangeListener implementation is a truer representation of the Observer
pattern. Each Observer watches for changes to an attribute of the Subject. The Observer is then
notified of the new state when changed in the Subject. Figure 2-4 shows the structure of this
Observer pattern as it relates to the specific classes within the JavaBeans libraries for property
change handling. In this particular case, the observable Subject has a set of add/remove prop-
erty change listener methods and a property (or properties) whose state is being watched.
Figure 2-4. The property change listener Observer pattern
With a PropertyChangeListener , the registered set of listeners is managed within the
PropertyChangeSupport class. When the watched property value changes, this support class
notifies any registered listeners of the new and old property state values.
Note Although PropertyChangeListener observers are registered at the class level, not all properties
of the class might be bound. A property is bound when a change to the property causes the registered
listeners to be notified. In addition, although the JavaBeans framework introduced the concept of property
change listeners in JDK 1.1, none of the properties of the AWT components were bound, although this changed
for the Component class in the 1.2 release. The Swing components have many of their properties bound. To
find out which ones are bound, see the property tables for each Swing component that appear in later chap-
ters of this topic.
 
Search WWH ::




Custom Search