Monday, May 9, 2016

Model View Presenter for android

Architecture Pattern is a fundamental part of software development. It’s the way to maintain a project clean, expansible and testable. These pattern make application to easy maintainable. It is the way to write code in specific format and separate there business logic, user interface, assets, data base related code etc.   
Here I am writing to consider best pattern for android mobile application. It is difficult to say that one pattern is best and other is not because it depends on application requirement and environment. Design pattern i.e. MVVM, MVC and MVP is more popular for android application. Here I am going to talk about MVP design pattern because it is best for android application in most of the scenario.

Model View Presenter Pattern

MVP (Model View Presenter) pattern is a derivative from the well-known MVC (Model View Controller).
 MVP could bring very valuable improvements to our existing approach. Because android current architecture by default divided in two layers (view and data), adding MVP felt natural. We simply had to add a new layer of presenters and move part of the code from the view to presenters.
MVP makes views independent from our data source. We divide the application into at least three different layers, which let us test them independently. With MVP we are able to take most of logic out from the activities so that we can test it without using instrumentation tests
There is three layers in MVP pattern:

Presenter:

The Presenter is responsible to act as the middle layer between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.

View:

The view, usually implemented by an Activity (it may be a Fragment, a View depending on how the app is structured), will contain a reference to the presenter. Presenter will be ideally provided by a dependency injector such as Dagger, but in case you don’t use something like this, it will be responsible for creating the presenter object. The only thing that the view will do is calling a method from the presenter every time there is an interface action like a button click.

Model:

In an application with a good layered architecture, this model would only be the gateway to the domain layer or business logic. See it as the provider of the data we want to display in the view. Model is responsible to provide logic related database.






Why MVP is suitable for android application?

In Android we have a problem arising from the fact that Android activities are closely coupled to both interface and data access mechanisms. That I mansion above default implementation is view and model (Data) We can find extreme examples such as Cursor Adapter, which mix adapters, which are part of the view, with cursors, something that should be relegated to the depths of data access layer.
For an application to be easily extensible and maintainable we need to define well separated layers. What do we do tomorrow if, instead of retrieving the same data from a database, we need to do it from a web service? We would have to redo our entire view.
MVP makes separates our logic in three layer just add Presentation layer between views and data. It makes application to maintainable, clean, expansible and testable.



No comments:

Post a Comment