Model view controller
The Model View Controller (MVC) is one of the oldest architectural patterns. It is still widely used in web and application programming. It was 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.
- 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
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
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.
MVC as an Input-Output Process
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
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
- Model View Controller - What Ward's wiki has to say about the MVC
- What's a Controller anyway? has loads of interesting history. Note the comment by Ralph Johnson about half way down.
- MVC's OO Nature - A discussion on Ward's wiki as to whether the MVC is appropriate or even valid for OOD.
- GUI Architectures by Martin Fowler
- MVC & MVP
See also
- Architectural patterns
- Presentation separation idiom
- Observer
- The MVC song
- MVC Paper - The original paper describing the MVC Pattern - Trygve Reenskaug 1979. More details of Trygve's involvement can be found here.