mercredi 1 avril 2020

MVP: Is it okay for the View to pass a (Model instance) dependency to the Presenter?

I know that in MVP the View doesn't touch the Model, as it delegates to the Presenter instead.

However, in my app, I need to provide a dependency in the Presenter's constructor that is part of the Model:

class Presenter(view: View, dependency: Model) {
    ...
}

The reason I need to do this is to make unit testing easier (and I don't want to use a DI framework).

Is it okay for the View to instantiate a Model and pass it to the Presenter?

class View {
    init {
        val presenter = Presenter(this, Model())
        ...
    }
}

Or should the Presenter provide another constructor that instantiates the dependency, allowing the View to call that instead?

class Presenter(...) {
    constructor(view: View) : this(view, Model())
}

class View {
    fun initialize() {
        val presenter = Presenter(this) // no longer instantiating Model
        ...
    }
}

I like the simplicity of the View instantiating and passing the dependency by itself, but I'm not sure whether it's appropriate for the View to know about the Model.

Aucun commentaire:

Enregistrer un commentaire