Design Patterns Java™ Workbook

(Michael S) #1
Chapter 9. Observer

When you decide to apply OBSERVER, you can write the registration and listening
mechanisms yourself, but Java includes two packages with classes that have these mechanics.
You can use the Observable class and the Observer interface in java.util.
Alternatively, you can use the PropertyChangeSupport class and
PropertyChangeListener in java.beans. The mechanics of these class/interface pairs
are similar. In particular, you can use either an Observable object or a
PropertyChangeSupport object when your interesting class needs to implement
OBSERVER through delegation instead of subclassing.


The value of OBSERVER remains the same whether you effect it through subclassing or
delegation and regardless of the classes you use to support it. OBSERVER lets clients know
when an interesting object changes, at the same time minimizing the responsibility of the
interesting object.

Free download pdf