Model-view-controller
From Wikipedia, the free encyclopedia
| This article may require cleanup to meet Wikipedia's quality standards. Please improve this article if you can. (August 2007) |
| The external links in this article may not follow Wikipedia's content policies or guidelines. Please improve this article by removing excessive or inappropriate external links. |
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 and the business rules used to manipulate the data, the view corresponds to elements of the user interface such as text, checkbox items, and so forth, and the controller manages details involving the communication to the model of user actions such as keystrokes and mouse movements.
Contents |
[edit] History
The pattern 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]
After that numerous derivatives of the MVC pattern appeared. Probably one of the most known of them is the Model View Presenter pattern, which appeared in the early 90s and was designed to be an evolution of MVC. However Model-View-Controller still remains very popular and widely used.
[edit] Pattern description
Model-view-controller is both an architectural pattern and a design pattern, depending on where it is used.
[edit] As an architectural pattern
It is common to split an application into separate layers that run on different computers: presentation (UI), domain logic, and data access. In MVC the presentation layer is further separated into view and controller.
MVC is often seen in web applications, where the view is the actual HTML page, and the controller is the code that gathers dynamic data and generates the content within the HTML. Finally, the model is represented by the actual content, usually 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 generally works as follows:
- The user interacts with the user interface in some way (e.g. presses a button).
- A 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. (e.g. controller updates user's Shopping cart).[3]
- A view uses the model (indirectly) to generate an appropriate user interface (e.g. the view produces a screen listing the shopping cart contents). The view gets its own data from the model. The model has no direct knowledge of the view.
- The user interface waits for further user interactions, which begins the cycle anew.
By decoupling models and views, MVC helps to reduce the complexity in architectural design, and to increase flexibility and reuse.
[edit] As a design pattern
MVC encompasses more of the architecture of an application than is typical for a design pattern.
- Model
- The domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (e.g., 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 invoke changes on the model.
[edit] Selected frameworks
[edit] GUI frameworks
[edit] 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 necessarily use a single controller. Because its event model is based on interfaces, it is common to create an anonymous action class for each event. 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.
[edit] Combined frameworks
[edit] Java: Java Enterprise Edition (Java EE)
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 Java Server 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).
[edit] Implementations of MVC as GUI frameworks
Smalltalk's MVC implementation inspired many other GUI frameworks, such as the following:
- Core Data framework in Mac OS X, a modern addition to Cocoa.
- GNUstep, based on these technologies, also use MVC.
- GTK+
- JFace
- Microsoft Foundation Classes (MFC) (called Document/View architecture here)
- NeXTSTEP development environments encourage the use of MVC. Cocoa
- OPENSTEP development environments encourage the use of MVC. Cocoa
- Qt Toolkit since Qt4 Release.
- Java Swing
- Windows Presentation Foundation (WPF) uses a similar Model-view-viewmodel pattern.
Visual FoxExpress is a Visual FoxPro MVC framework.
[edit] Implementations of MVC as web-based frameworks
In the design of web applications, MVC is implemented by web template systems as "View for web" component.
MVC is typically implemented as a "Model 2" architecture in Sun parlance. Model2 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.
[edit] .NET
- ASP.NET MVC Framework
- Maverick.NET
- Monorail An ActionPack inspired MVC framework from the Castle Project
[edit] ColdFusion
- Mach-II A framework that focuses on trying to ease software development and maintenance
[edit] Java
MVC web application frameworks:
- Aranea
- Cocoon
- Grails
- Oracle Application Framework
- Spring MVC Framework
- Struts
- Tapestry
- WebObjects
- WebWork
- Wicket
- JSF
- Web Dynpro ABAP
[edit] Informix 4GL
- Informix 4GL MVC models to use for Informix 4GL report and form creation
- EGL – IBM's EGL MVC Implementation
[edit] Perl
- Catalyst An MVC-based avant-garde web framework.
[edit] PHP
- Akelos PHP Framework a Ruby on Rails port to PHP4/5.
- CakePHP webapplication framework modeled after the concepts of Ruby on Rails.
- CodeIgniter A PHP MVC framework.
- Drupal Community plumbing: open source modular framework and content management system.
- FUSE A powerful but easy-to-use PHP 5 Framework for MVC development
- Jelix Framework an open source PHP 5 MVC framework designed for highly performance.
- Joomla an open source PHP 5 MVC CMS
- KohanaPHP A powerful, lightweight, easily extendable PHP 5 MVC Framework.
- Odin Assemble Small footprint PHP based MVC Framework.
- phpXCore A MVC design pattern based PHP content management framework compatible with PHP4 and PHP5.
- PRADO A PHP 5 MVC framework.
- Qcodo is an open-source PHP 5 web application framework
- SilverStripe contains a fully fledged PHP 5.2 ORM/MVC Framework focused on building websites.
- Switch board (framework) with Routing PHP 5 MVC Framework with Routing.
- Symfony Framework PHP 5 MVC Framework.
- Zend Framework A PHP 5-based MVC framework.
- ZNF PHP5 MVC framework for enterprise web applications
- Zoop Framework A Mature PHP 4/5 MVC framework.
[edit] Python
- Django A complete Python web application framework. Django prefers to call its MVC implementation MTV, for Model-Template-View.
- Pylons - Python Web Framework
- TurboGears for Python
- Zope Content Management Framework
[edit] Ruby
[edit] 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
[edit] References
- ^ http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
- ^ How to use Model-View-Controller (MVC)
- ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
[edit] External links
General information regarding MVC
- 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.

