Just Start Typing Text and Press Enter

alt text
Vinod K. Morya
Androi developer | Mobile Architect

DRINK A COFFEE WITH ME

>Loading...
Message Sent
Error!
Menu
Close
Sep 25, 2017

Android – MVP with Interactor | VIPER architecture

4 Comments | By

Earlier we used to develop the application using design like patterns MVC but these pattern cause some problems to meet the customers requirement. The apps are having a very less life time and the customers understand that app need frequent changes to cope up with user engagement and incorporate the upcoming technologies into the app. So, developers need to understand that they have to go with frequent changes in the application on any layer of development. So here comes the new design patterns and most promising are MVP & MVVM pattern.

MVP pattern
As the name suggest that this design pattern is having a Model, View layer and Presenter layer. In MVP pattern our model holds the data as like in any other design pattern. The View layer represents the UI part for most of the part and shows the data which is being hold by the model and View handles the user actions on an activity. Now, Presenter layer – To make our code more readable and maintainable a new concept comes here which is being implemented by the presenter layer. Presenter layer is responsible for all the logic implementation and complex calculations in sort of all the logic code which we do in our activity thus providing a good abstraction of the code. An activity can have multiple presenters if there is a lot which is going on in the activity and can be distinguished independently. Presenter acts as a communication channel between our model and view. It’s the responsibility of the presenter that if data changes in either View and Model it should maintain the singularity of the Model and View. So if there is change in View, let’s say you have a EditText and you gave it some input then Model should also change and if Model changes due to any external reason like an API call our View should be able to reflect the same.

Adding a Interactor

Since we know that in Presenter we make all of our logics and is the communicator layer between View interface and the model, now going further if need to do any I/O operation like database callbacks, ¬†webservices implementation etc then it is a lot to take by the ¬†presenter’s code as it will become more complicated and lengthy, to handle this the concept of Interactor interface is born whose job is to provide any data required by the presenter, the interactor is again loosely and only coupled with the presentor same way the later is coupled with the activity, to know more we will see the implementation in codes below.

Show me code – sample app
To understand MVP pattern, I am going to demonstrate that through an example so that developers can have the concrete understanding of this design pattern. In this example we will populate the data on a recyclerview in an Activity from an API call.

Model
Model is the class in our project which holds the data, as in any other design patterns. For example

View

The View is implemented by an Activity and it contains the reference to the presenter. View’s job is to call a method from the Presenter every time there is a user interface action. So let’s see how our View class looks like.

Also, In this example we are having two layouts, one for activity and other for recycler item view.

And the below layout is for our recycler view item.

Now, we have our two layouts let’s see how our presenter works with both the Model layer and View layer.

Presenter

To implement the presenter we use two classes, the first in which we define our presenter interface and the other in which we implement the presenter class. See the code below.

In this interface class we define our methods and the implementation will be done in RecepiePresenterImpl.java class. Have a look to below class.

You can see from the code that the constructor is having the reference of our View class i.e. IRecepieView.java. I have also implemented an RecepieInteractor, which is basically an interactor class, I will explain this in next blog but for now understand that Interactor is used to handle the API call or the database queries and then it returns the required data to the presenter. Now see how our Activity class looks like.

Notice that our RecepieActivity.java implements the IRecepieView and implements it’s methods now note that we have created our presenter object at line 29

And when you see it’s implementation in the RecepiePresenterImpl.java class you can find that we are gettting the reference of IRecepieView from there, a simple example of implementing the interfaces.

So, till now we have implemented our Models, Views and Presenter. Now as the user will start giving it’s input in the SearchView of our RecepieActivity.java the presenter object will send that data to the Presenter Implementation class i.e RecepiePresenterImpl.java and in presenter class our interactor object handles the data get the list of recepies from server and again share it’s callback to presenter class.

Here is our interactor class is sharing the data to the presentor class via onRecepieReady callback.

Interactor
Interactor is the class which helps our presenter to get the data from database or API call, it helps to segregate the network calls, database query calls from our main logic which we are implementing in our presentor class. The implementation of interaction between interactor and presentor depicts the same relationship between our activity and presentor, here is how our interactor class looks like.

And below is it’s implementation class.

Conclusion

MVP design pattern is widely used by most of the developer community around the world these days as it increases the scalability, readability thus providing more maintainable code. I hope you liked the session.

Happy coding

Vinod K Morya

Download the source code of sample from Github link.

4 Comments

  • Vinod Morya
    Vinod Morya

    Do let me know if you liked this tutorial and do comment.

    September 26, 2017 - 10:32 pm Reply
  • Avatar
    abhilasha

    nice

    September 27, 2017 - 10:47 am Reply
  • Avatar
    Priyank

    Nice description. MVP definitely has the heads up over MVC. Its time for all developers to adopt with MVP to rule the coding world.

    September 27, 2017 - 12:22 pm Reply
  • Avatar
    pawan

    Great tutorial

    October 26, 2017 - 4:51 pm Reply

Leave A Comment

Author Details
Vinod Morya
Vinod Morya

The author.

Share Post