How to do MVP

I have found a couple of great examples of using the Model-View-Presenter pattern with GWT (Google Web Toolkit).

  1. Large Scale Application Development and MVP – posted in the Google code website. It has a good example of a Contact application. This seems like a very straightforward approach
  2. GWT-Presenter– This framework looks to be very well designed. Their example is a replacement for the greeting test app you get when you create a new GWT application in Eclipse. For more on this framework, see Chris Lowe’s blog.

The big questions for me follow.

  • What are the differences in the two frameworks?
  • Which framework should be the starting point for my work?

What are the differences in the two frameworks?

My sense right now is that there is something very compelling about Framework 1 (from Google Code). It seems so straightforward and takes care of the essential elements: (1) MVP;  (2) events and event bus; (3) getting browser history right so you can bookmark pages; (4) easily tested.  Their discussion of events is good and they make a point that you really only want to have app-wide events on the event bus. They warn against getting “bogged down in event handling”. For the Contact app example, they list 5 events and 5 event handlers that they have defined. It feels about right to me, meaning the events make sense as things you’d have to know about in order to present the user with the right view at the right time.

One thing to note about the example of Framework 1. It does have a server side for updating contacts, but is totally mocked up. It’s not really stored in a database and you can only have one new contact at a time. Each time add a new contact, it updates the list item on the list it is maintaining on the server side.

Framework 1 handles history by having its AppController track ValueChangeEvents from the History object. So AppController implements onValueChange, and it’s there that it checks the history token and locates the next Presenter to run. It looks a bit like:

public class AppController implements ValueChangeHandler { 
  public void onValueChange(ValueChangeEvent event) { 
    String token = event.getValue(); 

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

      if (token.equals("list")) { 
        presenter = new ContactsPresenter(rpcService, eventBus, new ContactView()); 
      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) { 

What I like about Framework 2 (GWT-Presenter) is that it has all the things that Ray Ryan talked about. It makes use of Dependency Injection using Guice and GIN, and it looks like it has a little bit more developed handling of history, as embodied in a PlaceRequest object.

What framework should be the starting point of my work?

I don’t know yet. My next step will be to take the Google Code Contact example and redo it on top of GWT-Presenter. That will test my understanding of GWT-Presenter. I expect to find that GWT-Presenter is just a little bit better in its handling of events and places. Besides, it will force me to come to terms with DependencyInjection (DI), which Ray Ryan insists is not such a big deal and is really good for you.

About Bill Lahti

Bill Lahti is a software engineer building mobile applications and knowledge management solutions. Two of his interests are writing for this blog and building Android apps, with strategy games being an area of particular interest.
This entry was posted in web and tagged , , , . Bookmark the permalink.

One Response to How to do MVP

  1. Pingback: Displaying Links in GWT FlexTables « More Is Not Always Better

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s