Will referring to a single ClientBundle class from

2019-06-24 04:01发布

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?

标签: gwt uibinder
1条回答
姐就是有狂的资本
2楼-- · 2019-06-24 04:41

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)

查看更多
登录 后发表回答