Java Reference
In-Depth Information
Ideally, we should manage the sketch data in a class designed specifically for that purpose - this class
will be the model for a sketch.
A class representing a view of the data in the model class will display the sketch and handle user
interactions - so this class will combine viewing functions and a sketch controller. The general GUI
creation and operations not specific to a view will be dealt with in the SketchFrame class. This is not
the only way of implementing the things we want in the Sketcher program, but it's quite a good way.
Our model object will contain a mixture of
text and graphics that will go to make up a
sketch. We'll call our model class
SketchModel and we'll call the class
representing a view of the model
SketchView , although we won't be adding
the view to the program until the next
chapter. The following diagram illustrates
the relationships between the classes we will
have in Sketcher.
The application object will have overall responsibility for managing links between the other objects
involved in the program. Any object that has access to the application object will be able to
communicate with any other object as long as the application class has methods to make each of the
objects available. Thus the application object will act as the communication channel between objects.
Note that SketchFrame is not the view class - it just defines the application window and the GUI
components associated with that. When we create a SketchView object in the next chapter, we'll
arrange to insert the SketchView object into the content pane of the SketchFrame object, and
manage it using the layout manager for the content pane. By defining the view class separately from the
application class, we separate the view of a sketch from the menus and other components we use to
interact with the program. One benefit of this is that the area in which we display the document has its
own coordinate system, independent of that of the application window.
To implement the foundations for the model/view design in Sketcher we need to define classes for the
model and the view, at least in outline. The class to contain the data defining a sketch we can define in
skeleton form as:
import java.util.Observable;
class SketchModel extends Observable {
// Detail of the rest of class to be filled in later...
}
We obviously have a bit more work to do on this class, so we will add to this as we go along. Since it extends
the Observable class, we will be able to register the view class with it as an observer, and automatically
notify the view of any changes. This facility will come into its own when we have multiple views. We can
define the view class as a component by deriving it from JComponent . This will build in all the methods for
operating as a component and we can override any of these as necessary. The view class also needs to
implement the Observer interface so that we can register it with the model. Here's the outline:
Search WWH ::




Custom Search