Model–view–controller: Difference between revisions
Robert K S (talk | contribs) |
m + uk |
||
Line 290: | Line 290: | ||
[[fi:MVC-arkkitehtuuri]] |
[[fi:MVC-arkkitehtuuri]] |
||
[[sv:Model-View-Controller]] |
[[sv:Model-View-Controller]] |
||
[[uk:Model-view-controller]] |
|||
[[zh:MVC]] |
[[zh:MVC]] |
Revision as of 12:56, 29 June 2007
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.
Pattern description
It is common to split an application into separate layers: presentation (UI), domain, 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 on which the application operates. It is a common misconception that the model is another name for the domain layer. Domain logic adds meaning to raw data (e.g., calculating if 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.
- 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 which 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:
- 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 accesses the model, possibly updating it in a way appropriate to the user's action (e.g., controller updates user's shopping cart).[1]
- A view uses the model 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.
Implementations
The pattern was first described in 1979[2] 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[3].
Smalltalk's MVC implementation inspired many other GUI frameworks such as:
- Adobe AS2/3.
- The NeXTSTEP and OPENSTEP development environments encourage the use of MVC. Cocoa and GNUstep, based on these technologies, also use MVC.
- The Core Data framework in Mac OS X, a modern addition to Cocoa.
- Microsoft Foundation Classes (MFC) (called Document/View architecture here)
- Java Swing
- JFace
- The Qt Toolkit since Qt4 Release.
- XForms has a clear separation of model (stored inside the HTML head section) from the presentation (stored in the HTML body section). XForms uses simple bind commands to link the presentation to the model.
- Web template systems (see list of implementation on article) are widely used on web and other contexts. It is a kind of "view subsystem" in a MVC
- Business Server Pages (BSP) in SAP
- Joomla 1.5 (CMS)
- Ruby on Rails
- Zend Framework
- Groovy on Rails (Grails)
- Bindows Framework
- Maverick
MVC in various languages
TROIKA.ASP Framework is Model-View-Controller (Model 2) web development framework for ASP 3.0. It uses OO JavaScript for Model and Controller logic and XSLT transformation templates for the View.
In ASP.NET, the patterns for the view and controller are not well defined. The model is left to the developer to design, views and controls can be created in a variety of ways.
- Model
- DataSets are the most common use of the model in .Net projects. A typed DataSet allows one to create an entity specific model.
- 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 inherits from the controller object. This is different from the Smalltalk implementation, in which separate classes have pointers to one another.
- 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.
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 between 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 handling of events is usually done in the code-behind class how it can be changed.
.NET MVC Frameworks
- MonoRail is a MVC Web Framework inspired by Action Pack from Ruby on Rails.
- The Spring.NET is an application framework focused on helping build enterprise .NET applications.
Java Platform, 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. 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.
Java MVC Frameworks
- Struts is one of the original web-application frameworks which makes extensive use of MVC logic.
- WebWork is a MVC Java web-application development framework. It is built specifically with developer productivity and code simplicity in mind, providing robust support for building reusable UI templates, such as form controls, UI themes, internationalization, dynamic form parameter mapping to JavaBeans, robust client and server side validation, and much more.
- The Spring Framework is a newer Java EE application framework for both native Java applications as well as Java-served web-applications. Spring utilizes a multi-tier approach to java applications. One project within Spring is a web based MVC framework named "Spring MVC".
- Roma is based on Spring Framework as IoC container, but you can use another one if you want.
- Java Server Faces (JSF) framework is the Java EE standard web-application framework.
- Swing (see below)
- JFace, a MVC framework built on the Eclipse project's SWT set of operating-system-native controls
- Openbravo, a web based ERP for small to medium sized business
- Stripes, a light-weight yet full featured MVC framework that utilizes Java 1.5+ enhancements like annotations to simplify configuration and validation. Strong type conversion and a powerful binding engine allow complex objects to be displayed and edited in the html view. Add-on libraries like Stripernate help bind the framework to Hibernate's EJB3 persistence layer.
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.
The Nitro and Ruby on Rails web frameworks are popular Model-view-controller architectures for Ruby.
Python has many MVC frameworks. The two most popular frameworks are Django and TurboGears, but Pylons is gaining in popularity.
The most popular MVC for Perl is Catalyst. Catalyst borrows from other frameworks such as Ruby on Rails and Apache Struts. It makes extensive use of the CPAN archive to provide the various components as follows.
- Model
- Catalyst is very flexible on the Model, examples being DBIx::Class, Class::DBI, Class::DBI::SQLite
- View
- CPAN modules such as Template Toolkit, Mason, HTML::Template, XSLT and Petal can all be used as the View.
- Controller
- Catalyst uses advanced URL to action dispatching to map the URL to the correct controller.
There are many different MVC frameworks for PHP, some of which seek to imitate framework features from Java, .NET, and elsewhere. PHP Frameworks like CakePHP, Symfony, PHPOnTrax, PHPMVC and CodeIgniter implement the MVC pattern. Often the Smarty template language is used to separate presentation from program logic. Zend, the primary company behind the development of PHP, recently began developing an MVC framework for PHP. Symfony, cakePHP or Achievo ATK are newer rapid/development MVC frameworks that mimic some of the best features on Rails and Django. Joomla version 1.5 uses an MVC model throughout its web-application framework.
PHP is the most popular language for creating Content Management Systems and some of the CMSs implement MVC. The popular Drupal CMS is an example of optional MVC. The core system separates the data, processes, and presentation but still lets users create shortcuts to the data when developing new features. Optional MVC provides the advantages of MVC while allowing developers to plug in external modules that are not yet MVC.
ColdFusion also does not have a built in MVC structure, but has many third-party frameworks such as Model-Glue, Fusebox and Mach-II which stress the MVC ideal
Architectures for Web-based Interfaces
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.
MVC frameworks in ActionScript
- ARP Usable with Apollo, Flex, and Flash
- Cairngorm Usable with Apollo, Flex, and Flash
- PureMVC Suitable for use with Apollo, Flex, Flash, and any platform running AS3 (no non-native class dependencies)
MVC frameworks in Perl
- Catalyst An MVC-based avant-garde web framework.
MVC frameworks in PHP
- Zend Framework A PHP 5 based MVC framework.
- Zoop Framework A Mature PHP 4/5 MVC framework.
- MVCnPHP Lightweight yet flexible PHP5 MVC Implementation.
- Symfony Framework PHP 5 MVC Framework.
- Switch board with Routing PHP 5 MVC Framework with Routing.
- CakePHP webapplication framework modeled after the concepts of Ruby on Rails.
- Odin Assemble Small footprint PHP based MVC Framework.
- phpXCore A MVC design pattern based PHP content management framework compatible with PHP4 and PHP5.
- Akelos Framework A PHP version of Ruby on Rails MVC Framework.
- CodeIgniter A PHP MVC framework.
- Copix PHP Framework
- DragonPHP
- MvcSkel web development framework
- PHOCOA A PHP MVC framework.
MVC frameworks in Python
- Django A complete Python web application framework
- Pylons - Python Web Framework
- TurboGears for python, with AJAX
MVC frameworks in .NET
- Maverick.NET [1]
- Microsoft UIP
- Monorail An ActionPack inspired MVC framework from the Castle Project
- Igloo software (Experimental) MVC Framework for .Net 2.0
MVC frameworks in other languages
- AJILE - A JavaScript module that supports MVC via the separation of HTML and JavaScript.
- Apache Cocoon - use the pyramid of contracts concept, to the separation of content, style, logic and management functions.
- ASWing An opensource actionscript2 (Flash) implementation of the Java Swing library.
- Camping is a Microframework
- Catalyst
- Ingenious MVC .Net MVC
- Fusebox
- JavaServer Faces, Apache Struts and Webwork2 are currently the most popular web oriented MVC implementations.
- Joomla! v 1.5 A PHP MVC application framework
- Mach-II
- Maypole
- Model-Glue for ColdFusion
- Nitro (software)
- OpenXava Business component oriented as alternative to classical MVC frameworks
- Orbeon
- PHPwact MVC frameworks written in PHP (Wiki)
- Phrame (software)
- PHP on Trax
- PRADO (software) a component-based and event-driven framework
- Radicore For either PHP 4 or 5
- Ruby on Rails
- Sanders - MVC framework based on Mojavi 3
- Smart3
- Solarphp PHP framework under development
- Solstice - An MVC-based Perl web application framework.
- Spring Framework
- Spring MVC
- Home Stripes
- Struts
- Switch board PHP 5 MVC Framework with Routing
- Tapestry
- The WebObjects development/deployment environment is strongly based on MVC.
- Wicket
- Witango rapid application development tool based on XML and a C++ server engine. Easily ports code to Java.
- XForms is an approach on building web based applications based on MVC, under development by W3C
- Zend Framework A light-weight framework for PHP.
- ZNF
See also
- Trygve Reenskaug - first formulated the model-view-controller pattern
- Architectural patterns
- Model 1
- Three-tier (computing)
- Model-View-Presenter (MVP) design pattern
- The Observer design pattern
- The Template Method design pattern
- The Presentation-abstraction-control (PAC) pattern (a strict version of the Hierarchical-Model-View-Controller (HMVC))
References
- ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.
- ^ http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html
- ^ http://st-www.cs.uiuc.edu/users/smarch/st-docs/mvc.html
External links
This page or section may contain link spam masquerading as content. |
This article's use of external links may not follow Wikipedia's policies or guidelines. (May 2007) |
General information regarding MVC
- An Alternative Explanation of MVC by Apple.
- An overview of the MVC pattern in Java from the Sun website
- MVC Description in the Portland Pattern Repository
- Discussion of JavaServer Pages Model 2 architecture
- Model View Controller Overview
- Presentation
- Model View Controller Song from WWDC 2003
- Planet MVC
- Model View Presenter with ASP.NET CodeProject article.
- A Java implementation example of MVC
Specific aspects of MVC or alternatives to MVC
- Core J2EE Patterns - Front Controller
- Holub, Allen (1999). "Building user interfaces for object-oriented systems". Java World.
- Gresh, John E. (2004). "The Collection Switch Design Pattern" (PDF). Rensselaer Computer Science Seminar, 2004.
- MVC versus "Event Driven" Discussion
- What's a Controller Anyway
- GUI Architectures article by Martin Fowler.