HOME

TheInfoList



OR:

Model–view–controller (MVC) is a software
architectural pattern An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware perform ...
commonly used for developing
user interface In the industrial design field of human–computer interaction, a user interface (UI) is the space where interactions between humans and machines occur. The goal of this interaction is to allow effective operation and control of the machine fr ...
s that divide the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user. Traditionally used for desktop
graphical user interface The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inst ...
s (GUIs), this pattern became popular for designing
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serv ...
s. Popular programming languages have MVC frameworks that facilitate the implementation of the pattern. __TOC__


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 Trygve Mikkjel Heyerdahl Reenskaug (born 21 June 1930) is a Norwegian computer scientist and professor emeritus of the University of Oslo. He formulated the model–view–controller (MVC) pattern for graphical user interface (GUI) software des ...
created MVC while working on
Smalltalk Smalltalk is an object-oriented, dynamically typed reflective programming language. It was designed and created in part for educational use, specifically for constructionist learning, at the Learning Research Group (LRG) of Xerox PARC by Alan K ...
-79 as a visiting scientist at the Xerox
Palo Alto Research Center PARC (Palo Alto Research Center; formerly Xerox PARC) is a research and development company in Palo Alto, California. Founded in 1969 by Jacob E. "Jack" Goldman, chief scientist of Xerox Corporation, the company was originally a division of Xe ...
(PARC) in the late 1970s.Notes and Historical documents
from Trygve Reenskaug, inventor of MVC.
"A note on DynaBook requirements", Trygve Reenskaug, 22 March 1979
SysReq.pdf
He wanted a pattern that could be used to structure any program where users interact with a large, convoluted data set. His design initially had four parts: Model, View, Thing, and Editor. After discussing it with the other Smalltalk developers, he and the rest of the group settled on Model, View, and Controller instead. In their final design, a Model represents some part of the program purely and intuitively. A View is a visual representation of a Model, retrieving data from the Model to display to the user and passing requests back and forth between the user and the Model. A Controller is an organizational part of the user interface that lays out and coordinates multiple Views on the screen, and which receives user input and sends the appropriate messages to its underlying Views. This design also includes an Editor as a specialized kind of Controller used to modify a particular View, and which is created through that View. Smalltalk-80 supports a version of MVC that evolved from this one. It provides abstract View 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 as
A Description of the Model–View–Controller User Interface Paradigm in the Smalltalk-80 System
(Report), ParcPlace Systems; Retrieved 2012-06-05.
The MVC pattern subsequently evolved,
from Martin Fowler.
giving rise to variants such as hierarchical model–view–controller (HMVC), model–view–adapter (MVA), model–view–presenter (MVP),
model–view–viewmodel Model–view–viewmodel (MVVM) is an architectural pattern in computer software that facilitates the separation of the development of the graphical user interface (GUI; the ''view'')—be it via a markup language or GUI code—from the develop ...
(MVVM), and others that adapted MVC to different contexts. The use of the MVC pattern in web applications grew after the introduction of
NeXT Next may refer to: Arts and entertainment Film * ''Next'' (1990 film), an animated short about William Shakespeare * ''Next'' (2007 film), a sci-fi film starring Nicolas Cage * '' Next: A Primer on Urban Painting'', a 2005 documentary film Lit ...
's
WebObjects WebObjects was a Java web application server and a server-based web application framework originally developed by NeXT Software, Inc. WebObject's hallmark features are its object-orientation, database connectivity, and prototyping tools. ...
in 1996, which was originally written in
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
(that borrowed heavily from Smalltalk) and helped enforce MVC principles. Later, the MVC pattern became popular with Java developers when WebObjects was ported to
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
. Later frameworks for Java, such as Spring (released in October 2002), continued the strong bond between Java and MVC. In 2003, Martin Fowler published ''Patterns of Enterprise Application Architecture'', which presented MVC as a pattern where an "input controller" receives a request, sends the appropriate messages to a model object, takes a response from the model object, and passes the response to the appropriate view for display. This is close to the approach taken by the
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
framework (August 2004), which has the client send requests to the server via an in-browser view, where they are handled by a controller, which then communicates with the appropriate model objects. The Django framework (July 2005, for
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pr ...
) put forward a similar "MTV" (Model Template View) take on the pattern, in which a view retrieves data from models and passes it to templates for display. Both Rails and Django debuted with a strong emphasis on rapid deployment, which increased MVC's popularity outside the traditional enterprise environment in which it has long been popular.


Components


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's
database In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases span ...
.


View

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 a Client 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 Naked objects is an architectural pattern used in software engineering. It is defined by three principles: The naked object pattern's innovative feature arises by combining the and principles into a principle: The naked objects pattern was ...
". 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 for
World Wide Web The World Wide Web (WWW), commonly known as the Web, is an information system enabling documents and other web resources to be accessed over the Internet. Documents and downloadable media are made available to the network through web se ...
applications in major
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s. Several
web framework A web framework (WF) or web application framework (WAF) is a software framework that is designed to support the development of web applications including web services, web resources, and web APIs. Web frameworks provide a standard way to build and ...
s have been created that enforce the pattern. These
software framework In computer programming, a software framework is an abstraction in which software, providing generic functionality, can be selectively changed by additional user-written code, thus providing application-specific software. It provides a standard ...
s vary in their interpretations, mainly in the way that the MVC responsibilities are divided between the client and server. Some web MVC frameworks take a
thin client In computer networking, a thin client is a simple (low-performance) computer that has been optimized for establishing a remote connection with a server-based computing environment. They are sometimes known as ''network computers'', or in t ...
approach that places almost the entire model, view and controller logic on the server. In this approach, the client sends either
hyperlink In computing, a hyperlink, or simply a link, is a digital reference to data that the user can follow or be guided by clicking or tapping. A hyperlink points to a whole document or to a specific element within a document. Hypertext is text wi ...
requests or form submissions to the controller and then receives a complete and updated web page (or other document) from the view; the model exists entirely on the server.


See also


References


Bibliography

{{DEFAULTSORT:Model-view-controller Software design patterns