GWT 项目中的单例
有人能给我解释一下吗?这是一些场景。
假设我有一个类模板并在应用程序中使用 Gin/Guice。
@Singleton
public class Template extends Compose
{
private HorizontalPanel header;
private HorizontalPanel content;
private VerticalPanel menu;
public Template()
{
this.add(initHeader());
this.add(initMenu());
this.add(initContent());
}
public void setContent(Widget widget)
{
content.clear();
content.add(widget);
}
.............
......
}
在入口类中,
........
public void onModuleLoad()
{
RootPanel.get().add(new Template());
....
}
每次我需要重新加载内容时,我都会这样做..
例如
HorizontalPanel hp = new HorizontalPanel();
hp.add ....
...
Template template = injector.getTemplate(); // return singleton instance using gin
template.setContent(hp)
等等..
所以,模板是单例的,据我所知,单例实例是每个虚拟机一个,这意味着整个应用程序共享,对吧? 模板类有标题、菜单和内容,其想法是仅重新加载内容部分作为清理和添加小部件。 但这是一个好方法吗?
例如,我们是否会遇到这样的情况:用户“A” setContent(widgetA) ,但同时用户“B”使用方法 setContent(widgetB) ,那么这里会发生什么?
谢谢,如果有人最终能与我分享一种好的方法并发表评论。
问候
Could some explain me something. Here is some scenario.
Let assume i have a class template and use Gin/Guice in the app.
@Singleton
public class Template extends Compose
{
private HorizontalPanel header;
private HorizontalPanel content;
private VerticalPanel menu;
public Template()
{
this.add(initHeader());
this.add(initMenu());
this.add(initContent());
}
public void setContent(Widget widget)
{
content.clear();
content.add(widget);
}
.............
......
}
and in the entry class
........
public void onModuleLoad()
{
RootPanel.get().add(new Template());
....
}
Every time i need to reload the content i do..
For example
HorizontalPanel hp = new HorizontalPanel();
hp.add ....
...
Template template = injector.getTemplate(); // return singleton instance using gin
template.setContent(hp)
and so on..
So, Template is singleton and as far as i know singleton instance is one per VM meaning shared by entire application, right?
Template class has header, menu and content, the idea is to reload only the content part as cleaning and adding widgets.
But is this a good approach?
For example, could we have a situation like user "A" setContent(widgetA) ,but in the same time user "B" use method setContent(widgetB) ,so what is going to happen here?
Thanks, if anyone could share with me a good approach eventually and comment that one.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Singleton
的作用域为Ginjector
实例(是的,如果您GWT.create()
两次GInjector
,你会得到两个“单身人士”)。没有单一的手段 GIN 可以以某种方式在onModuleLoad
中“拦截”您的new Template()
,因此injector.getTemplate()
将返回一个不同的 <代码>模板实例。(这与 Stein 所说的“单例代码反模式”完全不同,使用静态状态)
没有什么魔法:GIN 是一个代码生成器,它只编写您可以输入的代码手。
至于您的其他问题:
@Singleton
is scoped to theGinjector
instance (yes, if youGWT.create()
yourGInjector
twice, you'll get two "singletons"). There's no single mean GIN can somehow "intercept" yournew Template()
inonModuleLoad
, soinjector.getTemplate()
will return a distincttemplate
instance.(this is totally different from the "singleton code anti-pattern" that Stein talks about, using
static
state)There's no magic: GIN is a code generator, it only writes code that you could have typed by hand.
As for your other questions:
我已经为我们的应用程序注入了通用 RPC 代码的类。
方法如下:
我们的 Gin 模块:
它作为单例注入,如下所示:
I have injected the class with common RPC code for our app.
Here's how:
Our Gin module:
And it's injected as singleton as follows:
我很确定 GWT 编译器会忽略该注释。
当我在 gwt 中需要 Singleton 时,我只需创建一个带有私有/受保护构造函数的类,以及一个
private static NameOfSingletonClass 实例;
和一个初始化实例的getInstance()
方法如果为 null 并返回实例。I'm pretty sure the annotation is ignored by the GWT compiler.
When I need a Singleton in gwt i just create a class with a private/protected constructor, and a
private static NameOfSingletonClass instance;
and agetInstance()
method that initializes the instance if null and returns the instance.