To use the MVC Framework, developers create a customization that extends the framework:
Here's a simplified version of the framework's design:
The framework uses the following patterns:
Model-View-Controller
Model = Model
View = View
Controller = Command Processor
Publisher-Subscriber
Publisher = Observable
Subscriber = Observer
Concrete Publisher = Model
Concrete Observer = View
Command Processor
Command Processor = Command Processor
Command = Command
Memento
Caretaker = Command
Memento = Memento
Originator = Model
Here are a few details of the View class design:
Views are JPanels. They have two menus: File (New, Save, SaveAs, Open, Exit) and Edit (Undo, Redo, etc.). There are two inner classes called FileController and EditController that listen to these menus.
The View also instantiates the Abstract Factory pattern:
Abstract Products = Commands & Model
Abstract Factory = View
Programmers will provide the concrete products and factory in the customization (see below).
Here's a screen shot of the Stoplight Simulator:
Under the Edit menu is an additional item called change. Repeatedly selecting this item causes the lights to change from red, to green, to yellow, and back to red again.
Here's the design of the simulator:
Perhaps the trickiest part is the StopLightView class:
public class StopLightView extends View {
public
StopLightView(StopLight model) {
super(model);
title
= "Stop Light Simulation";
windowHeight
= 300;
windowWidth
= 300;
JMenuItem changeItem = new JMenuItem("Change");
changeItem.addActionListener(ec);
editMenu.add(changeItem);
}
public
void paintComponent(Graphics gc) {
StopLight model = (StopLight)theModel;
StopLight.State state =
model.getState();
if
(state.equals(StopLight.State.STOP))
{
gc.setColor(Color.RED);
} else
{
gc.setColor(Color.BLACK);
}
gc.fillOval(10, 10, 10, 10);
if
(state.equals(StopLight.State.SLOW))
{
gc.setColor(Color.YELLOW);
} else
{
gc.setColor(Color.BLACK);
}
gc.fillOval(10, 30, 10, 10);
if
(state.equals(StopLight.State.GO)) {
gc.setColor(Color.GREEN);
} else
{
gc.setColor(Color.BLACK);
}
gc.fillOval(10, 50, 10, 10);
}
// three oveerrides:
public
Model makeModel() {
return
new StopLight();
}
public
Command makeCommand(String cmmd) {
System.out.println("COMMAND = " + cmmd);
if
(cmmd.equals("Change")) {
return
new ChangeCommand((StopLight)theModel);
} else
{
return
null;
}
}
public
void update(Observable arg0, Object
arg1) {
repaint();
}
public
static void
main(String[] args) {
// TODO
Auto-generated method stub
StopLightView gui = new StopLightView(new
StopLight());
gui.display();
}
}
Notes:
1. This class is the main class (i.e., contains main).
2. This class overrides paintComponent, inherited from JPanel. This simply draws the stop light using information from the stoplight model.
3. This class overrides the update method. The call to repaint() forces the OS to call paintComponent.
4. This class is a concrete factory overriding factory methods that make needed components: model and commands. In this case the model is an instance of StopLight. The ChangeCommand is the concrete product manufactured by makeCommand. Note that this method can produce many types of commands, depending on the input.
5. The constructor makes an additional menu item, then adds it to the edit menu.
Study the code below for more details on how this framework is designed, implemented, and used.
mvc.zip (An Eclipse project)