vendredi 1 septembre 2017

Best way to implement MVP

In the MVP pattern, there is no dependency between the view and the model. Everything is done by the presenter.

In our GWT-project, we implement all our MVP classes like shown on the GWT .page. To access the view, our presenter uses an interface like this:

public interface Display {
    HasClickHandlers getAddButton();
    HasClickHandlers getDeleteButton();
    ...
}

So, the presenter gets a button from the view, and applies its click handlers to it.

But this feels so wrong to me. This violates the single responsibility principle, as well as the information hiding principle. the presenter should not know anything of the internals of the view (even when we get an abstract HasClickHandlers).

In my opinion, we should better use this kind of pattern:

public interface Display {
    void setPresenter(Presenter p);
}

public interface Presenter {
    void onAdd();
    void onDelete();
}

So, the view tells the presenter actively, that some interaction occured. But not on which view-element. My team partners argue, that with the first solution, we avoid circular dependencies. That´s right. But anyway, I would prefer the second way because the modules are better separated and can be maintained independently.

What is the better way? What are the advantages / disadvantages?

Aucun commentaire:

Enregistrer un commentaire