简体   繁体   中英

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

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?

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.

@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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM