I was today having a discussion with my amigos on the various design patterns and we were usually struck with mind blowing MVP pattern. I did a Bing on reseearch but could not eventually find a single article which explains the users very clear of this pattern. That’s the reason of this post.

Many people might initially get confused over the MVC [ Model, view and Controller ] and the MVP [ Model, View and Presenter ] pattern. There is a slight variation, but this pattern reduces the coupling.

Model View and Control Pattern

mvc
In this pattern, view dispatches the events to the controller which in turn interacts with business logic of the application and the data binding techniques which updates the view automatically when ever there is a change in our model.

Model View and Presenter Pattern

MVP_Model4
In this pattern, controller plays a major role in handling the user input and also an interaction with business logic also responsible of the update of the view. In this way there remains now link of view with Model and it totally relies on controller for all the presentation logic. As a result, there is no dependencies in either direction between the view and the model.

Let us now go through an example in RajiniKanth style. The below defines the the structure of our application.

StructureThe presenter folder contains the AdditionPresenter which acts as a controller handling the input and also does the business logic.

The view is area where our form is displayed with an Interface IAddView (implemented by the AdditionForm.mxml file) define the methods we need in order to update the view and get the the numbers from the view to complete our Addition operation.

The MVPAddition.mxml is the main file, it only defines the custom component view.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” xmlns:view=”com.vinoth.MVP.view.*”>
<view:AdditionForm/>
</mx:Application>

The Interfact IAddView defines the methods we need in order to update the view and get the numbers from the view. It is implemented by the AdditionForm.mxml

package com.vinoth.MVP.view
{
public interface IAddView
{

function set Number3(value:String):void;

function get Number1():Number;
function get Number2():Number;

}
}

The AddView custom component contains the MXML tag needed to define the UI and it calls the addData() method in the Presenter.

<mx:Label text=”Addition of Two Numbers” width=”100%” fontWeight=”bold”/>

<mx:Form width=”100%” height=”50%”>
<mx:FormItem label=”Number One” width=”100%”>
<mx:TextInput id=”_Number1″ editable=”true” width=”100%” restrict=”0-9″/>
</mx:FormItem>
<mx:FormItem label=”Number Two” width=”100%”>
<mx:TextInput id=”_Number2″ editable=”true” width=”100%” restrict=”0-9″/>
</mx:FormItem>
</mx:Form>
<mx:HBox width=”100%”>
<mx:Label text=”Result” />
<mx:Label width=”100%” id=”_Number3″ />
<mx:Button label=”Add” click=”_presenter.addData()” />
</mx:HBox>

It also contains the methods defined in the IAddView and a private method that is registered as a listener for the creationComplete event that initialize the presenter.

import com.vinoth.MVP.presenter.AdditonPresenter;

private var _presenter:AdditonPresenter;

private function init():void{
_presenter = new AdditonPresenter(this);
}

public function get Number1():Number {
return Number(_Number1.text);
}

public function get Number2():Number{
return Number(_Number2.text);
}

public function set Number3(value:String):void{
_Number3.text = value;
}

The presenter is the one which handles the business logic and updates the view through the interface without accessing the View Form.The presenter stores a private member of the view and it updates accordingly.

public var _view:IAddView

The Patter is simple but yet very powerful because of its least dependency and the presenter relies only on the interface and not on the UI.

Application Souce