Baby MVC Lab

The Big Picture

MVC is a model-view-controller framework that supports diverse customizations, including BrickCAD (bc):

 

Framework Architecture

 Model-View-Controller

Notes

·       MVC uses the Model-View-Controller Architecture.

·       The (Java version of the) Publisher-Subscriber Pattern is used to notify views when the model has changed. (This is done by calling Model.changed.)

·       The Command Processor Pattern (aka Smart Commands Pattern) is used to allow customizers to easily add new commands to the system without needing to alter the controller.

·       The controller uses the Singleton Pattern to avoid confusion that might arise from multiple controllers.

·       This version of MVC does not support File Menu commands such as save, open, and new.

·       This version of MVC does not support View Menu commands that would allow users to open multiple views.

·       This version of MVC has a console user interface instead of a GUI.

·       This version of MVC does no error checking!

Application and Factory

Notes

·       MVCApp is the heart of the application. Its static start method uses a factory to create a model, a controller, and a default view, then calls repl.

·       MVCApp.repl is a read-execute-print loop. It prompt's the user for a command and handles the meta-commands such as quit, about, and help.

·       For application-specific commands (e.g., SetHeight, SetWidth, etc.) repl uses the factory to create the command, then asks the controller to execute the command.

·       Of course the controller simply asks the command to execute itself.

·       See Creation Patterns for Singleton and Abstract Factory

Brick CAD

Notes

·       The blue classes belong to the customization (bc).

·       The Brick setter methods call the inherited changed method to update the views.

·       SideView implements update by fetching the relevant dimensions from its model and displaying them.

·       Executing a SetHeight command calls the model's setHeight method, which updates the views:

·       For example, BrickFactory implements makeModel as:

return new Brick(20, 30, 40); // default initial dimensions

To piece it all together, bc needs a main method:

public class BC  {
  public static void main(String[] args) {
     MVCApp.start(new BrickFactory());
  }
}

Code

·       MVC.java

·       BC.java