antoniolg / androidmvp

MVP Android Example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interactor being instantiated in the View

noloman opened this issue · comments

In the MainActivity you're creating the Presenter by passing in a new instance of the interactor.
That means that the view knows about an inner layer of the app, which is that interactor.
Should it be like that? In theory, the view should only know about its closest inner layer, which is the presenter, but it shouldn't know about that interactor.

Thanks and good job!

It'd be better if they don't, but it's difficult to do without a dependency injector, as the presenter needs to get it as a dependency (otherwise you can't mock it in tests), and the presenter also needs to be declared somewhere.

To sum up, in this example, the activity is doing the task of the dependency injector, so that's why it builds and provides all the dependencies, but in a real project you'd probably use Dagger or similar to provide that.

This example is not using Dagger because is meant to be a simple MVP example for people that starts with the pattern for the first time.

Thanks for the answer! But is there any possibility of correctly doing this without a DI framework, such as Dagger?

You could create your own classes that implement a simple DI, but as the project grows, it's going to be difficult to manage.

Thanks!