Java Reference
In-Depth Information
although you won't be adding the view to the program until the next chapter. Figure 19-1 illustrates the re-
lationships between the classes you have in Sketcher.
FIGURE 19-1
The application object has overall responsibility for managing links between the other objects involved in
the program. Any object that has access to the application object is 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
acts as the communication channel between objects.
Note that SketcherFrame is not the view class — it just defines the application window and the GUI
components associated with that. When you create a SketcherView object, you arrange to insert the
SketcherView object into the content pane of the SketcherFrame object and manage it using the layout
manager for the content pane. By defining the view class separately from the application class, you separate
the view of a sketch from the menus and other components that you use to interact with the program. One
benefit of this is that the area in which you display a sketch has its own coordinate system, independent of
that of the application window.
To implement the foundations for the model/view design in Sketcher, you need to define classes for the
model and the view, at least in outline. You can define in skeleton form the class to encapsulate the data that
defines a sketch:
import java.io.Serializable;
import java.util.Observable;
public class SketcherModel extends Observable implements Serializable {
// Detail of the rest of class to be filled in later...
private final static long serialVersionUID = 1001L;
}
This is going to be Serializable because you want to save a sketch to a file. You obviously have a bit
more work to do on this class to make it effective! You add to this as you go along. Because the Sketcher-
Model class extends the Observable class, you are able to register the view class with it as an observer and
automatically notify the view of any changes to the model. This facility comes into its own when you have
multiple views of a sketch.
 
 
Search WWH ::




Custom Search