GWT MVP Architecture

270 views
Skip to first unread message

Aryan

unread,
Sep 13, 2012, 2:09:30 PM9/13/12
to google-we...@googlegroups.com
Hi all,
 
lets look at the code:
 
public class MyView implements IMyView {
 
    Button click;
.....
    public HasClickHandlers getClick(){
              return click;
     }
 
}
 
 
public class MyPresenter {
 
   public interface IMyView {
          public HasClickHandlers getClick();
    }
 
    private IMyView view;
 
    public MyPresenter(IMyView view){
      this.view = view;
      bind();
    }
 
    private void bind(){
       view.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent e){
                Window.alert("heellllo");
 
            }
    }//binds end
 
 }// class ends
 
//(We are not using Activities or any MVP framework)
 
ok tats it. Now in applicaton the view is singleton. but the presenter are not, so they are made as and when needed like :
 
MyPresenter p = new MyPresenter(view); //view is singleton throughout the application; assume getting it by some factory
 
Now suppose after a while if I have created 10 MyPresenter instance that will add 10 clickHandler to button "click" . So one click event will be handled 10 times by 10 different handlers.
 
I can see here it as happening when click the button I get 10 times alert window.
 
So where I misunderstood the MVP architecture, what I am missing..... please help
 
Thanks in advance.
 
 
 

Jens

unread,
Sep 13, 2012, 2:34:47 PM9/13/12
to google-we...@googlegroups.com
As your view is singleton you have to tell the presenter that it should detach itself from the view, e.g. by introducing a public Presenter.unbind() method. You then have to call that unbind() method before you throw away your presenter instance. Your presenter needs to remember the HandlerRegistration instances so unbind() can use them to remove the handlers from the view.

Alternative: Let the view know its presenter and let it delegate to the presenter once an event has occurred. If the view does not know any presenter, nothing will happen. That way you can swap presenters or remove the presenter by calling view.setPresenter(null). 

public MyView extends Composite implements View {
  
  Presenter presenter = ...;
  Button createNoteButton = ...;

  public void setPresenter(Presenter p) {
     presenter = p;
  }

   @UiHandler("createNoteButton") //if you use UiBinder. Otherwise you have to register the ClickHandler yourself in the view.
   void onClick(ClickEvent event) {
     if(presenter != null) { presenter.onCreateNote() };
   }

}

-- J.

stuckagain

unread,
Sep 14, 2012, 5:21:47 AM9/14/12
to google-we...@googlegroups.com
Why does the view need to be a singleton ?
 
Anyway, when you are done with the presenter, then you need to tell it so.
In that case it can unregister any installed handlers.
 
David

Aryan

unread,
Sep 14, 2012, 11:14:42 AM9/14/12
to Google Web Toolkit






On 14 Sep, 14:21, stuckagain <david.no...@gmail.com> wrote:
> Why does the view need to be a singleton ?

I guess why I am having view as singleton is having better performance
as I see views are expensive to create.
Not creating em everytime saves operation deep down like
"Document.create.... -> appendChild..... and so the DOM manipulation
that saves time.....
> > Now suppose after a while if I have created *10 MyPresenter *instance
> > that will add *10 clickHandler *to button "c*lick" . So one click event
> > will be handled 10 times by 10 different handlers.*
> > **
> > I can see here it as happening when click the button I get 10 times alert
> > window.
>
> > So where I misunderstood the MVP architecture, what I am missing.....
> > please help
> > **
> > Thanks in advance.- Hide quoted text -
>
> - Show quoted text -

Adolfo Panizo Touzon

unread,
Sep 18, 2012, 7:25:20 AM9/18/12
to google-we...@googlegroups.com
I don't know if i am missing something, but, why yo don't create a clientFactory in order to get the View? 

In that way you only create the view once and you avoid the problem of having multiple handlers attached.

2012/9/14 Aryan <saurab...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
El precio es lo que pagas. El valor es lo que recibes.
Warren Buffet
Reply all
Reply to author
Forward
0 new messages