19.1. Using the Model/View Architecture

You need to develop an idea of how you're going to manage the data for a sketch in the Sketcher program before you start drawing a sketch, because this will affect where and how you handle events. You already have a class that defines an application window, SketchFrame, but this class would not be a very sensible place to store the underlying data that defines a sketch. For one thing, you'll want to save a sketch in a file, and serialization is the easiest way to do that. If you're going to use serialization to store a sketch, you won't want all the stuff in the implementation of the SketchFrame class muddled up with the data relating to the sketch you have created.

For another thing, it will make the program easier to implement if you separate out the basic data defining a sketch from the definition of the GUI. This will be along the lines of the Model-View-Controller (MVC) architecture that I first mentioned in Chapter 17, a variant of which is used in the definition of Swing components. Ideally, you 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 ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.