Swapnil
Swapnil

Reputation: 8328

Managing many widgets in GWT

Suppose we've the following structure of widgets / panels in a GWT application -

One way to deal with such application is to keep singletons for all widgets / panels, which means composites will hold singletons to all of their child widgets. But I feel this is excess usage of singleton. The other alternative is to construct new Widget object every time one is needed, but this must be expensive.

What are the best practices around this? Are there any standard patterns which take care of this problem?

Upvotes: 2

Views: 265

Answers (1)

Danny Kirchmeier
Danny Kirchmeier

Reputation: 1134

The other alternative is to construct new Widget object every time one is needed, but this must be expensive.

You'd be surprised. While Widget creation isn't free, in most cases, it is pretty cheap, even in DevMode. There are several widgets that are expensive to create in DevMode (specifically, the FlexTable/Grid), but those speed up drastically once you compile the application.

There doesn't seem to be any pattern specifically for this, nor can I point to any one guru giving a best practice, but in our application we had previously treated all of our top level widgets as singletons and it caused a load of issues for us. (Managing state between different screens, handling event bus events, stale data on the screen when not cleared)

We just refactored most of the code to be created on demand more (and subsequently destroyed when no longer needed) and we gained simpler code and a faster startup time.

In the end, if you do create widgets on demand, there may be a few cases where you need to increase performance by making those widgets singletons, but I think you'll find that only a few would warrant that effort.

All in all, don't assume something will be slow if you haven't tried it, and don't preemptively optimize your code.

Upvotes: 4

Related Questions