MVP Android (part 1)

Let’s talk about this architectural pattern, applied to the Android development.

Some time ago, all were satisfied the fact that Android itself is pushing the use of the MVC pattern similarity. There is a separate views (xml View), controllers (Activity) and data (POJO Model). It lasted exactly as long as Android is not entered into the mass. There were whole departments of Android developments in large IT companies and as a result, these departments by force of habit start to implement patterns and approaches are accustomed.

But at first they met with fierce resistance from free developers. It is very well seen by the comments to the article, some years ago on the habrahabr.ru. Nevertheless, the use of patterns, the usual commercial development, triumphed.

I am also a long time did not take into consideration such approaches. Nevertheless, when studying of the testing, I was faced with one important thing. Separation of layers in the Android insufficient for testing. We always get too fat Activity, which is virtually impossible to break for testing individual modules.

This pushed me to the study of the use of MVP in the Android applications.

Difference between MVP and MVC is best described in the attached picture.

To understand how to break the application of these layers, you can start from the following:

View layer — is our Activity. It havs a link to the Presenter, and all that it do — say its presenter that some event was happened. As my mentor said: “What View is doing? Nothing. It is stupid.”

Presenter layer — is a such a “middle man”, the core layer, which can and should interact with the view and the model. Get data from the model and gives formatted data for the view. It is decides how to respond to the event of the view.

Model layer — is simple. This is the entry point for a domain model (POJO, plain old Java objects that are linked to a database), and business logic.

In the next article I will describe step by step how to implement this pattern in the application and it will show on the RSS Collector app.

Best regards.


Originally published at junior-freelancer.weebly.com.