4

I have a single ClientBundle with css resources that are needed throughout the application - default background colors, common layout patterns, etc.

A stated design goal from GWT is that there be "no penalty for having multiple ClientBundle resource functions refer to the same content." Do I have to do anything to avoid penalties and help ClientBundle realize this goal?

A naive approach would be to just add

<ui:with field="appWideResources"
    type='com.activegrade.client.resources.appwide.AppWideResources' />

at the top of every UiBinder file. But, in my 22 jillion UiBinder files, will this create 22 jillion instances of AppWideResources, each with different and redundant obfuscations, etc?

Riley Lark
  • 20,660
  • 15
  • 80
  • 128

1 Answers1

5

You could do it that way or via the @UiFactory method, and neither would cost you anything additional. GWT will only instantiate a resource set once and share that single instantiation with every file that references that set.

"...each time you call GWT.create() (which a ui:with will do) it will instantiate a new object, but all resources in the ClientBundle are initialized as static fields, so each instance is only a very lightweight "proxy" to those static fields; and the GWT compiler will optimize it out in the end, (almost) as if you had a singleton instance." (Thomas Broyer)

Chris Cashwell
  • 22,308
  • 13
  • 63
  • 94
  • This is the answer I was hoping for! Thanks! – Riley Lark Nov 30 '11 at 19:07
  • 1
    This is not exactly true, but it doesn't change the conclusions: each time you call `GWT.create()` (which a `ui:with` will do) it will instantiate a new object, but all resources in the `ClientBundle` are initialized as static fields, so each instance is only a very lightweight "proxy" to those static fields; and the GWT compiler will optimize it out in the end, (almost) as if you had a singleton instance. – Thomas Broyer Dec 01 '11 at 09:14
  • @ThomasBroyer Thanks for the clarification- that is what I meant, I guess my execution just wasn't as clear as yours. I'll update my answer to include your notes. – Chris Cashwell Dec 01 '11 at 23:59