Java Reference
In-Depth Information
Layering usually includes a clear definition of the interfaces between layers, such as a GUI
and the business, or domain, objects it represents. This opens the possibility of arranging for
different layers to execute on different computers. A layer that executes on a computer is a
tier in an n-tier system. You can reorganize the responsibilities of the ShowBallistics
code, achieving a layered system, as Figure 9.3 shows.
Figure 9.3. By creating an observable Tpeak class you can separate a business logic layer
from a GUI layer.
The design that Figure 9.3 illustrates creates a Tpeak class to model the t peak value,
the critical value in the ballistics equations that the application displays.
The BallisticsPanel and BallisticsLabel classes depend on Tpeak . Rather than
making a Tpeak object responsible for updating GUI elements, the design applies O BSERVER
so that interested objects can register for notification when Tpeak changes. The Java class
libraries supports this, providing an Observable class and an Observer interface in
the java.util package. The Tpeak class can subclass Observable and can update its
observers when its value changes.
public void setValue(double value)
{
this.value = value;
setChanged();
notifyObservers();
}
Note that you have to call setChanged() so that the Observable code will broadcast
the change. (It is questionable whether a design that requires two steps to notify observers is
ideal.)
Search WWH ::




Custom Search