Java Reference
In-Depth Information
CHALLENGE 9.6
Complete the class diagram in Figure 9.7 to show
Tpeak
using
a
PropertyChangeSupport
object to manage listeners.
Figure 9.7. A
Tpeak
business object can delegate calls that affect listeners to
a
PropertyChangeSupport
object.
Whether you use
Observer
,
PropertyChangeSupport
, or another class to establish
the O
BSERVER
pattern, the point is to define a one-to-many dependency among objects. When
one object changes state, all its dependents are notified and updated automatically. This limits
the responsibility and eases maintenance of both interesting objects and their interested
observers.
Summary
The O
BSERVER
pattern appears most frequently in GUI applications and is a fundamental
aspect of Java's Swing architecture. With Swing, you never have to change or subclass
a component class just to communicate its events to an interested object. For small
applications, a common practice is to register a single object to receive all the events in
a GUI. Doing so has no inherent problem, but you should recognize that it reverses the
distribution of responsibility that O
BSERVER
intends. For a large GUI, consider letting each
interested object register for events rather than introducing a mediator.
O
BSERVER
lets you delineate the responsibility between business objects and a GUI, which
allows you to establish an MVC design. MVC lets you create loosely coupled layers than can
change independently and that may execute on different machines.