Android MVP: What Is An Interactor?
Answer :
MVP exists to tackle God Activity problem (An Activity/Fragment that has way too many lines).
While it wasn't obligatory (you can code in any pattern that you want), many developers agree that MVP is suitable for Android. It makes your source code cleaner, testable, maintainable and robust.
You can think of an interactor as your "Model/Controller". An interactor will fetch data from your database, web services, or any other data source. After getting the data, the interactor will send the data to the presenter. Thus, making changes in your UI.
Advantages of using interactor in a separate class is that it will decouple your class, thus making it cleaner and testable. Sure, you can put the interactor in your presenter inner class, but what's the point? The disadvantages of putting the interactor in your presenter is it will make your presenter class bigger and relatively harder to read and manage.
Update: Of course this is just an over-simplification, if you want to dig deeper you may see fernando cejas blog or antonio leiva blog
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).
- View (Activity/Fragment) will call Presenter/ViewModel's method to get categoryList.
- Then Presenter/ViewModel will call interactor's method to get categoryList
- Interactor will call Repository's (CategoryRepository) method to get categoryList
- Repository will have logic to decide whether to fetch categories from Web Service (Remote Data Source) or from DB storage (Local Data Source) or from cache (temporary storage - can be variable in Repository class).
- Repository will return categoryList (fetched from selected data source) to Interactor
- Interactor will either process on categoryList (some formatting etc) and send it to Presenter/ViewModel. Interactor can directly send list to Presenter/ViewModel if no processing is needed
- Presenter/ViewModel will call View's method with categoryList as parameter
- View will show categoryList with or without Animation
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
- clean-architecture-with-mvvmi
- android-mvp-architecture-extension-with-interactor
- architecting-android-the-clean-way
I hope this will help you in understanding role of Interactor in better way. Happy Coding!!!
Interactor contains the use-cases of the application, which means that it will contain all the implementations for the business domain of the project.
Here is a very well-organized article on Architecturing Android Applications, using the MVP pattern., which I highly recommend you to study.
Comments
Post a Comment