Model–view–controller

In software engineering, Model–View–Controller (MVC) is an architectural pattern that splits interactions between users and applications into three roles: the Model (business logic), the View (user interface), and the Controller (user input).[1][2] This separation of concerns facilitates the independent development, testing, and maintenance of each role.
Contents |
History
MVC was first described in 1979 by Trygve Reenskaug, then working on Smalltalk at Xerox PARC.[3] The original implementation is described in depth in the influential paper "Applications Programming in Smalltalk-80: How to use Model–View–Controller".[4] MVC was then called a software architecture, in a now-deprecated sense.
Since the late 1990s, MVC is commonly classified as an architectural pattern,[5] i.e. a classic way of structuring software that is used in a software architecture in the modern sense of Shaw and Garlan.[6]
Overview
Although MVC comes in different flavors, the control flow is generally as follows:
- The user interacts with the user interface in some way (for example, by pressing 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 an 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).[7]
- A view queries the model 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) that require a screen update.
- The user interface waits for further user interactions, which restarts the control flow cycle.
Some implementations (e.g., 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 to simplify the architecture by decoupling models and views, and to make source code more flexible and maintainable. MVC has also been used to simplify the design of autonomic and self-managed systems.[8]
Concepts
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 view port typically has a one to one correspondence with a display surface and knows how to render to it.
The Controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and a view port to perform actions based on that input.
An application based on the MVC pattern may be a collection of model–view–controller triads, each responsible for a different user interface 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 application. The controller receives GET or POST input and decides what to do with it, handing over to domain objects (i.e. the model) that 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 a database. In MVC, the Model is both the data and the business 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 applications that have little domain logic there is no real distinction to be made. Active record is an accepted design pattern that merges domain logic and data access code; a model which knows how to persist itself.
Architecture vs. frameworks
Although the MVC pattern is typically associated with frameworks, it is essentially architectural.[9] This means that it can be implemented even without the object-oriented language or the specific class hierarchy. For example, using as little as jQuery's trigger()
and bind()
, it is possible to build robust MVC-based applications in the browser using JavaScript. The key is simply to divide up the responsibilities of the MVC components into clearly defined sections of the code. As stated in the overview, the code that embodies the model takes care of the state, business logic, persistence, and notifications. Persistence can be 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 Cascading Style Sheets (CSS) styles. The code that embodies the controller takes care of the initialization of the model and the wiring up of the events between the view's HTML DOM elements and controller and between the model and the view code, using jQuery.bind()
.
Example
Here is a simple application of the pattern, implementing Java Servlets and Java Server Pages from Java EE:[10]
- Model
- 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 Java Server 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.
Implementations as GUI frameworks
Smalltalk's MVC implementation inspired many other graphical user interface (GUI) frameworks, such as the following:
- AppFlower – open source application builder with visual designer.
- 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.
- GEOS (16-bit operating system)
- GNUstep – also based on OpenStep, encourages MVC
- GTK+ – provides models (as both interfaces and as concrete implementations) and views, while clients implement the controllers through signals
- JFace
- Oracle Application Development Framework – Oracle ADF
- Microsoft Foundation Class Library (MFC) – called the document/view architecture.
- ASP.NET MVC Framework – reusing jQuery libraries and proprietary 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
- Eiffel Agents – see Touch of Class, chapter 17
Implementations as web-based frameworks
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
- Bixbite MVC – framework
- Cairngorm – a Flex framework part of the Adobe Engagement Platform
- Fabrication Framework
- PureMVC – framework
- Robotlegs – MVCS framework with dependency injection
C++
- Wt – a library and application server for web applications using a desktop-like event-driven MVC pattern
- CppCMS – an MVC framework with many ideas from Django
- PureMVC – framework
CFML - Adobe ColdFusion, Railo, and Open BlueDragon
- ColdBox Platform The first conventions over configuration framework and development platform for ColdFusion
- 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.
CoffeeScript
DOS
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
- Robotlegs AS3 MVC framework for Flex, Flash and AIR
- Mate AS3 MVC framework for Flex
- Parsley ActionScript 3 MVC framework for Flex, Flash and AIR
Groovy
Java
MVC web application frameworks:
- Aranea
- Cocoon
- CodeCharge Studio
- Induction dynamically reloads class changes, only framework that supports static MVC dependency analysis
- JavaServer Faces (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
- Wavemaker, a WYSIWYG development platform for Ajax web applications.[14]
- 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
- ZK - an Ajax framework that enables the Ajax-level MVC pattern
- Hibernate
JavaScript
MVC web application frameworks:
- Agility.js a framework with agility
- AngularJS aims to be a terse but powerful client-side MVC
- Knockout.js An MVC Framework based on MVVM for JavaScript.
- 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.
- Backbone.js Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
- ExtJS uses MVC as of version 4
- Express A light-weight Sinatra-inspired web framework for Node.js
- Gravey JavaScript MVC/RIA framework (iPad compatible)
- YUI 3 has an MVC module
- eyeballs.js
- Myna JavaScript Application Server (FlightPath)
- spine.js[15]
Informix 4GL
- EGL — IBM's EGL MVC Implementation
.NET
- ASP.NET MVC Framework
- Bistro Framework
- CodeCharge Studio
- Maverick.NET
- MonoCross
- MonoRail An ActionPack inspired MVC framework from the Castle Project
- Naked Objects MVC
- PureMVC Framework for C#
- Sharp-Architecture
- Spring Framework.NET
- Fubu MVC
- Vici MVC
Perl
- Catalyst - Mature, stable and fast MVC framework.
- CodeCharge Studio
- Dancer - micro web application framework for Perl
- Maypole - An MVC Web framework, superseded by Catalyst.
- Mojolicious - A web application framework
- PureMVC A framework for Perl.
PHP
- Agavi is a PHP 5 application framework that focuses on sustained quality and correctness.
- Alloy A lightweight REST-focused modular Hierarchical MVC PHP 5.3 framework.
- AppFlower is a Rapid Application Development framework based on MVC, PHP 5 and Symfony.
- Aura PHP is a component based MVC 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.
- AN Framework ANFire is a PHP framework that make it easy to develop build dynamic websites, web applications, and what you need in PHP, JS. (Website)
- CodeIgniter A simple, light, fast, open source MVC framework for building websites using PHP.
- DooPHP
- 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 a geographically distributed people to collaborate by sharing information over the Internet.
- FLOW3 A modern PHP framework (Website)
- Fuel is a modern open source MVC framework, using PHP 5.3, combining the best of CodeIgniter, Kohana, Rails & more.
- Helium (framework) is an open source MVC framework, using PHP 5.3, focusing on languages and a multi-site/single-engine system
- Joomla is a free and open source content management system (CMS) for publishing content on the World Wide Web and intranets and a model–view–controller (MVC) Web application framework that can also be used independently.
- KISS MVC is a simple MVC framework for developing PHP applications rapidly, based on a "Keep it simple stupid" approach.
- Kohana Framework v2.x is an open source MVC framework, while v3.x is HMVC (both supported).
- Lightweight MVC LightWeight MVC is a Open-Source GPL/Lesser GPL PHP 5 Framework that is built to teach users the basics of MVC and implementation of a MVC Powered Web Site.
- lucid-php is a lucid PHP 5 mvc framework.
- Nette Very powerful PHP framework with very good performance.[citation needed]
- Moongrace is a light footprint PHP framework with a modular approach.
- Odin Assemble A PHP-based MVC framework with a small footprint.
- phpXCore An MVC design pattern based PHP content management framework compatible with PHP 4 and PHP 5.
- PureMVC A framework for PHP.
- Horde Framework A framework and application suite for PHP
- Qcodo An open-source PHP 5 web application framework.
- SleekMVC A fast and lightweight PHP 5 MVC framework, featuring similar syntax to Kohana.
- Scriptcase Generator A powerful tool to increase web development productivity.
- SilverStripe A developer friendly CMS with its own Framework. Use the framework with or without the CMS. (Website)
- Switch board (framework) A PHP 5 MVC framework with routing.
- SIFO MVC PHP Framework with multiple DB support, Sphinx, Redis and other stuff.
- Solar framework A PHP 5 MVC framework. It is fully name-spaced and uses enterprise application design patterns, with built-in support for localization and configuration at all levels. (Website)
- sPHPf A PHP 5.3 MVC framework for fast developing and deployment. (Website)
- 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.
- ZanPHP ZanPHP is an agile Web application development framework written in PHP 5 that uses different design patterns and best practices to create applications more quickly with good quality code.
- Zend Framework An open-source PHP 5-based framework featuring a MVC layer and a broad-spectrum of loosely coupled components.
- TinyMVC Framework It is also call TyMVC. It is a light-weight and simple Open-Source (BSD licensed) PHP 5 Model-View-Controller development framework.
Python
- Django - A complete Python web application framework. Django prefers to call its MVC implementation MTV, for model-template-view.[16]
- Enthought Tool Suite - Brings the model–view–controller mindset to scientific GUIs and visualizations
- Pylons
- Pyramid
- Tornado - Lightweight framework with event-driven server
- TurboGears
- web2py - An open-source web application framework focused on rapid development
- Zope Web application server
- Plone - A content management system built on top of Zope
- PureMVC has a Python port
Ruby
- Ruby on Rails
- Merb
- Ramaze
- Camping
- Nitro
- Monkeybars
- PureMVC Framework for Ruby.
Scala
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.
- XRX-XForms, ReST, XQuery-is a web application framework where server logic is written in XQuery which processes the submitted XForm, and (typically) returns another (often generated on-the-fly) XForm to the client.
See also
- Trygve Reenskaug - first formulated the MVC 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
- Template processor
References
- ^ [1], Martin Fowler, MVC
- ^ [2], Martin Fowler, GUI Architectures
- ^ Reenskaug, Trygve. "MVC Xerox PARC 1978-79". http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html.
- ^ How to use Model–View–Controller (MVC)
- ^ Martin Fowler, "Patterns of Enterprise Application Architecture," Addison-Wesley, 2002, page 330.
- ^ Mary Shaw and David Garlan, "Software Architecture: Perspectives on an Emerging Discipline," Prentice Hall, 1996.
- ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
- ^ E. Curry and P. Grace, “Flexible Self-Management Using the Model-View-Controller Pattern,” IEEE Software, vol. 25, no. 3, pp. 84-90, May. 2008.
- ^ Sagar Vasekar; Michael Rimov; Larry Hamel; et. al. (2004-12). "Expresso Developer's Guide". http://www.jcorporate.com/expresso/doc/edg/edg_WhatIsMVC.html: Jcorporate. "Flexibility in large component based systems raise questions on how to organize a project for easy development and maintenance while protecting your data and reputation, especially from new developers and unwitting users. The answer is in using the Model, View, Control (MVC) architecture."
- ^ Stearns, Beth (2002). Designing Enterprise Applications with the J2EE™ Platform (2nd Edition). Prentice Hall. ISBN 0201787903. http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/DEA2eTOC.html.
- ^ http://spinejs.com/
- ^ http://batmanjs.org/
- ^ http://dod.codeplex.com/
- ^ "Product Review: WaveMaker’s point-and-click Java". Infoworld. April 24, 2008. http://www.infoworld.com/article/08/04/17/16TC-wavemaker-studio_1.html. Retrieved 2008-04-25.
- ^ http://spinejs.com/
- ^ 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
- MVC Architecture in .Net
- An overview of the MVC pattern in Java from the Sun website and details of MVC design in Java Swing
- Java SE Application Design With MVC by Robert Eckstein, March 2007
- Model View Presenter with ASP.NET CodeProject article.
- History of the evolution of MVC and derivatives by Martin Fowler.
- Holub, Allen (1999). "Building user interfaces for object-oriented systems". Java World. http://www.javaworld.com/javaworld/jw-07-1999/jw-07-toolbox.html.
- Model View Controller in PHP A simple introduction to MVC pattern in PHP.
- Model View Controller - overview page in the Portland Pattern Repository
- Backbone.js A simple introduction to Backbone.js
- Model - Display view - Picking view - Controller (MDPC), an architecture for graphical interactive software aiming at improving further modularity and usability of programming
- Is MVC a design pattern or architectural pattern?, Discussion around MVC as a design pattern.
- Model View Controller, ASP.net 4.0 tutorial on MVC.