MVP: Should the View implement a Presenter's interface or vice versa?
Asked Answered
M

3

15

I am doing my first steps with GWT. I have a question after reading:

In the first example the Presenter defines the interface for the View.

public class ContactsPresenter implements Presenter {
  ...
  public interface Display extends HasValue<List<String>> {
    HasClickHandlers getAddButton();
    HasClickHandlers getDeleteButton();
    HasClickHandlers getList();
    void setData(List<String> data);
    int getClickedRow(ClickEvent event);
    List<Integer> getSelectedRows();
    Widget asWidget();
  }
}

And in the second one, the View defines the interface for the Presenter.

public interface ContactsView<T> {

  public interface Presenter<T> {
    void onAddButtonClicked();
    void onDeleteButtonClicked();
    void onItemClicked(T clickedItem);
    void onItemSelected(T selectedItem);
  }

  void setPresenter(Presenter<T> presenter);
  void setColumnDefinitions(List<ColumnDefinition<T>> columnDefinitions);
  void setRowData(List<T> rowData);
  Widget asWidget();
}

What's the idea of this difference?

Which should I choose?

Mismate answered 22/7, 2010 at 12:56 Comment(3)
A presenter should be able to modify the view. So, the presenter just needs to maintain a reference to its views. I don't understand what would be gained if the view and the presenter were to implement each other.Madras
@Abhijeet Kashnia: This are two different examples. Check the links, they do the same but they are implemented differently.Mismate
Even in the History management with Activity and Places and MVP we have activity implementing a view interface! How is that MVP?Neuritis
H
2

I think you should have used the word 'defines' in your question instead of 'implements' and if thats the case then it does not matter which class defines the interface.

You could do something different by defining the interfaces in its own files. At the end of the day all that matters is the Presenter implementing the Presenter interface and the View implementing the View interface.

Headdress answered 22/7, 2010 at 13:2 Comment(0)
Q
2

@deepak these are valid concerns . Word is infect implementation not definition .

Let me explain . In first example presenters hold the contract to what view must implement in other words drives what should be implemented by views a classical MVP approach .

Things get confusing in second example. Where Presenter has no control over what view must implement . This is not MVP and google is calling it as MVP . There is no way you can test the views with JRE /unit tests using this approach . That does not make it bad though just not MVP and google should not call this MVP or they must explain as to why is it an MVP ?

@Saket Bansal separating out interface is not correct approach . It will result in hard to maintain code as application grows .

In my opinion you can take either approach , I remember google saying some where first one worked for them for adwords and second for wave .

Any how you should also look at framworks like GWTP or ERRAI from jboss

Quintal answered 14/9, 2010 at 0:23 Comment(0)
T
0

In the second tutorial the code was changed to using a Presenter interface (defined in the view) to accommodate using UiBinders and Java generics. I think the Presenter interface was moved to the View interface as they both share the same generic T.

Ternion answered 22/7, 2010 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.