Model View Controller Pattern

The MVC Pattern combines three patterns into a solution that separate the responsibilities in managing an interaction.

Bates and SierraHead First Design Patterns

Class diagram

/images/posts/design-patterns/mvc_pattern.png

  • View present the model usually in graphic interface.
  • Controller takes user input, interpreting them to change the model, translates input into actions on the model.
  • Model holds all the data, state and application logic, sends notifications of state changes to the observers.

According to the interaction shown

  1. The user does something.
  2. The controller gets the input (action and/or parameters), interprets them and figures out how to change the state of the model based on that action. The controller can also ask the view to change such as enable or disable certain buttons.
  3. The model handles all the application data and logic.
  4. The model notifies the view that its state has changed.
  5. The view gets the state of the model to update itself.

##Patterns

###Strategy Pattern Implemented by the view and the controller. It decouples the view from the model because the controller is responsible for interacting with the model to carry out user requests. The view doesn’t know how the request will be interpreted to change the model.

The view delegates to the controller to handle the user actions. The controller is the object that knows how to handle the user actions.

###Composite The view is a composite of GUI components, the top level contains other components which contain components and so on until the leaf.

###Observer The model is the observable and the view is the observer which registers with the model.