Model-view-controller
From Wikipedia, the free encyclopedia
Model-View-Controller (MVC) is a design pattern used in software engineering. In complex computer applications that present lots of data to the user, one often wishes to separate data (Model) and user interface (View) concerns, so that changes to the user interface do not impact the data handling, and that the data can be reorganized without changing the user interface. The Model-View-Controller design pattern solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the Controller.
Contents |
[edit] 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. Hence the term architectural pattern may be useful [1], or perhaps an aggregate design pattern.
- Model
- The domain-specific representation of the information on which the application operates. 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. MVC is often seen in web applications, where the view is the HTML page and the code which gathers dynamic data for the page.
- Controller
- Processes and responds to events, typically user actions, and may invoke changes on the model.
Though MVC comes in different flavors, control flow generally works as follows:
- The user interacts with the user interface in some way (e.g., user 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).[2]
- A view uses the model to generate an appropriate user interface (e.g., 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. (However, the observer pattern can be used to allow the model to indirectly notify interested parties – potentially including views – of a change.)
- The user interface waits for further user interactions, which begins the cycle anew.
[edit] Implementations
The pattern was first described in 1979 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.
Smalltalk's MVC implementation inspired many other GUI frameworks such as:
- 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 is the most powerful MVC implementation for the Apple Macintosh to date. It is a modern addition to Cocoa frameworks and is fully supported by the latest Xcode IDE.
- Microsoft Foundation Classes (MFC) (called Document/View architecture here)
- The Java Swing
- 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.
[edit] MVC in various languages
[edit] .NET
.
[edit] ASP.NET
In ASP.NET, the patterns for the view and controller are well defined. The model is left to the developer to design.
- Model
- ASP.NET 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. That said, using a model to encapsulate business rules and database access is both possible and preferable.
- View
- The ASPX and ASCX files handle the responsibilities of the view. With this design, the view object actually inherits from the controller object. This is different from the Smalltalk implementation, in which separate classes have pointers to one another.
- Controller
- 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 centralizing the registration of Observers in the isolated Controller.
[edit] Windows Forms
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.
- 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 other
- 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.
[edit] Java
[edit] Java Platform, Enterprise Edition (Java EE)
Unlike the other frameworks, Java EE defines a pattern for model objects.
- Model
- The model is represented by entity beans.
- 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.
[edit] Java MVC Frameworks
- Struts is one of the original web-application frameworks which makes extensive use of MVC logic.
- The Spring Framework is a newer (as of late 2006) J2EE application framework for both native Java applications as well as Java-served web-applications. Spring utilizes a multi-tier approach to java applications. One of the layers enables struts-like MVC implementations.
- Java Server Faces (JSF) framework is one of the most advanced, Java standardized web-application framework.
[edit] Java Swing
Java Swing is different from the other frameworks, in that it actually 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.
[edit] Ruby
The Ruby on Rails framework is a popular Model-view-controller architecture for Ruby.
[edit] Python
Python has many MVC frameworks. The two most popular frameworks are Django and TurboGears.
[edit] Perl
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 also very flexible on the Model, examples being DBIx::Class, Class::DBI, Class::DBI::SQLite
- View
CPAN modules such as Template Toolkit, Mason, HTML::Template 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.
[edit] PHP
There are many different MVC frameworks for PHP, some of which seek to imitate framework features from Java, .NET, and elsewhere. Often the Smarty template language is used to separate presentation from program logic. Zend, the company that develops PHP, recently began developing a MVC framework for PHP.
[edit] ColdFusion
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
[edit] 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.
- 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.
- Blokz A PHP + PostgreSQL MVC Framework.
- CakePHP A MVC and Active Records design pattern based PHP framework
- Catalyst
- Code Igniter A PHP MVC framework
- Copix - PHP Framework
- Django
- DragonPHP
- Ingenious MVC .Net MVC
- Fusebox
- JavaServer Faces, Apache Struts and Webwork2 are currently the most popular web oriented MVC implementations.
- Mach-II
- Maverick for .Net
- Maypole
- Microsoft UIP
- Model-Glue for ColdFusion Link
- MonoRail An ActionPack inspired MVC framework from the Castle Project
- Nitro
- OpenXava Business component oriented as alternative to classical MVC frameworks
- Orbeon
- MVC frameworks written in PHP (Wiki)
- Phrame
- PHP on Trax
- Radicore For either PHP 4 or 5
- Ruby on Rails
- Smart3
- Solarphp - PHP framework under development
- Solstice - An MVC-based Perl web application framework.
- Spring Framework
- Stripes
- Struts
- Symfony
- Tapestry
- TurboGears for python, with AJAX
- The WebObjects development/deployment environment is strongly based on MVC.
- Wicket
- 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
- Zoop Framework A PHP Framework
[edit] See also
- Trygve Reenskaug
- Design patterns
- Model 1
- Three-tier (computing)
- The Observer design pattern
- The Template Method design pattern
- The Presentation-abstraction-control pattern
[edit] External links
General information regarding MVC
- 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
Specific aspects of MVC or alternatives to MVC
- Core J2EE Patterns - Front Controller
- Holub, Allen. "Building user interfaces for object-oriented systems", Java World, 1999.
- Gresh, John E.. "The Collection Switch Design Pattern", Rensselaer Computer Science Seminar, 2004, 2004.
- MVC versus "Event Driven" Discussion
- What's a Controller Anyway
MVC frameworks in PHP
- Zend Framework A PHP 5 based MVC framework.
- Symfony Framework PHP 5 MVC Framework Symfony Framework
- CakePHP, webapplication framework modeled after the concepts of Ruby on Rails.
- PHP on Trax (PDF), a presentation by John Peterson, creator of Trax
- MVC Frameworks for PHP (PDF), a presentation on general MVC principles, especially as applied to PHP
- The Model-View-Controller Design Pattern for PHP an implementation which utilises reusable XSL stylesheets for the View.
- Odin Assemble Small footprint PHP based MVC Framework
[edit] References
- ^ Frank Buschmann, Regine Meunier, Hans Rohnert, Peter Sommerlad, Michael Stal (1996). Pattern-Oriented Software Architecture. John Wiley and Sons. ISBN 0-471-95869-7.
- ^ Complex controllers are often structured using the command pattern to encapsulate actions and simplify extension.