Thursday, July 9, 2009

GWT and MVP (Model View Presenter) Pattern

After watching the google wave presentation at Google I/O, I changed my mind about the GWT (Google Web Toolkit) and I've been studying it hard. From the technical view, GWT is amazing and it's getting better on each new release. I was searching for best practices using GWT and I found this presentation: "GWT App Architecture Best Practices - Ray Ryan". Ray Ryan gives a lot of tips on how to create a well-structured and testable application using GWT. On the first thought I found it a little bit complex: when you deal with a simple screen it's easy to understand, but when you have to deal with a more complex screen, things get way more complex.

Ray Ryan presents the MVP (Passive View) pattern as a good solution, and it is. I spent many hours trying to figure out a way to implement the communication between the presenters. After this hard work I found - in my opinion - a good way to implement it, and I decided to share it with the world. I decided to share this knowledge as a sample application. You can find this application on http://gwt-mvp-sample.googlecode.com.

I'm not very experienced in this pattern so you might find some bugs or wrong concepts. The idea is to talk about it, to share knowledge. Feel free to change the source code, send me e-mails, add issues, etc.

I'm looking forward to your replies.

23 comments:

Chad Skinner said...

I know this is probably basic and I am overlooking something, but In the source code for the MailPresenterImpl you have the following code:

private void doIssueUpdated(Issue issue) {
BaseView displayView = displayPresenter.get().showIssue(issue);
view.addContent(displayView);
}


where is the method .get() on displayPresenter defined?

Jeff said...

Chad,

This is part of the DI handling. The specific .get() you are asking about is actually on com.google.inject.Provider.

Some good intros are here, and here.

Basically it is finding a concrete class that implements IssueDisplayPresenter; then instantiates it, and gives you the reference.

Jeff said...

I am curious about the appropriate level of MVP-granularity. I have done most of my GUI-esque programming in JFace/SWT with eclipse plug-in programming.

This lends itself to a certain level of granularity. For example the IssueDisplayWidget and IssueEditWidget are what may be compared to Views in the eclipse world. They are composite with some buttons, and one or more lower level widgets that map to some data.

For the actual lower level components though there is still need for some MVC/MVP concern. For example I could add an IssueListWidget, a presenter, and events wired appropriately to display this new widget. The question I have is that if I were to have some buttons and a table - should I have IssueListWidget handle the eventBus interaction and just use regular old attached listeners to update between IssueListWidget and the underlying table? Or is there any need to make the table have its own presenter and events?

I would be curious to hear thoughts on pros and cons on various ways this could be handled.

Kango_V said...

I'm in this situation as well. I'm wondering how far down the widget stack to take this.

Just getting to grips with gwt-presenter which is an excellent starting point.

Unknown said...

Looks VERY interesting.
Thanks for the show how.

Unknown said...

Hi, Im trying to use retrograde analysis to analyze how you set your code up. I have it working in eclipse, but am a bit confused still.
Is it possible to javadoc comments added to the project?
Like for example, If i hover over HandlerManagerProvider, i get the basic javadoc
Author:
esnunes@gmail.com (Eduardo S. Nunes)
But no actual info about the class.
This would be really helpful to the many many people im SURE are going to be checking this out in the near future.

Sincerely,

dave

brendan said...

Did you manage to get history / PlaceManager working when you used gwt-presenter?

On of the things Ray Ryan said was get history sorted out early.

Eduardo S. Nunes said...

Sorry about the late to answer your comments. I didn't receive any e-mail from blogger about the comments so I've just read them today. About gwt-presenter, I'm not using it, it was just a test. I did some modifications in my implementation. I will post these modifications as soon as possible. I will try to do it today.

Peter Sankauskas said...

I just want to say thank you for the awesome work!

GaryStruthers said...

Yes, thank you. I just demoed a ui based on gwt-mvp. The most loosely coupled UI I've ever written. Once I learned to import the correct Display. Once I figured out how to do something coding was easy.

How does the event bus handle multiple registrations of the same event/handler? Are there duplicates?

Araminos said...

Hello, everyone is interested in printable and readable version of Google IO about MVP, please look
http://extgwt-mvp4g-gae.blogspot.com/2009/10/gwt-app-architecture-best-practices.html

Unknown said...

Eduadro, have you had a look at this similar project?

http://code.google.com/p/mvp4g/

What do you think about it?

Thanks

Unknown said...

Hi, I just set up a simplified version of the mvp-sample project. Mine is very simple, with 2 different views and buttons attached to each view that allow it to swith from one view to another.

What would happen if one of the buttons didnt just switch the view, but did a RPC call, but some json data back to populate a table. How do I make the current view(composite) react to these?

See im confused because the sample project the Handlers are registered to the mainpresenterImpl. How would I access the current presenters bound view's widgets to manipulate them?

This kinda falls in line with Jeff and Kango's questions I think, and they didnt get answered.

Please help.

Sincerely,

dave

ps: Great work by the way.

Unknown said...

My other question follows along the lines of Brendens (most import question of mine is right before this one so answer that one first)
PlaceManager, were do you (view, presenter, or presenterImpl) Register the History?

sincerely,

Dave

(see ? before this one and thanks again)

shambhu said...

Hi,
I am trying to use
HasValue
it is throwing null pointer exception. I used like this

@UiField TextBox materialWeight;
private DoubleWidgetBinder dblMaterialWeight = new DoubleWidgetBinder(0.0,materialWeight);
@Override
public HasValue materialWeight() {
System.out.println("dblMaterialWeight : "+dblMaterialWeight);
return dblMaterialWeight;
}
out put:- dblMaterialWeight : (null handle)

Unknown said...

Hi

thank you for the gwt-mvp-example!
But I have a troubles with implemeting it :) For example I have a grid with user names and column with button "edit user". How can I expose from display these buttons as HasClickHandler() ?

Eduardo S. Nunes said...

You can't use HasClickHandlers, you will have to implement a specific Handler/Event that informs you about the click and some id which identifies the selected user.

Unknown said...

I have simple solving of my problem:
I display constructor I create button's array with getter and put these buttons to the grid. In presenter I register these button as HasClickHandler and use their btnId.

But it seems that it's not the best idea

Mikael KROK said...

Thank you for your work,

actually, it is hard to find clear information about MVP implementation. Especially when you try to do more than a tutorial page.

I am trying right now to implements MVP and I am facing some problems I can't figure out how to resolve them.

I have developped 3 differents Presenters independantly, now I am trying to call the second in an DialogBox from the first... that is my problem right now, maybe when I'll crakc your code I will be able to adapt it to my problem. :p :p

Anonymous said...

bet365 Casino no deposit bonus codes 2021 - Vie Casino
Join VieCasino to get up to £100 Welcome gioco digitale Bonus, 200 Spins No Deposit Bonus, and 100 Free Spins on Immortal Romance and bet365 Golden 우리카지노 Grimoire

fallonyaeasterly said...

Borgata Hotel Casino & Spa - MapYRO
View realtime driving directions to Borgata Hotel Casino & 바카라 게임 Spa, 4400 구리 출장샵 S Casino 부천 출장마사지 Dr, Atlantic City, based on live traffic updates and 진주 출장샵 road conditions – from 용인 출장마사지

eleanesabetta said...

You can even hunt down huge wins with the numerous jackpot titles on display. Bovada has been around the on-line on line casino space because the that} early days. With all gambling wants taken care of 토토사이트 추천 and a few nice promotions, you should to} feel proper at house here.

zarolalaatsch said...

I sex toy knew in my unsnatched soul there was a viral toy way more deserving of reward. After scouring social media, a viral Twitter publish led me to Tracy’s Dog OG Sucking Vibrator. “I’m pretty certain I blacked out,” the screen-capped evaluate states. We are knowledgeable producer of rose toys, with strict high quality management procedures, using safe medical silicone production, low-power motors, 100% safe for the human physique, and no safety accidents. You can search for the closest grownup store for rose toys bought close to you. Rose is a brand new} toy that has also been well-liked an extended time|for a very lengthy time}.