Java Reference
In-Depth Information
The notifyObservers() methods calls the update() method of each registered
observer. The update() method is a requirement for implementers of the Observer
interface, as Figure 9.4 shows.
Figure 9.4. A BallisticsLabel is an Observer ; it can register its interest in
an Observable object so that the label's update() method is called when
the Observable object changes.
A BallisticsLabel object need not retain a reference to the Tpeak object it observes.
Rather, the BallisticsLabel constructor can register for updates when the Tpeak object
changes. The label's update() method will receive the Tpeak object as its Observable
argument. The method can cast the argument to Tpeak , extract the new value, change
the label's text, and repaint.
CHALLENGE 9.3
Write the complete code for BallisticsLabel.java .
The new design of the ballistics application separates a business object from the GUI
elements that represent it. Two requirements must be met for this design to work.
1. Implementations of Observer must register their interest and must update
themselves appropriately, often including repainting themselves.
2. Subclasses of Observable must remember to notify observers when their values
change.
These two steps set up most of the wiring you need across layers in the ballistics application.
You also need to arrange for a Tpeak object to change when the application's slider changes.
You can achieve this without writing a class, by instantiating an anonymous subclass of
ChangeListener .
Search WWH ::




Custom Search