The MVC Framework is contained in the MVC package. This is a reusable framework that implements the Model-View-Controller architecture in Java.
The MVC framework uses the following design patterns:
Model-View-Controller
Publisher-Subscriber
Command Processor
Memento
Abstract Factory
MVC supports multiple views of the model using Swing's Internal Frames.
The following diagram shows how BrickCAD classes (pink) extend MVC classes (yellow), which extend Java classes (green):
· The Model class is abstract because it does not implement the Memento functions.
· In MVC Memento is an empty interface:
interface Memento {}
· Model.changed sets the unsavedChanges flag to true and notifies observers. This method must be called each time a brick changes its height, width, or length.
· Not shown: TopView, FrontView, and DimensionsView.
The next diagram shows the MVC Command class and its relationships:
· If a command is undoable, then Command.execute() asks the model to create a memento, which the command saves.
· If a command is undoable, then Command.undo passes the saved memento to the model.accept. The model will use this memento to restore its fields to their values before the command was executed.
· Command is an abstract class because execute is only partially implemented.
· SetHeight.execute will call super.execute, prompt the user for a new height, then set the height of the model (cast as a Brick).
The Command Processor is a singleton object that manages undo and redo command stacks.
MVCApp is the heart of any MVC application. It maintains references to the model and command processor. It is also the application's main window with a menu bar containing File menu (Save, SaveAs, New, Open, Close, Exit), an Edit menu (undo, redo, and application-specific edit commands), a View menu (containing application-specific views), and a Help menu (help and about):
The Abstract Factory pattern is used to decouple how MVCApp works from how it is built.
The static run method uses its factory input to create an AppFactory and make it visible.
Utilities is a service (i.e., all methods are static). Some of the services it provides include error handling, user dialogs, saving and reading models, and menu construction.
Here's some partially implemented code to get started. (No guarantees.)
Here's a runnable version of brickCAD: brickCad.jar