antoniolg / androidmvp

MVP Android Example
5.93k stars 1.58k forks source link

Interactor being instantiated in the View #30

Closed noloman closed 7 years ago

noloman commented 7 years ago

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!

antoniolg commented 7 years ago

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.

noloman commented 7 years ago

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

antoniolg commented 7 years ago

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

noloman commented 7 years ago

Thanks!