Model-view-controller

From Wikipedia, the free encyclopedia

(Redirected from Model View Controller)
Jump to: navigation, search
A simple diagram depicting the relationship between the Model, View, and Controller. Note: the solid lines indicate a direct association, and the dashed lines indicate an indirect association (e.g., observer pattern).
A simple diagram depicting the relationship between the Model, View, and Controller. Note: the solid lines indicate a direct association, and the dashed lines indicate an indirect association (e.g., observer pattern).

Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface. The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller.

Contents

The pattern was first described in 1979[1] by Trygve Reenskaug, then working on Smalltalk at Xerox research labs. The original implementation is described in depth in the influential paper Applications Programming in Smalltalk-80(TM):How to use Model-View-Controller[2].

It is common to split an application into separate layers: presentation (UI), domain logic, and data access. In MVC the presentation layer is further separated into view and controller. MVC encompasses more of the architecture of an application than is typical for a design pattern.

Model
The domain-specific representation of the information that 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.

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 XML files.

Though MVC comes in different flavors, control flow generally works as follows:

  1. The user interacts with the user interface in some way (e.g., presses a button).
  2. A controller handles the input event from the user interface, often via a registered handler or callback.
  3. The controller accesses the model, possibly updating it in a way appropriate to the user's action (e.g., controller updates user's Shopping cart).[3]
  4. 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.
  5. 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.

Java Swing is different from the other frameworks, in that it supports two MVC patterns.

Model (Frame level)
Like the other frameworks, the design of the real model is usually left to the developer.
Model (Control level)
Swing also supports models on the control level. 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. It catches and propagates the events to the view and model.

In WinForms, a .NET framework, the patterns for the view and controller are well defined. The model is left to the developer to design.

Model
Just like ASP.Net, WinForms does not strictly require a model. The developer has the option to create a model class, but may choose to forget it and have the event handlers in the controller perform any calculations and data persistence. Again, using a model to encapsulate business rules and database access is both possible and preferable. It is left to developers to design the Model.
View
A class inheriting from either Form or Control handles the responsibilities of the view. In the case of WinForms, the View and Controller are compiled into the same class. This differs from ASP.Net, which uses inheritance, and Smalltalk, which have separate classes with pointers to one another.
Controller
The duties of the controller are split among three places. The generation and passing of events starts at the OS level. Inside the .Net framework, the Form and Control classes route the event to the proper event handler. The event handlers typically reside in the Form or Control class and are implemented as delegates for the user interface events (e.g., button click, form load, listbox selection changed).

In ASP.NET, the patterns for the view and controller are not well defined. The model is left to the developer to design, while the views and controllers can be created in a variety of ways. An ASP.NET MVC framework has been announced as being in development and should be available the first half of 2008.[1]

Model
DataSets and DataTables are the most common use of the model in .NET projects. A typed DataSet allows one to create an entity-specific model based on a database.
View
The ASPX and ASCX files generally handle the responsibilities of the view, although it can also come from compiled server controls. With this pattern, the view object may choose to inherit from the controller object. This is different from the Smalltalk implementation, in which separate classes have pointers to one another. A more flexible option is to have the code-behind classes of the ASPX and ASCX to implement an Interface for a View, which can then be passed to the Controller. With this approach, the Controller is loosely coupled to and largely unaware of the actual concrete implementer of the View interface, be it an ASPX or an ASCX.
Controllers
The duties of the controller are split between two places. The generation and passing of events is part of the framework and more specifically the Page and Control classes. The handling of events is usually done in the code-behind class. However, moving code specific to the transition between views in a separate Controller is a good practice. In turn, it becomes possible to centralize the registration of Observers in the isolated Controller.

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). Alternately, 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 Technology (JSF).

Smalltalk's MVC implementation inspired many other GUI frameworks, such as the following:

In the design of web applications, MVC is implemented by web template systems as "View for web" component.

MVC is also known as a "Model 2" architecture in Sun parlance. Complex web applications continue to be more difficult to design than traditional applications, and MVC is being pushed as a potential solution to these difficulties.

  • ARP Usable with AIR, Flex, and Flash
  • Cairngorm Usable with AIR, Flex, and Flash
  • PureMVC Suitable for use with AIR, Flex, Flash, and any platform running AS3 (no non-native class dependencies)

MVC web application frameworks:

  • AJILE Cross-browser compatible (ECMAScript-262, edition 3, JavaScript 1.3+, JScript 3.0+)
  • Archetype Javascript Framework for structuring and ease development of RIA.
  • JavaScript MVC An open-source JavaScript framework for developing web applications.

  • Catalyst An MVC-based avant-garde web framework.
  • CGI::Application A mature, lightweight, flexible MVC framework for web application development.
  • Gantry Framework A web application framework for Apache/mod_perl, CGI and Fast-CGI.
  • Jifty A full-stack application framework.
  • Maypole A Perl framework for MVC-oriented web applications, similar to Jakarta's Struts
  • OpenInteract2 is a web application server written in Perl. It features integrated data persistence, security, user and group management, plus an easy way to create and distribute fully database-independent applications.
  • PageKit A mod_perl based web application framework that uses a template system and XML.

  • Agavi an open-source, LGPL licensed MVC framework for creating applications written using PHP5.
  • Akelos PHP Framework a Ruby on Rails port to PHP4/5.
  • BareBonesMVC A one-file, no-configuration, MVC framework for PHP5.
  • CakePHP webapplication framework modeled after the concepts of Ruby on Rails.
  • CodeIgniter A PHP MVC framework.
  • DragonPHP MVC2 Framework for PHP 5.
  • Fusebox Framework for building ColdFusion and PHP web applications.
  • FUSE A powerful but easy-to-use PHP 5 Framework for MVC development
  • KohanaPHP A powerful, lightweight, easily extendable PHP 5 MVC Framework.
  • LightVC Lightweight PHP 5 Strict MVC Framework with decoupling of Model and other non-View-Controller essentials to promote code reuse.
  • 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.
  • SilverStripe contains a fully fledged PHP 5.2 ORM/MVC Framework focused on building websites.
  • Solar PHP 5 framework Solar is a PHP 5 framework for rapid application development. It is fully name-spaced and uses enterprise application design patterns, with built-in support for localization and configuration at all levels.
  • Switch board with Routing PHP 5 MVC Framework with Routing.
  • Symfony Framework PHP 5 MVC Framework.
  • TinyMVC Framework Simple and lightweight PHP5 MVC (Model-View-Controller) framework.
  • TYPO3 extension library lib/div PHP 4/5 MVC framework for TYPO3 extension development
  • 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.

  • Django A complete Python web application framework. Django prefers to call its MVC implementation MTV, for Model-Template-View [2].
  • Gluon A full-stack python web application framework
  • Pylons - Python Web Framework
  • TurboGears for Python
  • Spring Python

  1. ^ http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
  2. ^ http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html
  3. ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.

General information regarding MVC

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.