Android MVP: What is an Interactor?

bkach picture bkach · Mar 2, 2016 · Viewed 40.7k times · Source

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?

Answer

aldok picture aldok · Jun 18, 2016

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