Wednesday, March 20, 2013

Model View Controller

 

History of Model View Controller (MVC):

MVC is really old and was first described in 1979 by Trygve Reenskaug, then working on Smalltalk at Xerox PARC. Being over 30 years ago does not mean it's dead, far from it... Today MVC is used in most modern web and GUI frameworks, including:

  1. Ruby On Rails: A popular Ruby web-framework
  2. Apple Cocoa: Apple's framework for developing Mac OS and iOS applications
  3. ASP.Net Framework: Microsoft's web-framework for implementing web applications
  4. Apache Struts: A popular Java web-framework
  5. And many many more languages and frameworks 

The main objectives of MVC:

Model-View-Controller as the name applies considers three roles:
  • Model: manages the behavior and data of the application domain
  • View: represents the display of the model in the UI
  • Controller: takes user input, manipulates the model and causes the view to update
There isn't a standard definition of the MVC pattern and many frameworks use a slightly different version. This said there is a core idea of what MVC tries to achieve:
  • Separating the presentation from the model:
    Enables implementation of different UIs and better testability
  • Separating the controller from the view:
    Most useful with web interfaces and not commonly used in GUI frameworks


Smalltalk MVC:

The first implementation of the MVC pattern was found in Smalltalk-80 - a programming language released in 1980. Smalltalk is a pure object oriented programming language that's very interactive and MVC was one of the core features of the implementation.
The main objective of using MVC in Smalltalk was separating the application logic from the user interface - - enabling reuse of the model to implement several user interfaces: 

Ruby On Rails MVC:

Ruby on Rails is a very popular Ruby web-framework that uses MVC.
The client-server environment of web-applications makes things more complicated than the original environment that MVC was invented in. This is mostly because the view can't easily observe the model (at least not without Ajax or Comet communication).

Overview of Rails MVC: 

Cocoa MVC:

Apple's Cocoa framework is used to create Mac OS X and iOS applications. Cocoa uses a lot of design patterns and MVC is one them.
Cocoa's MVC puts a lot importance on the controller and this makes it a lot different than the original Smalltalk MVC. A Cocoa controller acts as a mediator between the view and the model [this is commonly known from the Mediator pattern]: 



ASP.Net MVC framework:

ASP.Net started off using a pattern called Model-View-Presenter, but this pattern got "replaced" when Microsoft introduced ASP.Net MVC, which implements a similar MVC pattern used in Rails or Django.