What is an Interactor? How does it fit within the MVP Design? What are the advantages/disadvantages of using an interactor vs putting the interactor code in the presenter?
The role of the Presenter class is to keep the business logic of the application away from the activity. Below is the complete step-by-step implementation of this android application. Note that we are going to implement the project using both Java and Kotlin language.
MVP separates the application into three layers: Model: This holds the actual business logic of the application. which will allow us to perform and manage any business layer logic and data access, such as retrieving data from our Web service, Access to the database, etc.
Using livedata instead of MVPview in MVP makes the presenter loosely coupled with the view and helps in reusing presenter. We can create different presenter for different components and expose events using livedata, it also encourages reactive architecure.
The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components: Model: Layer for storing data.
At the time of writing (2016), many projects are written using bad version of MVC pattern. Where an Activity/Fragment/Controller has too many line of codes. This issue is commonly called God Activity. MVP is gaining popularity to solve this issue by decoupling class to a Model, View, and Presenter.
But MVP itself isn't enough, we also see Interactor and Repository pattern emerges.
What is an Interactor? How does it fit within the MVP Design?
You can think of an interactor as your "Util" class to Create, Read, Update, and Delete (CRUD) a Model. An interactor will fetch data from your database, web services, or any other data source, from a Repository. The Interactor is the "verb" or "action" to get the Model.
After getting the data, the interactor will send the data to the presenter. The presenter decides when or how to use the model to make changes in your UI.
Using an interactor means the business logic is decoupled. Since it's decoupled; the code is reusable, simpler, and testable.
What are the advantages/disadvantages of using an interactor vs putting the interactor code in the presenter?
You can put the "interactor code" in the presenter, for example if you're confident the code is simple enough it doesn't need to be extracted to a separate class. But if you decide to use interactor, the interactor can be reused on other presenters.
What about repository?
The repository is the class that responsible for the implementation detail of the CRUD operation, like connecting to a database.
The Repository contains the implementation detail to get the Model.
class UserRepository { fun connectToDb() {} fun getUser(): User {} }
Some people call this Data Source, but I believe the terms are interchangeable.
Update (2021): Even though the MVP + Interactor is still useful. MVVM pattern with Android Jetpack is the preferred UI pattern by Google.
Interactor is a class which separates Domain Layer from Presentation Layer. In simple words it provides way to write business logic separately than code which is used for manipulate UI (by binding data to UI/ animate / navigation).
So Interactor is mediator between Presenter/ViewModel and Repository pattern.
I haven't used Interactor pattern in MVP, I have used it in MVVM though. Interactor can be interchangeably used for UseCases.
For example, lets take use case of fetching categories to show in list (In below example, Presenter represents MVP and ViewModel represents MVVM pattern).
Please make note that in this process Interactor can be avoided so instead of using data flow like this Repository->Interactor->Presenter/ViewModel, communication can be happened by Repository->Presenter/ViewModel this way. Here Presenter/ViewModel will be part of Presentation as well as Domain layer. Like I said above Interactor acts as separator of these two layer.
These are some concisely written blogs to explain this concept for reference
I hope this will help you in understanding role of Interactor in better way. Happy Coding!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With