Fall 2005»CSCI 380 MVC Intro

CSCI 380 MVC Intro

MVC (Model-View-Controller) Intro

Model : The core of the application. This maintains the state and data that the application represents. When significant changes occur in the model, it updates all of its views

  • If model is active, it should run in a separate thread (see HeartModel.java).
  • If model is passive, it should call updateObservers() within each set (transformer) method.
  • For example, see HeartModel.java.

Controller : The user interface presented to the user to manipulate the application.

  • The model object should be passed as parameter to the controller's constructor.
  • The controller, within its event handlers, should call model's set methods.
  • For example, see HeartController.java.

View : The user interface which displays information about the model to the user. Any object that needs information about the model needs to be a registered view with the model.

  • The model object should be passed as parameter to the view's constructor.
  • The view constructor should register itself (this) as an observer by calling the model's addObserver() method.
  • The view should define update()
    • This method will be called by model anytime the model's state has changed.
    • Thus, this method should update the output view widgets accordingly.
  • For example, see HeartView.java.

In Java, everything is "glued" together by a system driver class. This class creates the main GUI window, and instantiates the Model, Controller, and View objects. For example, see Heart.java.

File Heart.jar runs the above example.


Cristobal Baray's intro on the model-view-controller (MVC) design pattern.