Model–view–controller
![]() | This article may require copy editing for grammar, style, cohesion, tone, or spelling. (December 2008) |
Model–view–controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model.
History
MVC was first described in 1979[1] 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.[2]
There have been several derivatives of MVC; one of the most known (due to its use by Microsoft) is the Model View Presenter pattern which appeared in the early 1990s and was designed to be an evolution of MVC. However Model–View–Controller still remains very widely used.
In November 2002 the W3C voted to make MVC structures part of their XForms architecture for all future web applications [3]. These specifications will now be integrated directly into the XHTML 2.0 specifications. There are now over 20 vendors that support XForms frameworks with MVC integrated into the application stack.
Pattern description
Model–view–controller is both an architectural pattern and a design pattern, depending on where it is used.
As an architectural pattern
It is common to split an application into separate layers that run on different computers: the presentation/user interface (UI) (view), business logic (controller), and data access (model).
MVC is often seen in web applications, where the view is the actual HTML or XHTML page, and the controller is the code that gathers dynamic data and generates the content within the HTML or XHTML. Finally, the model is represented by the actual content, which is often stored in a database or in XML nodes, and the business rules that transform that content based on user actions.
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.
- 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 uses the model indirectly 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. The model and controller have no direct knowledge of the view.
- 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.
By decoupling models and views, MVC helps to reduce the complexity in architectural design and to increase flexibility and reuse of code. [citation needed]
As a design pattern
MVC encompasses more of the architecture of an application than is typical for a design pattern. [citation needed] When considered as a design pattern, MVC is semantically similar to the Observer pattern.
- Model
- Is the domain-specific representation of the data on which the application operates. Domain logic adds meaning to raw data (for example, calculating whether today is the user's birthday, or the totals, taxes, and shipping charges for shopping cart items).
- 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.
- 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.
- Controller
- Processes and responds to events (typically user actions) and may indirectly invoke changes on the model.
Selected frameworks
GUI frameworks
Java: Java Swing
Java Swing is different from the other frameworks in that it supports two MVC patterns:
Model
- Frame level model—Like other frameworks, the design of the real model is usually left to the developer.
- Control level model—Swing also supports models on the level of controls (elements of the graphical user interface). Unlike other frameworks, Swing exposes the internal storage of each control as a model.
- View
- The view is represented by a class that inherits from Component.
- Controller
- Java Swing doesn't use a single controller. Because its event model is based on interfaces, it is common to create an anonymous action class for each event. [citation needed] In fact, the real controller is in a separate thread, the Event dispatching thread. It catches and propagates the events to the view and model.
Combined frameworks
Java: Java Platform, Enterprise Edition (Java EE)
Simple Version using only Java Servlets and JavaServer Pages from Java EE:
- Model
- The model is a collection of Java classes that forms a software application intended to store, and optionally move, data. There is 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 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 JavaServer Faces Technology (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:
- 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+.
- JFace.
- MFC (called Document/View architecture here).
- Microsoft Composite UI Application Block, part of the Microsoft Enterprise Library.
- Qt since Qt4 release.
- Java Swing.
- Adobe Flex.
- Wavemaker open source, browser-based development tool based on MVC.
- WPF uses a similar Model–view–viewmodel pattern.
- Visual FoxExpress is a Visual FoxPro MVC framework.
- Tk toolkit Tcl/Tk toolkit.
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 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]
.NET
- ASP.NET MVC Framework
- Maverick.NET
- Monorail An ActionPack inspired MVC framework from the Castle Project
- ProMesh.NET Open source MVC Web Application Framework for .NET 2.0 or higher.
- PureMVC Framework for C#
- Spring Framework.NET
- PureMVC Framework for Actionscript.
- FlashMVC- A lightweight Framework for Actionscript3 aimed at beginners to power users.
- 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.
- 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.
- PureMVC Framework for ColdFusion
- Coldbox is an event-driven conventions based MVC ColdFusion Framework with an extensive array of patterns for its operations such as Factories, Helpers, Workers, etc.
- Switchboard is a MVC framework with built in authentication, redirecting, and URL routing.
Flex
- Cairngorm one of the primary open source frameworks for application architecture in Adobe Flex.
- Mate Architectural framework for Flex development.
Java
MVC web application frameworks:
- Aranea
- Cocoon
- JSF
- Oracle Application Framework
- Spring MVC Framework
- Struts
- Stripes
- Tapestry
- WebObjects
- WebWork
- Wicket
- PureMVC Framework for Java
- LongJump
- Sofia
- Struts2
Groovy
JavaScript
MVC web application frameworks:
- SproutCore
- Wavemaker WYSIWYG development platform for Ajax web applications[5]
- DojoMVC JavaScript MVC based upon Dojo core. Includes 200 method library, jQuery and MooTools integration.
- PureMVC Framework for JavaScript
- Woozoo MVC Framework for the Prototype JavaScript Framework
- JavascriptMVC Javascript MVC framework based upon jQuery core.[6]
ABAP Objects
Informix 4GL
- Informix 4GL MVC models to use for Informix 4GL report and form creation
- EGL — IBM's EGL MVC Implementation
Lua
Perl
- Catalyst An MVC-based avant-garde web framework.
- O2 An MVC-based web framework.
- Maypole A useful perl MVC framework
- Gantry An MVC framework similar to django
- Jifty the "one true way" for MVC
- CGI::Application Another MVC Web Framework
- MasonX::MiniMVC MVC Framework
- Solstice Based on the MVC programming paradigm
PHP
- Agavi a PHP5 application framework that focuses on sustained quality and correctness.
- Akelos PHP Framework a Ruby on Rails port to PHP4/5..
- Atomik Framework micro framework for PHP5.
- CakePHP webapplication framework modeled after the concepts of Ruby on Rails.
- CodeIgniter A PHP MVC framework.
- CodeLighter Micro Codeigniter-like MVC PHP5 framework that uses Codeigniter plugins and libraries.
- Concrete5 A PHP MVC framework and CMS platform.
- Drupal A Content Management System utilizing the MVC framework.
- Exponent CMS is web Content Management System web application framework using its own MVC framework modeled after Rails.
- FUSE A powerful but easy-to-use PHP 5 Framework for MVC development modeled after the concepts of Ruby on Rails.
- FLOW3 TYPO3's MVC framework (being developed)
- Jelix (web framework)
- Jaws is a Framework and Content Management System for building dynamic web sites.
- Joomla v1.5.x is an open source Content Management System that employs the MVC model for its extensions, called components and modules.
- Kohana is an open source MVC oriented framework, originally forked from CodeIgniter.
- Kumbia PHP Framework is an open source PHP5 MVC framework with ActiveRecord, Routing,...
- LISA MVC is an open source object oriented web application framework.
- Micro CMS Open Source (LGPL) MVC framework and nice and simple CMS in one
- MVCnPHP Fast, open source and only does MVC.
- Nette Framework A PHP MVC/MVP framework.
- Neutron A lightweight open-source PHP5 library featuring a simple MVC routing framework.
- Odin Assemble Small footprint PHP based MVC Framework.
- OpenCart Open Source Shopping Cart based on MVC Framework.
- Orinoco Framework is a full-stack yet lightweight framework written in PHP5. It implements the Model 2 design paradigm.
- PHPonTrax A PHP 5 MVC framework modeled after Ruby on Rails.
- phpXCore A MVC design pattern based PHP content management framework compatible with PHP4 and PHP5.
- Prado A PHP 5 MVC framework modeled after ASP.NET web forms.
- PureMVC Framework for PHP
- Qcodo is an open-source PHP 5 web application framework that also includes a code generator.
- QCubed is a community fork of Qcodo with more rapid development.
- SilverStripe contains a fully fledged PHP 5.2 ORM/MVC Framework focused on building websites. Much like Ruby on Rails.
- Solar
- Switch board (framework) with Routing PHP 5 MVC Framework with Routing.
- Symfony Framework PHP 5 MVC Framework modeled after the concepts of Ruby on Rails.
- Yii PHP Framework A PHP 5, high-performance component-based framework best for developing large-scale Web applications
- SimpleTools An Object Oriented MVC Framework
- MvcSkel web framework for PHP5
- Zend Framework A PHP 5-based MVC framework modeled after the concepts of Ruby on Rails.
- ZNF PHP5 MVC framework for enterprise web applications
- Zoop Framework A Mature PHP 4/5 MVC framework.
Python
- Django A complete Python web application framework. Django prefers to call its MVC implementation MTV, for Model-Template-View.
- Enthought The Enthought Tool Suite brings the Model–view–controller mindset to scientific GUI's 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
Smalltalk
XML
- 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
- 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
References
- ^ Trygve M. H. Reenskaug/MVC—XEROX PARC 1978-79
- ^ How to use Model–View–Controller (MVC)
- ^ Dubinko, Micah (2004-11-12). "XForms 1.0 Basic Profile". Retrieved 2008-10-16.
{{cite web}}
: Check date values in:|date=
(help) - ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
- ^ "Product Review: WaveMaker's point-and-click Java". Infoworld. April 24, 2008. Retrieved 2008-04-25.
- ^ "JavascriptMVC Learning Center".
External links
- An overview of the MVC pattern in Java from the Sun website
- 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", Ctrl-Shift-B, 2007.
- Building Graphical User Interfaces with the MVC Pattern in Java
- What is behind MVC