What is MVC (Model, View, Controller)?

blog image source
MVC (Model, View, Controller) is a pattern for organising code in an application to improve maintainability.

Imagine a photographer with his camera in a studio. A customer asks him to take a photo of a box. The box is the model, the photographer is the controller and the camera is the view. Because the box does not know about the camera or the photographer, it is completely independent. This separation allows the photographer to walk around the box and point the camera at any angle to get the shot/view that he wants.

Models; represent knowledge.
  • contains the logic of the application
  • responsible for managing application state
  • expresses solution to our problem in terms of domain model
  • this is where our OOP/domain modelling concepts come in
Views; a (visual) representation of its model.
  • responsible for providing a representation of the requested application state to the client
  • traditionally would be HTML pages, but can be any representation of the data
  • move towards decoupled UI and API’s mean that this is commonly JSON formatted data

Controllers; a link between a user and the system.
  • responsible for the HTTP request cycle
  • accepts input from HTTP request
  • interacts with the Model and View layers
  • sends HTTP response

Popular posts from this blog

Week 9 to 12 - Learning with MCR Codes

Week 8 - Session 1 - JavaScript in the Browser

Week 9 - Session 1 - The Internet, the Web, HTTP