21.1. Serializing the Sketch
The Sketcher program can be considered to be a practical application only if you can save sketches in a file and retrieve them later—in other words, you need to implement serialization for a SketchModel object and use that to make the File menu work. Ideally, you want to be able to write the model for a sketch to a file and be able to read it back at a later date and reconstruct exactly the same model object. One obvious way to do this is to use serialization because the primary purpose of serialization is the accurate storage and retrieval of objects, so that's what you'll implement in this chapter. In Chapter 23 you'll explore another possibility—saving sketches in Extensible Markup Language (XML).
You've seen how to serialize objects back in Chapter 12. All you have to do to serialize a sketch document in our Sketcher program is to apply what you learned there. Of course, quite a few classes are involved in a sketch document but it will be remarkably easy considering the potential complexity of a sketch—I promise!
Putting in place the graphical user interface (GUI) functionality for saving a sketch on disk and reading it back from a file will be significantly more work than implementing serialization for the model. The logic of opening and saving files so as not to lose anything accidentally can get rather convoluted. Before you get into that, there is a more fundamental point I should address—a sketch doesn't have a name. You should at least make ...
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.