![Model View Presenter GUI Design Pattern](https://upload.wikimedia.org/wikipedia/commons/d/dc/Model_View_Presenter_GUI_Design_Pattern.png)
Model–view–presenter (MVP) is a derivation of the
model–view–controller
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of infor ...
(MVC)
architectural pattern
An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware perform ...
, and is used mostly for building user interfaces.
In MVP, the ''presenter'' assumes the functionality of the "middle-man". In MVP, all presentation logic is pushed to the presenter.
History
The model-view-presenter software pattern originated in the early 1990s at
Taligent
Taligent Inc. (a portmanteau of "talent" and "intelligent") was an American software company. Based on the Pink object-oriented operating system conceived by Apple in 1988, Taligent Inc. was incorporated as an Apple/IBM partnership in 1992, and ...
, a joint venture of
Apple
An apple is an edible fruit produced by an apple tree (''Malus domestica''). Apple fruit tree, trees are agriculture, cultivated worldwide and are the most widely grown species in the genus ''Malus''. The tree originated in Central Asia, wh ...
,
IBM, and
Hewlett-Packard
The Hewlett-Packard Company, commonly shortened to Hewlett-Packard ( ) or HP, was an American multinational information technology company headquartered in Palo Alto, California. HP developed and provided a wide variety of hardware components ...
. MVP is the underlying programming model for application development in Taligent's
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
-based CommonPoint environment. The pattern was later migrated by Taligent to
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
and popularized in a paper by Taligent CTO Mike Potel.
After Taligent's discontinuation in 1998, Andy Bower and Blair McGlashan of
Dolphin Smalltalk
Dolphin Smalltalk, or "Dolphin" for short, is an implementation of the Smalltalk programming language for Microsoft Windows.
The Dolphin 7 version release coincided with the project going open-source using the MIT License
The MIT License is ...
adapted the MVP pattern to form the basis for their Smalltalk user interface framework. In 2006,
Microsoft
Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
began incorporating MVP into its documentation and examples for user interface programming in the
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
.
The evolution and multiple variants of the MVP pattern, including the relationship of MVP to other design patterns such as MVC, is discussed in detail in an article by
Martin Fowler
and another by Derek Greer.
Overview
MVP is a user interface
architectural pattern
An architectural pattern is a general, reusable solution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware perform ...
engineered to facilitate
automated
Automation describes a wide range of technologies that reduce human intervention in processes, namely by predetermining decision criteria, subprocess relationships, and related actions, as well as embodying those predeterminations in machines ...
unit testing
In computer programming, unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures&md ...
and improve the
separation of concerns
In computer science, separation of concerns is a design principle for separating a computer program into distinct sections. Each section addresses a separate '' concern'', a set of information that affects the code of a computer program. A concern ...
in presentation logic:
* The ''model'' is an interface defining the data to be displayed or otherwise acted upon in the user interface.
* The ''view'' is a passive interface that displays data (the model) and routes user commands (
event
Event may refer to:
Gatherings of people
* Ceremony, an event of ritual significance, performed on a special occasion
* Convention (meeting), a gathering of individuals engaged in some common interest
* Event management, the organization of e ...
s) to the presenter to act upon that data.
* The ''presenter'' acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.
Normally, the view implementation
instantiates the concrete presenter object, providing a reference to itself. The following
C# code demonstrates a simple view constructor, where implements the interface:
public class DomainView : IDomainView
The degree of logic permitted in the view varies among different implementations. At one extreme, the view is entirely passive, forwarding all interaction operations to the presenter. In this formulation, when a user triggers an event method of the view, it does nothing but invoke a method of the presenter that has no parameters and no return value. The presenter then retrieves data from the view through methods defined by the view interface. Finally, the presenter operates on the model and updates the view with the results of the operation. Other versions of model-view-presenter allow some latitude with respect to which class handles a particular interaction, event, or command. This is often more suitable for web-based architectures, where the view, which executes on a client's browser, may be the best place to handle a particular interaction or command.
From a layering point of view, the presenter class might be considered as belonging to the application layer in a
multilayered architecture
In software engineering, multitier architecture (often referred to as ''n''-tier architecture) is a client–server architecture in which presentation, application processing and data management functions are physically separated. The most wide ...
system, but it can also be seen as a presenter layer of its own between the application layer and the
user interface
In the industrial design field of human–computer interaction, a user interface (UI) is the space where interactions between humans and machines occur. The goal of this interaction is to allow effective operation and control of the machine f ...
layer.
Implementations
.NET
The
.NET environment supports the MVP pattern much like any other development environment. The same model and presenter class can be used to support multiple interfaces, such as an
ASP.NET
ASP.NET is an open-source, server-side web-application framework designed for web development to produce dynamic web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services. The name s ...
Web application, a
Windows Forms
Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .NET, .NET Framework or Mono Framework, providing a platform to write client applications for desktop, laptop, and tablet PCs. While ...
application, or a
Silverlight
Microsoft Silverlight is a discontinued application framework designed for writing and running rich web applications, similar to Adobe Inc., Adobe's Run time environment, runtime, Adobe Flash. A plugin for Silverlight is still available for a v ...
application. The presenter gets and sets information from/to the view through an interface that can be accessed by the interface (view) component.
In addition to manually implementing the pattern, a model-view-presenter framework may be used to support the MVP pattern in a more automated fashion.
Java
In a
Java
Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
(
AWT/
Swing/
SWT) application, the MVP pattern can be used by letting the user interface class implement a view interface.
The same approach can be used for Java web-based applications, since modern Java component-based Web frameworks allow development of client-side logic using the same component approach as thick clients.
Implementing MVP in
Google Web Toolkit
Google Web Toolkit (GWT ), or GWT Web Toolkit, is an open-source set of tools that allows web developers to create and maintain JavaScript front-end applications in Java. It is licensed under the Apache License 2.0.
GWT emphasizes reusable ...
requires only that some component implement the view interface. The same approach is possible using
Vaadin Vaadin () is an open-source web application development platform for Java. Vaadin includes a set of Web Components, a Java web framework, and a set of tools that enable developers to implement modern web graphical user interfaces (GUI) using the Jav ...
or the
Echo2 Web framework.
Java frameworks include the following:
*
JavaFX
JavaFX is a software platform for creating and delivering desktop applications, as well as rich web applications that can run across a wide variety of devices. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linu ...
*
Echo2
*
Google Web Toolkit
Google Web Toolkit (GWT ), or GWT Web Toolkit, is an open-source set of tools that allows web developers to create and maintain JavaScript front-end applications in Java. It is licensed under the Apache License 2.0.
GWT emphasizes reusable ...
*
JFace
JFace is defined by the Eclipse project as "a UI toolkit that provides helper classes for developing UI features that can be tedious to implement."
The Standard Widget Toolkit (SWT) is an open source widget toolkit for Java designed to provid ...
*
Swing
*
Vaadin Vaadin () is an open-source web application development platform for Java. Vaadin includes a set of Web Components, a Java web framework, and a set of tools that enable developers to implement modern web graphical user interfaces (GUI) using the Jav ...
*
ZK
PHP
As of
PHP
PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
's flexible runtime environment, there are wide possibilities of approaches of an application logic. Implementation of model layer is left on the end application programmer.
PHP frameworks include the following:
*
CodeIgniter
CodeIgniter is an open-source software rapid development web framework, for use in building dynamic web sites with PHP.
Popularity
CodeIgniter is loosely based on the popular model–view–controller (MVC) development pattern. While controller c ...
*
Laravel
Laravel is a free and open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern and based on Symfony. Some of the features ...
*
Nette Framework
Nette Framework is an open-source framework for creating web applications in PHP. It supports AJAX, DRY, KISS, MVC and code reusability. The original author of the framework is David Grudl, but further development is now maintained by the ...
See also
*
Multitier architecture
In software engineering, multitier architecture (often referred to as ''n''-tier architecture) is a client–server architecture in which presentation, application processing and data management functions are physically separated. The most wide ...
*
Model–view–controller
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements. This is done to separate internal representations of infor ...
*
Model–view–viewmodel
Model–view–viewmodel (MVVM) is an architectural pattern in computer software that facilitates the separation of the development of the graphical user interface (GUI; the ''view'')—be it via a markup language or GUI code—from the develo ...
*
Presenter first (software approach)
References
External links
Model View Presenter Example from the GWT project.Model-View-Presenter implementation thoughts at StackExchange
{{DEFAULTSORT:Model-view-presenter
Software design patterns
Architectural pattern (computer science)