Model view controller

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (MVC-Observer-Strategy)
m (Reverted edits by Ebybymic (Talk); changed back to last version by Marina)
 
(9 intermediate revisions by 3 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 an 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. In OO terms, the ''"controller offers facilities to change the state of 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.  
  
[[Ward's wiki]] considers both of these options incorrect [http://www.c2.com/cgi/wiki?ModelViewController]. The controller should simply exist to promote communication and validation between the model and the view. It should provide ways to change the view, either through direct calls or the use of an [[Observer]]. 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 ==
 
== Patterns ==
Line 15: Line 20:
 
=== MVC-Observer-Strategy ===
 
=== MVC-Observer-Strategy ===
 
[[Image:MVCDiagram.png|none|frame|Diagram from lecture 27/7/09]]
 
[[Image:MVCDiagram.png|none|frame|Diagram from lecture 27/7/09]]
This model is the original MVC pattern proposed by [[Trygve Reenskaug]] in 1979t and popularised in its use with [[Smalltalk]]. This model is also Sun's proposed structure of [http://java.sun.com/developer/technicalArticles/J2EE/despat/ MVC in J2EE].
+
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]].
 
* '''Model''' and '''View''' resemble an [[Observer]].
 
* The '''Controller''' is a [[Strategy]].
 
* The '''Controller''' is a [[Strategy]].
Line 23: Line 28:
 
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.
 
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'''
 
* '''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 ===
 
=== MVC as an Input-Output Process ===
Line 36: Line 43:
 
* '''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.
 
* '''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.
 
* '''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==
 
==External links==
Line 42: Line 57:
 
* [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://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://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]
+
* [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==

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