Scaling gwt's "Contacts" (sample project) AppController with MVP
Asked Answered
P

2

12

I'm just learning GWT so I'm still trying to sort out all of its quirks and features. I'm reading through the example they give illustrating the MVP pattern, and I pretty much get it, except I'm wondering about one thing.

The AppController they use implements the ValueChangeHandler interface and the onValueChange method is triggered when history changes.

My problem is with this onValueChange in the AppController (i've included it below for anyone who hasn't seen the sample project). It's doing a string comparison on the history token sent in and instantiating the appropriate presenter to handle the action. This is all fine and dandy for the sample app with 3 actions, but how would one scale this to a real app with many more actions?

Sticking to this pattern would lead to a pretty large/ugly else if, but I'm still too new to GWT (and java) to infer a better pattern for larger apps.

Any help is greatly appreciated!

public class AppController implements Presenter, ValueChangeHandler<String> {

  ...

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();

    if (token != null) {
      Presenter presenter = null;

      if (token.equals("list")) {
        presenter = new ContactsPresenter(rpcService, eventBus, new ContactsView());
      }
      else if (token.equals("add")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      }
      else if (token.equals("edit")) {
        presenter = new EditContactPresenter(rpcService, eventBus, new EditContactView());
      }

      if (presenter != null) {
        presenter.go(container);
      }
    }
  } 
}
Peshawar answered 1/4, 2010 at 14:30 Comment(0)
C
6

You raise a valid point with large scale GWT application. I recently worked on 50.000+ line GWT portal app and we are getting buried in events and complex switch/handler patterns. There is a good blog post available here that describes how terrible this can become and also hints at a solution (see terrible footnote).

However the new GWT2 UIBinder and MVP functionality does simplify things. In fact the author of the above mentioned blog post has written about the places framework (which is a part of GWT 2.1) here.

Cobblestone answered 2/4, 2010 at 11:52 Comment(0)
L
0

The only event the onValueChange method should receives are the "view changing" one. Considering each condition is 1 line, it's never going to be THAT big. In the end you'll be fine using that pattern.

As Lars said though, combining UiBinder with the MVP pattern is easy and will greatly reduce the number of code line and make your code easier to modify.

Lockridge answered 20/5, 2010 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.