Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of MVP in android

Tags:

android

mvp

I am new in android development.I want to implement MVP in my project but I don't know What are the advantages of using MVP SO Please tell me what are the advantages of using MVP in android.

like image 374
Vivek Pandey Avatar asked Nov 23 '16 13:11

Vivek Pandey


2 Answers

There are two parts to your question as I see it:

  1. Advantages of MVP in general
  2. Advantages of MVP in Android

The general advantages that one gets from implementing MVP (or similar architectural pattern like MVC, MVVM, MVVC, etc.) are:

  1. Clear separation of responsibilities between components. This separation allows for an easier understanding and maintenance of the code base.
  2. Modularity. Modularity allows you to e.g. switch to a different implementation of view component in order to completely change application's UI, while all other components remain intact.
  3. Easier testing. Since there are well defined boundaries between components, it becomes much easier to test each component in isolation (by e.g. mocking other components).

The above are general advantages of MVx on any platform. These also apply to Android, but, IMHO, in Android the gains from following MVx are higher due to the following reasons:

  1. Android framework does not encourage developers to write a clean code (to say the least). Adhering to a clear set of practices in this case is especially important.
  2. Unit testing is damn hard on Android (in several aspects). Therefore, having a clear boundary between components and being able to mock them out is especially important if you want the code to be testable.

If you want to go down MVP path in Android, check out these resources as well:

  • Why Activities in Android are not UI Elements
  • MVP and MVC Architectures in Android
like image 77
Vasiliy Avatar answered Oct 27 '22 17:10

Vasiliy


Technically speaking, the advantages are testability, maintainability, extendability, etc, as others noted. I have written a complete post on that here.

But as you said you are new to android, I feel a tangible example of using an architectural pattern in building an app will help you much better understand its importance:

Assume an app having an EditText field where you enter an address and a FIND button leading to a search results page.

If you don’t use MVP or any other proper architecture, you have to put everything inside your Activity or Fragment:

findButton.setOnClickListener(new View.OnClickListener() {
    @Override    
    public void onClick(View view) {
        callTheNetworkSearchMethod();    }
});

Now what if you want to change the endpoint API of your search address? This way you have to change that Activity for everything, even for the endpoint API. But is it reasonable? Definitely not.

In a team project, when the lead developer reviews the feature you have developed she finds out that the whole Activity is changed for just adding a single endpoint api change. She has to go through the scan the whole Activity just to figure out you have only changed an endpoint. Also, if another developer is working on doing a minor change to the Activity, you might even get conflicts and have to spend some time on resolving it.

All of the above and many other enhancements are at your reach by using MVP, in which you simply write:

findButton.setOnClickListener(new View.OnClickListener() {
    @Override    
    public void onClick(View view) {
        mPresenter.doWhenFindButtonIsClicked();    }
});

Now, if you want to change anything when the click is made on FIND button you would do it in the Presenter and the View remains intact.

This is one of the most important aspects of using architectural patterns, called ‘separation of concerns’.

If you’re interested to know more about how MVP can help with an android project, together with a complete sample app, check my MVP article

like image 21
Ali Nem Avatar answered Oct 27 '22 17:10

Ali Nem