Java Reference
In-Depth Information
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
O
BSERVER
through delegation instead of subclassing.
The value of O
BSERVER
remains the same whether you effect it through subclassing or
delegation and regardless of the classes you use to support it. O
BSERVER
lets clients know
when an interesting object changes, at the same time minimizing the responsibility of the
interesting object.