Model View Presenter
Encyclopedia
Model–view–presenter is a derivative of the model–view–controller (MVC) software pattern, also used mostly for building user interfaces
User interface
The user interface, in the industrial design field of human–machine interaction, is the space where interaction between humans and machines occurs. The goal of interaction between a human and a machine at the user interface is effective operation and control of the machine, and feedback from the...

.

In MVP the presenter assumes the functionality of the "middle-man" (played by the controller in MVC). Additionally, the view is responsible for handling the UI events (like mouseDown, keyDown, etc), which used to be the controller's job. Eventually, the model becomes strictly a domain model
Domain model
A domain model in problem solving and software engineering can be thought of as a conceptual model of a domain of interest which describes the various entities, their attributes, roles and relationships, plus the constraints that govern the integrity of the model elements comprising that problem...

.

Pattern description

MVP is a user interface design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...

 engineered to facilitate automated unit testing and improve the separation of concerns
Separation of concerns
In computer science, separation of concerns is the process of separating a computer program into distinct features that overlap in functionality as little as possible. A concern is any piece of interest or focus in a program. Typically, concerns are synonymous with features or behaviors...

 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 an interface that displays data (the model) and routes user commands (events) 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 ConcreteDomainPresenter implements the IDomainPresenter interface:


public class DomainView: IDomainView
{
private IDomainPresenter domainPresenter;

public DomainView // Constructor
{
this.domainPresenter = new ConcreteDomainPresenter(this);
}
}


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 which 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 then 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
Multilayered architecture
A multilayered software architecture is using different layers for allocating the responsibilities of an application.There is also an architectural pattern that is named Layers and has been described in different publications, including the book Pattern-Oriented Software ArchitectureA System of...

 system with common layers
Common layers in an information system logical architecture
The following four layers are the most common layers in a logical multilayered architecture for an information system with an object-oriented design:* User Interface Layer...

 but it can also be seen as a presenter layer of its own between the application layer and the user interface
User interface
The user interface, in the industrial design field of human–machine interaction, is the space where interaction between humans and machines occurs. The goal of interaction between a human and a machine at the user interface is effective operation and control of the machine, and feedback from the...

 layer.

History

The model-view-presenter software pattern originated in the early 1990s at Taligent
Taligent
Taligent was the name of an object-oriented operating system and the company dedicated to producing it...

, a joint venture of Apple, IBM
IBM
International Business Machines Corporation or IBM is an American multinational technology and consulting corporation headquartered in Armonk, New York, United States. IBM manufactures and sells computer hardware and software, and it offers infrastructure, hosting and consulting services in areas...

, and HP, and was the underlying programming model for application development in Taligent's C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

-based CommonPoint environment. The pattern was later migrated by Taligent to Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 and popularized in a paper by Taligent CTO Mike Potel. After Taligent's demise in 1997, Andy Bower and Blair McGlashan of Dolphin Smalltalk
Dolphin Smalltalk
Dolphin Smalltalk, or "Dolphin" for short , is an implementation of the Smalltalk programming language by Object Arts, targeted at the Microsoft Windows platform.The last major release was Dolphin Smalltalk X6, which comes in two versions:...

 adapted the MVP pattern to form the basis for their Smalltalk user interface framework. In 2006, Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

 began incorporating MVP into their documentation and examples for user interface programming in the .NET framework
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

. The evolution and multiple variants of the MVP pattern, including the relationship of MVP to other design patterns such as MVC, were analyzed in detail in an article by Martin Fowler
Martin Fowler
-Online presentations:* at RailsConf 2006* at JAOO 2006* at QCon London 2007 * at QCon London 2008 * at ThoughtWorks Quarterly Technology Briefing, October 2008...


and another by Derek Greer

Implementation in .NET

The .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

 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
ASP.NET is a Web application framework developed and marketed by Microsoft to allow programmers to build dynamic Web sites, Web applications and Web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages ...

 Web application, a Windows Forms
Windows Forms
Windows Forms is the name given to the graphical application programming interface included as a part of Microsoft .NET Framework, providing access to native Microsoft Windows interface elements by wrapping the extant Windows API in managed code...

 application, or a Silverlight
Microsoft Silverlight
Microsoft Silverlight is an application framework for writing and running rich Internet applications, with features and purposes similar to those of Adobe Flash. The run-time environment for Silverlight is available as a plug-in for web browsers running under Microsoft Windows and Mac OS X...

 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. Below is a list of such frameworks under the .NET platform.

Frameworks


Implementation in Java

In a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 (AWT
Abstract Window Toolkit
The Abstract Window Toolkit is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes — the standard API for providing a graphical user interface for a Java program.AWT is also the GUI toolkit for a...

/Swing
Swing (Java)
Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....

/SWT
Standard Widget Toolkit
The Standard Widget Toolkit is a graphical widget toolkit for use with the Java platform. It was originally developed by IBM and is now maintained by the Eclipse Foundation in tandem with the Eclipse IDE...

) 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
Google Web Toolkit is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. Other than a few native libraries, everything is Java source that can be built on any supported platform with the included GWT Ant build files...

 requires only that some component implement the view interface. The same approach is possible using the Echo2
Echo (framework)
Echo is a web application framework that was created by the company NextApp. It originally started as a request-response web application framework that leveraged the Swing object model to improve the speed of application development...

 web framework.

MVP can be implemented in Java SE (Swing and AWT) applications using the Biscotti framework.

Frameworks

  • Swing
    Swing (Java)
    Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes — an API for providing a graphical user interface for Java programs....

  • JFace
    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." It is a layer that sits on top of the raw widget system, and provides classes for handling common UI programming tasks...

  • Google Web Toolkit
    Google Web Toolkit
    Google Web Toolkit is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. Other than a few native libraries, everything is Java source that can be built on any supported platform with the included GWT Ant build files...

  • Echo2
    Echo (framework)
    Echo is a web application framework that was created by the company NextApp. It originally started as a request-response web application framework that leveraged the Swing object model to improve the speed of application development...

  • Vaadin
    Vaadin
    Vaadin is an open source Web application framework for rich Internet applications. In contrast to JavaScript libraries and browser-plugin based solutions, it features a server-side architecture, which means that the majority of the logic runs on the servers. Ajax technology is used at the...

  • ZK
    ZK (framework)
    ZK is an open-source Ajax Web application framework, written in Java, that enables creation of rich graphical user interfaces for Web applications with no JavaScript and little programming knowledge....


See also

  • Presenter First
    Presenter First
    Presenter First is a software development approach that combines the ideas of the model–view–presenter design pattern, test-driven development, and Feature-driven development.-Approach:...

  • Model–view–controller
  • Common layers in an information system logical architecture
    Common layers in an information system logical architecture
    The following four layers are the most common layers in a logical multilayered architecture for an information system with an object-oriented design:* User Interface Layer...

  • Model View ViewModel
    Model View ViewModel
    The Model View ViewModel is an architectural pattern used in software engineering that originated from Microsoft as a specialization of the Presentation Model design pattern introduced by Martin Fowler...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK