Model View Controller Architecture

Independent elements:

Model

  • state data for each component
  • different data for different models

View

  • how the component looks onscreen

Controller

  • dictates how the component reacts to events

example

Model:

Minimum=0 Maximum=100 Value=0

View:

Controller:

-accept mouse click on end buttons -accept mouse drag on thumb

 

Java's version of MVC:

 

  • Swing uses the model-delegate design, a similar architecture to MVC.

  • The View and Controller elements are combined into the UI delegate since Java handles most events in the AWT anyway.
  • Multiple views can be used with a single model.
  • Changes to a single Model can affect different views.
  • Components can share a model (JScrollbar and JSlider share the BoundedRangeModel).

     

 

Most components provide the model-defined API directly in the component class. The component can be manipulated without interacting with the model at all.

 

//example of method in JSlider class

public int getValue() {
return getModel().getValue(); }

//so we can use the following and avoid the model

JSlider slider = new JSlider();

int value = slider.getValue();

 

 

 

 

 

 

 

 

 

© Lynne Grewe