GWT 项目中的单例

发布于 2024-11-15 15:35:47 字数 1114 浏览 3 评论 0原文

有人能给我解释一下吗?这是一些场景。

假设我有一个类模板并在应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-11-22 15:35:47

@Singleton 的作用域为 Ginjector 实例(是的,如果您 GWT.create() 两次 GInjector,你会得到两个“单身人士”)。没有单一的手段 GIN 可以以某种方式在 onModuleLoad 中“拦截”您的 new Template(),因此 injector.getTemplate() 将返回一个不同的 <代码>模板实例。
(这与 Stein 所说的“单例代码反模式”完全不同,使用静态状态)

没有什么魔法:GIN 是一个代码生成器,它只编写您可以输入的代码手。

至于您的其他问题:

  • 您的客户端代码显然在客户端上运行,即在浏览器上运行。每个浏览器选项卡/窗口都有一个显示您的应用程序的“应用程序实例”。不存在同时存在“用户 A”和“用户 B”的情况。
  • JavaScript 是单线程的,因此您也不必担心并发访问。

@Singleton is scoped to the Ginjector instance (yes, if you GWT.create() your GInjector twice, you'll get two "singletons"). There's no single mean GIN can somehow "intercept" your new Template() in onModuleLoad, so injector.getTemplate() will return a distinct template 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:

  • You client code obviously run on the client, i.e. on the browser. There's one "application instance" per browser tab/window displaying your app. There's no "user A" and "user B" at the same time.
  • JavaScript is single-threaded, so you don't have to fear for concurrent accesses either.
嘦怹 2024-11-22 15:35:47

我已经为我们的应用程序注入了通用 RPC 代码的类。
方法如下:

@Singleton
public class SomeService {

/** The real service. */
private static final RealServiceAsync realService;

...

}

我们的 Gin 模块:

public class MyGinModule extends AbstractGinModule {

    @Override
    protected void configure() {
        bind( SomeService .class ).in(Singleton.class);
        ...
        ...
    }

}

它作为单例注入,如下所示:

public class ApplicationInfoPresenter {

@Inject
private SomeService service;

...
...

}

I have injected the class with common RPC code for our app.
Here's how:

@Singleton
public class SomeService {

/** The real service. */
private static final RealServiceAsync realService;

...

}

Our Gin module:

public class MyGinModule extends AbstractGinModule {

    @Override
    protected void configure() {
        bind( SomeService .class ).in(Singleton.class);
        ...
        ...
    }

}

And it's injected as singleton as follows:

public class ApplicationInfoPresenter {

@Inject
private SomeService service;

...
...

}
高速公鹿 2024-11-22 15:35:47

我很确定 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 a getInstance() method that initializes the instance if null and returns the instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文