Model view controller

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Marina)
 
(37 intermediate revisions by 8 users not shown)
Line 1: Line 1:
The Model View Controller (MVC) is one of the oldest architectural patterns. It is still widely used in web and application programming.
+
The '''Model View Controller (MVC)''' is one of the oldest architectural patterns. It is still widely used in web and application programming. It was initially developed by [[Trygve Reenskaug]] in 1979. In order to complete this pattern, he utilised his knowledge of control and command strategies and the practices of Norwegian shipping yards.
  
The general premise is to separate application into three distinct parts:
+
The general premise is to separate an application or architectural area into three distinct parts:
* '''Model:''' The underlying model of the data, this is often provided by a back-end database.
+
* '''Model:''' This is usually considered to be related to a domain model of the application, possibly including persistence. It will often include behaviour as well as data.
* '''View:''' The interface with the web client/browser or the application's GUI.
+
* '''View:''' The interface with the web client/browser or the application's GUI. This part may also be used to provide internal details to other parts of the program and does not need to be directly related to the end user.
* '''Controller:''' The logic (sometimes called 'business logic') that interfaces between the view and the model.
+
* '''Controller:''' This is the most disputed component, it can either act as a [[Mediator]], [[Strategy]] or business logic.
  
The MVC is often considered circular in form, with the model providing data for the view, which uses the controller to update the model. However, in some situations the controller is thought to sit between the view and the model.
+
While the components are ''relatively'' undisputed, the connections between them are more important and more conflicting. Below the known varieties of the MVC pattern are described. Controversy exists on whether the MVC should be considered an OO design pattern at all.
 +
 
 +
An important source of discussion and debate on this pattern is [[Ward's wiki]], and this information can be found [http://www.c2.com/cgi/wiki?ModelViewController here].
 +
 
 +
==Usage ==
 +
Given the variety of patterns with the name 'MVC', it would seem that the term has lost explanatory potential. Maybe the term should be avoided to limit misunderstanding.
 +
==Purpose ==
 +
Each of the different patterns that can be thought of as MVC have subtly different effects and purpose.
 +
 
 +
== Patterns ==
 +
So, lets consider the variations of MVC.
 +
 
 +
=== MVC-Observer-Strategy ===
 +
[[Image:MVCDiagram.png|none|frame|Diagram from lecture 27/7/09]]
 +
This version is the original MVC pattern proposed by [[Trygve Reenskaug]] in 1979 and popularised with its use with [[Smalltalk]]. It is also Sun's proposed structure of [http://java.sun.com/developer/technicalArticles/J2EE/despat/ MVC in J2EE].
 +
* '''Model''' and '''View''' resemble an [[Observer]].
 +
* The '''Controller''' is a [[Strategy]].
 +
 
 +
=== MVC-Mediator ===
 +
[[Image:Mvc-mediator.png|none|frame|Diagram from lecture 27/7/09]]
 +
This is the pattern that the developers of Apple's Cocoa framework believe is MVC, they even made a [http://youtube.com/watch?v=YYvOGPMLVDo song] about it.
 +
* '''Controller''' resembles the [[Mediator]] between '''Model''' and '''View'''
 +
* The purpose of the Controller, in this case, is to ensure smooth communication and validation between the '''Model''' and '''View'''. The controller takes user input, manupulates the '''Model''' and gets '''View''' to update appropriately.
 +
* The '''View''' represents the display of the model in the UI.
 +
 
 +
=== MVC as an Input-Output Process ===
 +
[[Image:Mvc-as-input-output.png|none|frame|Diagram from lecture 27/7/09]]
 +
 
 +
The diagram above shows how MVC can be viewed as a slight deviation from the OO paradigm.  This is one way to argue that MVC is not OO [http://www.c2.com/cgi/wiki?MvcIsNotObjectOriented].
 +
 
 +
=== MVC as a Distributed Architecture ===
 +
[[Image:DistributedMVC.png|right|frame|Distributed MVC]]
 +
This pattern is often used by those in web application development and is often know as MVC.
 +
 
 +
* '''View''': The graphics/markup coding that provides a user interface to a remote user (i.e. in a web browser)
 +
* '''Controller''': The systems business logic. A distinct layer between the Model and the View. Provides a handling of user state, if necessary, and any behavioural modifications required to the persistent model.
 +
* '''Model''': A set of data objects that are responsible for the underlying persistence of a system. Usually these classes form the interface to an underlying DB.
 +
 
 +
Each of these layers has the potential to be separated in a physical (and logical) fashion. Multiple versions of each layer can run on different machines with heartbeats between allowing the state to be maintained yet providing redundancy and the possibility of large scale usage.
 +
 
 +
=== MVC as a Round Trip Pattern ===
 +
This version of the pattern calls for a distinct separation of the three elements in a round trip manner.
 +
* '''View''': Displays information to the user. Updates to the view directly update the model.
 +
* '''Controller''': Controls the information sent to the user by manipulating the view.
 +
* '''Model''': The model provides the underlying domain structure. It tells the controller when and how it changes.
 +
 
 +
==External links==
 +
* [http://c2.com/cgi/wiki?ModelViewController Model View Controller] - What [[Ward's wiki]] has to say about the MVC
 +
** [http://www.c2.com/cgi/wiki?WhatsaControllerAnyway What's a Controller anyway?] has loads of interesting history.  Note the comment by [[Ralph Johnson]] about half way down.
 +
* [http://c2.com/cgi/wiki?MvcIsNotObjectOriented MVC's OO Nature] - A discussion on [[Ward's wiki]] as to whether the MVC is appropriate or even valid for OOD.
 +
* [http://www.martinfowler.com/eaaDev/uiArchs.html GUI Architectures] by [[Martin Fowler]]
 +
* [http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx MVC & MVP Article]
 +
* [http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller Wikipedia Article]
  
 
==See also==
 
==See also==
 +
*[[Architectural patterns]]
 
*[[Presentation separation idiom]]
 
*[[Presentation separation idiom]]
 +
*[[Observer]]
 +
* [http://youtube.com/watch?v=YYvOGPMLVDo The MVC song]
 +
* [http://heim.ifi.uio.no/~trygver/1979/mvc-1/1979-05-MVC.pdf MVC Paper] - The original paper describing the MVC Pattern - [[Trygve Reenskaug]] 1979. More details of Trygve's involvement can be found [http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html here].

Latest revision as of 03:17, 25 November 2010

The Model View Controller (MVC) is one of the oldest architectural patterns. It is still widely used in web and application programming. It was initially developed by Trygve Reenskaug in 1979. In order to complete this pattern, he utilised his knowledge of control and command strategies and the practices of Norwegian shipping yards.

The general premise is to separate an application or architectural area into three distinct parts:

  • Model: This is usually considered to be related to a domain model of the application, possibly including persistence. It will often include behaviour as well as data.
  • View: The interface with the web client/browser or the application's GUI. This part may also be used to provide internal details to other parts of the program and does not need to be directly related to the end user.
  • Controller: This is the most disputed component, it can either act as a Mediator, Strategy or business logic.

While the components are relatively undisputed, the connections between them are more important and more conflicting. Below the known varieties of the MVC pattern are described. Controversy exists on whether the MVC should be considered an OO design pattern at all.

An important source of discussion and debate on this pattern is Ward's wiki, and this information can be found here.

Contents

Usage

Given the variety of patterns with the name 'MVC', it would seem that the term has lost explanatory potential. Maybe the term should be avoided to limit misunderstanding.

Purpose

Each of the different patterns that can be thought of as MVC have subtly different effects and purpose.

Patterns

So, lets consider the variations of MVC.

MVC-Observer-Strategy

Diagram from lecture 27/7/09

This version is the original MVC pattern proposed by Trygve Reenskaug in 1979 and popularised with its use with Smalltalk. It is also Sun's proposed structure of MVC in J2EE.

MVC-Mediator

Diagram from lecture 27/7/09

This is the pattern that the developers of Apple's Cocoa framework believe is MVC, they even made a song about it.

  • Controller resembles the Mediator between Model and View
  • The purpose of the Controller, in this case, is to ensure smooth communication and validation between the Model and View. The controller takes user input, manupulates the Model and gets View to update appropriately.
  • The View represents the display of the model in the UI.

MVC as an Input-Output Process

Diagram from lecture 27/7/09

The diagram above shows how MVC can be viewed as a slight deviation from the OO paradigm. This is one way to argue that MVC is not OO [1].

MVC as a Distributed Architecture

Distributed MVC

This pattern is often used by those in web application development and is often know as MVC.

  • View: The graphics/markup coding that provides a user interface to a remote user (i.e. in a web browser)
  • Controller: The systems business logic. A distinct layer between the Model and the View. Provides a handling of user state, if necessary, and any behavioural modifications required to the persistent model.
  • Model: A set of data objects that are responsible for the underlying persistence of a system. Usually these classes form the interface to an underlying DB.

Each of these layers has the potential to be separated in a physical (and logical) fashion. Multiple versions of each layer can run on different machines with heartbeats between allowing the state to be maintained yet providing redundancy and the possibility of large scale usage.

MVC as a Round Trip Pattern

This version of the pattern calls for a distinct separation of the three elements in a round trip manner.

  • View: Displays information to the user. Updates to the view directly update the model.
  • Controller: Controls the information sent to the user by manipulating the view.
  • Model: The model provides the underlying domain structure. It tells the controller when and how it changes.

External links

See also

Personal tools