Model–view–controller
This page or section may contain link spam masquerading as content. (August 2010) |
![]() | This article may contain unverified or indiscriminate information in embedded lists. (September 2010) |

Model–View–Controller (MVC) is a software architecture,[1] currently considered an architectural pattern used in software engineering. The pattern isolates "domain logic" (the application logic for the user) from the user interface (input and presentation), permitting independent development, testing and maintenance of each (separation of concerns).
The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.
The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.
The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.
An MVC application may be a collection of model/view/controller triads, each responsible for a different UI element. The Swing GUI system, for example, models almost all interface components as individual MVC systems.
MVC is often seen in web applications where the view is the HTML or XHTML generated by the app. The controller receives GET or POST input and decides what to do with it, handing over to domain objects (i.e. the model) which contain the business rules and know how to carry out specific tasks such as processing a new subscription, and which hand control to (X)HTML-generating components such as templating engines, XML pipelines, Ajax callbacks, etc.
The model is not necessarily merely a database; the 'model' in MVC is both the data and the business/domain logic needed to manipulate the data in the application. Many applications use a persistent storage mechanism such as a database to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the model. Models are not data access objects; however, in very simple apps that have little domain logic there is no real distinction to be made. Active Record is an accepted design pattern which merges domain logic and data access code - a model which knows how to persist itself.
History
MVC was first described in 1979[2] by Trygve Reenskaug, then working on Smalltalk at Xerox PARC. The original implementation is described in depth in the influential paper "Applications Programming in Smalltalk-80: How to use Model–View–Controller".[3]
Overview
Though MVC comes in different flavors, control flow is generally as follows:
- The user interacts with the user interface in some way. (for example, presses a mouse button).
- The controller handles the input event from the user interface, often via a registered handler or callback and converts the event into appropriate user action, understandable for the model.
- The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)[4]
- A view queries the model in order to generate an appropriate user interface (for example, the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) which require a screen update.
- The user interface waits for further user interactions, which restarts the cycle.
Some implementations such as the W3C XForms also use the concept of a dependency graph to automate the updating of views when data in the model changes.
The goal of MVC is, by decoupling models and views, to reduce the complexity in architectural design and to increase flexibility and maintainability of code.
Architecture vs. Frameworks
Although MVC is typically associated with frameworks, it is essentially an architecture. This means that it can be implemented even without an object-oriented language or a specific class hierarchy. For example, using as little as jQuery's trigger() and bind(), it's possible to build robust MVC applications in a browser using Javascript. The key is simply to divide up the responsibilities of the MVC components into clearly defined sections of code. As stated in the overview, the code that embodies the model takes care of state, business logic, persistence, and notifications. The persistence can by implemented via cookies or AJAX. The notifications can be taken care of by jQuery.trigger(). The code that embodies the view takes care of querying the model and rendering the view. The view code can be implemented in a variety of ways, including inserting HTML DOM nodes or changing CSS styles. The code that embodies the controller takes care of initialization of the model and wiring up the events between the view's HTML DOM elements and controller and between the model and the view code, using jQuery.bind().
Selected frameworks
GUI frameworks
Combined frameworks
Java: Java Platform, Enterprise Edition (Java EE)
Simple Version implementing Java Servlets and JavaServer Pages from Java EE:
- Model
- This is the application’s business layer and is usually comprised of the objects that represent the business entities that make up the application[5]. The model is a collection of Java classes that form a software application intended to store, and optionally separate, data. A single front end class that can communicate with any user interface (for example: a console, a graphical user interface, or a web application).
- View
- The view is represented by a JavaServer Page, with data being transported to the page in the HttpServletRequest or HttpSession.
- Controller
- The Controller servlet communicates with the front end of the model and loads the HttpServletRequest or HttpSession with appropriate data, before forwarding the HttpServletRequest and Response to the JSP using a RequestDispatcher.
The Servlet is a Java class, and it communicates and interacts with the model, but does not need to generate HTML or XHTML output; the JSPs do not have to communicate with the model because the Servlet provides them with the information—they can concentrate on creating output.
Unlike the other frameworks, Java EE defines a pattern for model objects.
- Model
- The model is commonly represented by entity beans, although the model can be created by a servlet using a business object framework such as Spring.
- View
- The view in a Java EE application may be represented by a JavaServer Page, which may be currently implemented using a web framework, such as Adobe Flex, Struts 2, or JavaServer Faces (JSF). Alternatively, the code to generate the view may be part of a servlet.
- Controller
- The controller in a Java EE application may be represented by a servlet, which may be currently implemented using JavaServer Faces (JSF).
XForms
XForms is an XML format for the specification of a data processing model for XML data and user interface(s) for the XML data, such as web forms.
- Model
XForms stores the Model as XML elements in the browser. They are usually placed in the non-visible <head>
elements of a web page.
- View
The Views are XForms controls for screen elements and can be placed directly in the visible section of web page. They are usually placed in the <body>
elements of a web page.
The model and views are bound together using reference or binding statements. These binding statements are used by the XForms dependency graph to ensure that the correct views are updated when data in the model changes. This means that forms developers do not need to be able to understand either the push or pull models of event processing.
- Controller
All mouse events are processed by XForms controls and XML events are dispatched.
Implementations of MVC as GUI frameworks
Smalltalk's MVC implementation inspired many other GUI frameworks, such as the following:
- XPages - for IBM Lotus Notes/Domino
- Cocoa framework and its GUI part AppKit, as a direct descendant of OpenStep, encourage the use of MVC. Interface Builder constructs Views, and connects them to Controllers via Outlets and Actions.
- GNUstep, also based on OpenStep, encourages MVC as well.
- GTK+ provides models (as both interfaces and as concrete implementations) and views, while clients implement the controllers through signals.
- JFace.
- Microsoft Foundation Class Library (MFC) - called the Document/View architecture
- Microsoft ASP.NET - reusing JQuery libraries and propietary Microsoft AJAX Libraries.
- Microsoft Composite UI Application Block, part of the Microsoft Enterprise Library.
- Qt since Qt4 release.
- Java Swing.
- Apache Pivot.
- Adobe Flex with the Cairngorm Framework.
- Wavemaker open source, browser-based development tool based on MVC.
- The Model–view–viewmodel pattern, similar to MVC, is often used to develop Windows Presentation Foundation (WPF) applications.
- Visual FoxExpress is a Visual FoxPro MVC framework.
- Crank Storyboard Suite
Implementations of MVC as web-based frameworks
![]() | It has been suggested that this article be merged into Comparison of web application frameworks. (Discuss) Proposed since September 2008. |
In the design of web applications, MVC is implemented by web template systems as a "View for web" component.
MVC is typically implemented as a "Model 2" architecture in Sun parlance. Model 2 focuses on efficiently handling and dispatching full page form posts and reconstructing the full page via a front controller. Complex web applications continue to be more difficult to design than traditional applications because of this "full page" effect. More recently "View for web" Ajax driven frameworks that focus on firing focused UI events at specific UI Components on the page are emerging. This is causing MVC to be revisited for web application development using traditional desktop programming techniques. [citation needed]
ABAP Objects
ActionScript
- PureMVC Framework for ActionScript.
- Cairngorm is a Flex Framework part of the Adobe Engagement Platform.
ASP
C++
- Wt - Web toolkit A library and application server for web applications using a desktop-like event-driven MVC pattern.
- CppCMS - A C++ MVC framework that has taken many ideas from Django.
CFML - Adobe ColdFusion, Railo, and Open BlueDragon
- ColdFusion on Wheels A convention over configuration framework similar to Ruby on Rails.
- Framework One Framework/1 is a new framework developed by Sean Corfield and provides a good stepping stone for users new to the MVC pattern. It consists of a single CFC and favors convention over configuration.
- Fusebox Fusebox does not force the Model–View–Controller (MVC) pattern or Object-Oriented Programming (OOP) on the developer. However, either or both of these development approaches can be used with Fusebox.
- Mach-II A framework that focuses on trying to ease software development and maintenance.
- Model-Glue Through a simple implementation of Implicit Invocation and Model–View–Controller, they allow applications to be well organized without sacrificing flexibility.
- PureMVC Framework for ColdFusion.
Flex
- Cairngorm one of the primary open source frameworks for application architecture in Adobe Flex.
- PureMVC ActionScript 3 MVC framework for Flex, Flash and AIR development.
Groovy
Java
MVC web application frameworks:
- Aranea
- Cocoon
- CodeCharge Studio
- Induction
- JSF
- Makumba Web development framework in the form of JSP Tag Library and Java API that is based on MVC, but willingly breaks it
- Oracle Application Development Framework
- Oracle Application Framework
- Play Framework
- PureMVC, a framework for Java
- Sling, used to create content based applications on top of JCR. Supported scripting languages are JSP, server-side JavaScript, Ruby, Velocity
- Spring MVC Framework
- Struts
- Struts2
- Stripes
- Tapestry
- VRaptor
- Wavemaker, a WYSIWYG development platform for Ajax web applications.[6]
- WebObjects
- WebWork
- Wicket
- Web Dynpro Java
Java Stand-alone Application Toolkit:
- Swing, which uses a Model-Delegator pattern, where the view and controller are combined, but the model is separate.
JavaScript
MVC web application frameworks:
- SproutCore
- PureMVC Framework for JavaScript
- JavascriptMVC Javascript MVC framework based upon jQuery core.
- eMVC is an MVC framework based upon Dojo Toolkit.
- Dojo toolkit MVC used in stores + widgets.
Informix 4GL
- IBM Informix-4GL MVC models to use for Informix 4GL report and form creation
- EGL — IBM's EGL MVC Implementation
.NET
- ASP.NET MVC Framework
- Bistro Framework
- CodeCharge Studio
- FubuMVC
- Maverick.NET
- MonoRail An ActionPack inspired MVC framework from the Castle Project
- Naked Objects MVC
- PureMVC Framework for C
- Spring Framework.NET
Perl
- Catalyst - Mature, stable and fast MVC framework inspired by Ruby on Rails.
- CodeCharge Studio
- Maypole - An MVC Web framework, superseded by Catalyst.
PHP
- Agavi is a PHP5 application framework that focuses on sustained quality and correctness.
- Alloy A lightweight REST-focused modular Hierarchical MVC PHP 5.3 framework
- CakePHP A web application framework modeled after the concepts of Ruby on Rails.
- CodeCharge Studio is a visual rapid application development environment for web-based database driven application development. Code Charge Studio places emphasis on code generation technology to provide ASP.NET, PHP, JSP, Servlets, ColdFusion and Perl language support.
- CodeIgniter A simple, light, fast, open source MVC framework for building websites using PHP.
- Drupal An open source content management system that uses MVC for its add-ons called modules.
- eddy a lightweight, open-source PHP 5.3 MVC web application framework
- Exponent CMS A Content Management System web application framework using its own MVC framework modeled after Rails.
- eZ Publish Based on eZ Components is an object-oriented web application framework written in PHP that separates its functionality into several abstraction layers, following a strict MVC approach.
- Feng Office is an open source MVC Framework Extranet that allows a group of geographically distributed people to collaborate by sharing information over the Internet.
- Joomla! v1.5.x is an open source content management system that employs the MVC model for its extensions, called components and modules.
- Kohana v2.x is an open source MVC framework, while v3.x is HMVC (both supported).
- MODx A full-featured open source OOP MVC/ORB xPDO-based CMS.
- MooVC An Object Oriented MVC Framework for PHP.
- Odin Assemble A Small footprint PHP based MVC framework.
- OpenCart Shopping cart which is fully based on MVC framework.
- phpXCore A MVC design pattern based PHP content management framework compatible with PHP4 and PHP5.
- PRADO Framework An open source PHP5 framework for creating professional web applications.
- PureMVC A framework for PHP.
- Qcodo An open-source PHP 5 web application framework.
- Recess An open-source framework focused on RESTful API and advanced language features
- SilverStripe Contains a fully fledged PHP 5.2 ORM/MVC framework focused on building websites.
- Switch board (framework) A PHP 5 MVC framework with routing.
- Spawn Framework An open source PHP5/OOP MVC framework
- Symfony Framework A PHP 5 MVC framework modeled after the concepts of Ruby on Rails.
- Yii An open source, object-oriented, high-performance component-based PHP web application framework.
- Zend Framework An open-source PHP 5-based framework featuring an MVC layer and a broad-spectrum of loosely coupled components.
Python
- Django A complete Python web application framework. Django prefers to call its MVC implementation MTV, for Model-Template-View.[7]
- Enthought The Enthought Tool Suite brings the Model–view–controller mindset to scientific GUIs and visualization
- Pylons Python Web Framework
- TurboGears for Python
- web2py A scalable full-stack enterprise level Python agile web development framework with support for highly flexible and rapid database-driven web application development.
- Zope Web application server
- Plone Content management system built on top Zope
- PureMVC Framework for Python
Ruby
- Ruby on Rails
- Merb
- Ramaze
- Camping
- Nitro
- Monkeybars
- PureMVC Framework for Ruby.
Smalltalk
XML
- Obyx—Designed from the ground up to use and encourage Model–view–controller programming, integrating mysql xhtml and httpd in a simple and intuitive manner.
- XForms—XForms has an integrated Model–view–controller architecture with an integral dependency graph that frees the programmer from specifically having to perform either push or pull operations.
See also
- Trygve Reenskaug—first formulated the model–view–controller pattern
- Architectural patterns
- Model View Presenter
- Model 1
- Model 2
- Three-tier (computing)
- The Observer design pattern
- The Template Method design pattern
- The Presentation-abstraction-control (PAC) pattern
- The naked objects pattern, often positioned as the antithesis of MVC, though Reenskaug suggests otherwise
- Model–View–Adapter
- Model View ViewModel
- Application View Controller an alternative to Model View Controller
- Template processor
References
- ^ Reenskaug, Trygve. "MVC XEROX PARC 1978-79". Retrieved 2008-06-09.
- ^ Trygve M. H. Reenskaug/MVC—XEROX PARC 1978-79
- ^ How to use Model–View–Controller (MVC)
- ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
- ^ MVC Model View Controller ASPNET Book 2010
- ^ "Product Review: WaveMaker's point-and-click Java". Infoworld. April 24, 2008. Retrieved 2008-04-25.
- ^ Django appears to be a MVC framework, but you call the Controller the “view”, and the View the “template”. How come you don’t use the standard names?, FAQ: General, Django
External links
- An overview of the MVC pattern in Java from the Sun website and details of MVC design in Java Swing
- Model View Presenter with ASP.NET CodeProject article.
- History of the evolution of MVC and derivatives by Martin Fowler.
- ASP.NET MVC Framework Microsoft's Scott Guthrie on .NET MVC
- Introduction to the ASP.NET Model View Controller (MVC) Framework Scott Hanselman builds an application step-by-step using the first CTP of the ASP.NET MVC Framework in this Introductory Video
- Holub, Allen (1999). "Building user interfaces for object-oriented systems". Java World.
- Greer, Derek. "Interactive Application Architecture Patterns", Aspiring Craftsman, 2007.
- Building Graphical User Interfaces with the MVC Pattern in Java
- Patrón clásico de diseño web Modelo-Vista-Controlador (MVC) en PHP
- Model View Controller in PHP A simple introduction to MVC pattern in PHP.