History
One of the seminal insights in the early development of graphical user interfaces, MVC became one of the first approaches to describe and implement software constructs in terms of their responsibilities. Trygve Reenskaug created MVC while working on Smalltalk-79 as a visiting scientist at the Xerox Palo Alto Research Center (PARC) in the late 1970s.Notes and Historical documentsView
and Controller
classes as well as various concrete subclasses of each that represent different generic widgets. In this scheme, a View
represents some way of displaying information to the user, and a Controller
represents some way for the user to interact with a View
. A View
is also coupled to a model object, but the structure of that object is left up to the application programmer. The Smalltalk-80 environment also includes an "MVC Inspector," a development tool for viewing the structure of a given model, view, and controller side-by-side.
In 1988, an article in '' The Journal of Object Technology'' (JOT) by two ex-PARC employees presented MVC as a general "programming paradigm and methodology" for Smalltalk-80 developers. However, their scheme differed from both Reenskaug et al.'s and that presented by the Smalltalk-80 reference books. They defined a view as covering any graphical concern, with a controller being a more abstract, generally invisible object that receives user input and interacts with one or many views and only one model. Also published asComponents
Model
The central component of the pattern. It is the application's dynamic data structure, independent of the user interface. It directly manages the data, logic and rules of the application. In Smalltalk-80, the design of a model type is left entirely to the programmer. With WebObjects, Rails, and Django, a model type typically represents a table in the application'sView
Any representation of information such as a chart, diagram or table. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants. In Smalltalk-80, a view is just a visual representation of a model, and does not handle user input. With WebObjects, a view represents a complete user interface element such as a menu or button, and does receive input from the user. In both Smalltalk-80 and WebObjects, however, views are meant to be general-purpose and composable. With Rails and Django, the role of the view is played by HTML templates, so in their scheme a view specifies an in-browser user interface rather than representing a user interface widget directly. (Django opts to call this kind of object a "template" in light of this.) This approach puts relatively less emphasis on small, composable views; a typical Rails view has a one-to-one relationship with a controller action. Smalltalk-80 views communicate with both a model and a controller, whereas with WebObjects, a view talks only to a controller, which then talks to a model. With Rails and Django, a view/template is used by a controller/view when preparing a response to the client.Controller
Accepts input and converts it to commands for the model or view. A Smalltalk-80 controller handles user input events, such as button presses or mouse movement. At any given time, each controller has one associated view and model, although one model object may hear from many different controllers. Only one controller, the "active" controller, receives user input at any given time; a global window manager object is responsible for setting the current active controller. If user input prompts a change in a model, the controller will signal the model to change, but the model is then responsible for telling its views to update. In WebObjects, the views handle user input, and the controller mediates between the views and the models. There may be only one controller per application, or one controller per window. Much of the application-specific logic is found in the controller. In Rails, requests arriving at the on-server application from the client are sent to a "router," which maps the request to a specific method of a specific controller. Within that method, the controller interacts with the request data and any relevant model objects and prepares a response using a view. Conventionally, each model type has an associated controller; for example, if the application had aClient
model, it would typically have an associated Clients
controller as well. However, developers are free to make other kinds of controllers if they wish.
Django calls the object playing this role a "view" instead of a controller. A Django view is a function that receives a web request and returns a web response. It may use templates to create the response.
Interactions
In addition to dividing the application into these components, the model–view–controller design defines the interactions between them.Buschmann, Frank (1996) ''Pattern-Oriented Software Architecture''. * The model is responsible for managing the data of the application. It receives user input from the controller. * The view renders presentation of the model in a particular format. * The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model. As with other software patterns, MVC expresses the "core of the solution" to a problem while allowing it to be adapted for each system.Gamma, Erich et al. (1994) ''Design Patterns'' Particular MVC designs can vary significantly from the traditional description here.Motivation
As Alan Kay wrote in 2003 the original motivation behind the MVC was to allow creation of a graphical interface for any object. That was outlined in detail in Richard Pawson's book called " Naked Objects". Trygve Reenskaug, originator of MVC at PARC, has written that "MVC was conceived as a general solution to the problem of users controlling a large and complex data set." In their 1991 guide ''Inside Smalltalk'', Carleton University computer science professors Wilf LaLonde and John Pugh described the advantages of Smalltalk-80-style MVC as: * independence of presentation and data, e.g. multiple views on one model simultaneously, * composable presentation widgets, e.g. one view used as a subview of another, * switchable input modes, by swapping one controller out for another during runtime, and * independence of input and output processing, via the separate responsibilities of controllers and views.Use in web applications
Although originally developed for desktop computing, MVC has been widely adopted as a design forSee also
References
Bibliography
{{DEFAULTSORT:Model-view-controller Software design patterns