Java Reference
In-Depth Information
public BallisticsPanel_2(
BallisticsFunction func,
JSlider slider)
{
this.func = func;
this.slider = slider;
slider.addChangeListener(this);
}
When the slider changes, the BallisticsPanel_2 object is notified. The label recalculates
its tPeak value and repaints itself:
public void stateChanged(ChangeEvent e)
{
double val = slider.getValue();
double max = slider.getMaximum();
double min = slider.getMinimum();
tPeak = (val - min) / (max - min);
repaint();
}
A new problem emerges in this refactoring. The design adjusts responsibilities so that each
interested object registers for and reacts to changes in the slider. The distribution of
responsibility is good, but now each component that listens to the slider needs to recalculate
the value of tPeak . In particular, if you use a BallisticsLabel_2 class, as the solution
to Challenge 9.2 does, its stateChanged() method will be nearly identical to the
stateChanged() method of BallisticsPanel_2 . To consolidate this duplicated code,
we can refactor again, extracting an underlying domain object from the current design.
Model/View/Controller
As applications and systems grow, it is important to divide and redivide responsibility so that
classes and packages stay small enough to maintain. The phrase model/view/controller
( MVC ) refers to separating an interesting object—the model—from GUI elements that
portray it—the view and the controller. Java supports this separation of responsibility with its
O BSERVER mechanics.
The initial versions of the ShowBallistics application combine intelligence about an
application GUI with information about ballistics. You can refactor this code, following MVC
to divide this application's responsibilities. In this refactoring, the revised ShowBallistics
class should retain the views and controllers in its GUI elements.
The creators of MVC envisioned that the look of a component—its "view"—might be
separable from its feel—its "controller." In practice, the appearance of a GUI component and
its support for user interaction are tightly coupled, and Swing does not divide views from
controllers. The value of MVC is to push the "model" out of an application into its own
domain.
When you divide GUI objects from business objects, you can create layers of code. A layer
is a group of classes with similar responsibilities, often collected in a single Java package.
Higher layers, such as a GUI layer, usually depend only on classes in equal or lower layers.
Search WWH ::




Custom Search