Monday, September 6, 2010

Re: MVP Question

On 6 sep, 23:29, Jeff Chimene <jchim...@gmail.com> wrote:
> A couple of observations:
>
> o Consider presenters as singletons
> o Consider Gin
>
> I'm sure others will provide additional observations.

"presenters as singletons" means you have to make sure you cleanup
state between uses; "throwable" presenters are easier to code (and
test) because they don't need this kind of clean-up. Not to mention
that singletons eat up memory even when you no longer need them.
See "a note on performance" in http://tbroyer.posterous.com/gwt-21-activities
(and look at GWT 2.1's com.google.gwt.app.place.AbstractProxy*Activity
–in milestones up to and including M3, they're called
AbstractRecord*Activity–; Ray Ryan recently said this design was
inspired by Wave where it was quite succesful).

Jambi: Gin won't help you solve this particular problem, but I advise
you to look at it soon, as it'll make your life so much easier!
(BTW, dependency injection is a very good pattern to follow, be it
managed manually or with tools such as Gin; the rule of thumb is: do
not "new" objects –such as your presenters and views–, have them, or
factories of them, passed to your constructor)

> On 09/06/2010 01:10 PM, Jambi wrote:
>
> > Hey guys,
>
> > I have some problems with my MVP architecture. Do I have to "destroy"
> > my old presenter instances when I m creating a new one? For example:
>
> > I m on page A and switch to page B. After that I go back to page A and
> > create another instance of pageApresenter with my history management.
>
> I don't use history management (yet), but you shouldn't need to create a
> new presenter just to go to a place.
>
> > But it feels, that the old instance of my presenter runs in the
> > background and is also affecting my actions. When I m now fireing an
> > event to the eventBus the event is called twice or more often if I
> > switch another time to page B and then back to page A.
>
> That's what we call a "clue" :)
>
> You probably want to deregister your handlers before switching to Page
> B, and reregister upon returning to page A.

+1
So to answer Jambi's question, yes you have to "destroy" your
presenter (in GWT 2.1, stopping the activity) to tell it to deregister
itself from whereever it registered references to itself (which
prevent it from being garbage collected).
This is done by keeping handles to the HandlerRegistration returned by
the addHandler calls to the event bus to be able to removeHandler()
(you could also just call removeHandler on the event bus with the
exact same arguments as your previous addHandler calls; note that in
GWT 2.1 activities, this would be automatic, handled by the
StopperedEventBus you're passed in your activity's start())
http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/event/shared/HandlerRegistration.html

--
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-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment