article

Model-view-controller (MVC) is a software architecture that separates an application's data model, user interface, and control logic into three distinct components so that modifications to one component can be made with minimal impact to the others.

MVC is often thought of as a software design pattern. However, MVC encompasses more of the architecture of an application than is typical for a design pattern. Hence the term architectural pattern may be useful (Buschmann, et al 1996), or perhaps an aggregate design pattern.

Operation


In broad terms, constructing an application using an MVC architecture involves defining three classes of modules.
  • Model: The domain-specific representation of the information on which the application operates. The model is another name for the domain layer. Domain logic adds meaning to raw data (e.g. calculating if today is the user's birthday, or the totals, taxes and shipping charges for shopping cart items).
  • View: Renders the model into a form suitable for interaction, typically a user interface element. MVC is often seen in web applications, where the view is the HTML page and the code which gathers dynamic data for the page.
  • Controller: Responds to events, typically user actions, and invokes changes on the model and perhaps the view.
  • Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention this data access layer, because it is understood to be underneath or encapsulated by the Model.

It's common to think of an application as having three main layers: presentation (UI), domain, and data access. In MVC, the presentation layer is split into controller and view. The most important separation is between presentation and domain; the View/Controller split is less so.

Though MVC comes in different flavors, control flow generally works as follows:

  1. The user interacts with the user interface in some way (e.g., user presses a button)
  2. A controller handles the input event from the user interface, often via a registered handler or callback.
  3. The controller accesses the model, possibly updating it in a way appropriate to the user's action (e.g., controller updates user's shopping cart). Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
  4. A view uses the model to generate an appropriate user interface (e.g., view produces a screen listing the shopping cart contents). The view gets its own data from the model. The model has no direct knowledge of the view. (However, the observer pattern can be used to allow the model to indirectly notify interested parties – potentially including views – of a change.)
  5. The user interface waits for further user interactions, which begins the cycle anew.

Through decoupling of the model, view and controller components, each unit can change irrespective of the implementation of the other. For instance, a typical implementation of a web application will rely on the view component to layout HTML in response to a user's request. If, however, an XML response is required, only the view component need change (to an XML formatting component) while the model and controller remain the same.

A simple diagram depicting the relationship between the Model, View, and Controller is provided below. Note: the solid lines indicate a direct association, and the dashed line indicate an indirect association (i.e. Observer pattern).

Implementations


The pattern was first described in 1979 by Trygve Reenskaug, then working on Smalltalk at Xerox research labs. The original implementation is described in depth in the influential paper Applications Programming in Smalltalk-80(TM):How to use Model-View-Controller.

Smalltalk's MVC implementation inspired many other GUI frameworks such as:

More recently there have been attempts to apply MVC architectures for web-based interfaces. In the design of web applications, MVC is also known as a "Model 2" architecture in Sun parlance. Complex web applications continue to be more difficult to design than traditional applications, and MVC is being pushed as a potential solution to these difficulties.

See also


External links


General information regarding MVC

Specific aspects of MVC or alternatives to MVC

MVC frameworks in PHP

References


Software design patterns | Software architecture | Programming paradigms

Model-view-controller | Model View Controller | Modelo Vista Controlador | MVC | Modèle-Vue-Contrôleur | Model-View-Controller | ארכיטקטורת MVC | Model-View-Controller-model | Model View Controller | MVC | MVC | Model View Controller | MVC-arkkitehtuuri | Model-View-Controller | MVC

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Model-view-controller".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld