问题
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?
回答1:
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)
来源:https://stackoverflow.com/questions/8330660/will-referring-to-a-single-clientbundle-class-from-multiple-uibinders-cost-anyth