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.
There are two parts to your question as I see it:
- Advantages of MVP in general
- Advantages of MVP in Android
The general advantages that one gets from implementing MVP (or similar architectural pattern like MVC, MVVM, MVVC, etc.) are:
- Clear separation of responsibilities between components. This separation allows for an easier understanding and maintenance of the code base.
- 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.
- 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:
- 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.
- 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:
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
if you want to know the difference between android Mvp,Mvc, MvvM see this explanation by realm https://academy.realm.io/posts/eric-maxwell-mvc-mvp-and-mvvm-on-android/
if you want to compare three pattern see this wonder full blog https://thinkmobiles.com/blog/mvp-vs-mvvm-android-patterns/
google's own words that presenter is preferred:
If your UI is complex, consider creating a presenter class to handle UI modifications. This might be a laborious task, but it can make your UI components easier to test.
i find presenter easier to test that UI elements will really be displayed/updated. One thing a pattern like MVVM does, is you need to find a way to test that the view actually subscribed to the viewModel. Sure,we can test by mocking an observer and showing that when an event occurs then the observer is triggered but how do we know the developer didn't forget to subscribe to viewmodel events ? its more work to test in my opinion. mvp brings clear separation of components.
when im starting a new project i ask, will this app ever need rotation changes ? what kind of config changes will it need to react to ? if not much, i just go with MVP as the unit testing is easier to me. this might be opinion based.
The advantages of MVP or any UI architectural patterns are :
1) All components in the app can be tested easily and independently
2) Modification of app can be done without having to refactor entire app as there is a clear separation of code related to view and app logic.
3) Developers can work on building components independently.
Make sure that MVP is not impacting performance and increasing app crashes by implementing it properly, for more info see http://www.zoftino.com/android-model-view-presenter-mvp-pattern-example
I't google rep for Android MVP with different technologies: https://github.com/googlesamples/android-architecture
And article about interface for Presenter: http://blog.karumi.com/interfaces-for-presenters-in-mvp-are-a-waste-of-time/
© 2022 - 2024 — McMap. All rights reserved.