1

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

brakebg
  • 414
  • 3
  • 11
  • 24
  • Correct me if I'm wrong (I haven't used Java in a while), but it doesn't look like anything is actually enforcing that object to be a singleton. Sure, you're marking it with that decorator, but I don't think it is going to perform magic for you. In my experience, you have to use a DI system like Guice (Gin for GWT). – Travis Gockel Jun 17 '11 at 07:17
  • It's my mistake i haven't mentioned, i m using Gin/Guice in that application. I will update the post. – brakebg Jun 17 '11 at 07:22

3 Answers3

12

@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.
Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • is there some way to make Ginjector instance forget about already created singletone and create the new one? – hsestupin May 30 '13 at 11:19
1

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;

...
...

}
beastieboy
  • 91
  • 1
  • 3
0

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.

Stein G. Strindhaug
  • 5,077
  • 2
  • 28
  • 41
  • DI pattern do the singleton, in my case Gin/Guice . The question is because Template is singleton , is it shared by the entire application and what happen when different users set their content via setContent() method ? – brakebg Jun 17 '11 at 07:29
  • (Don't know anything about Gin) If the singleton is in the gwt code (the frontend) it will be a one singleton per browser window. A different user is using a different computer and will of course not share the singleton. (If it's used in the backend it will depend on how the webserver works [most likely one per thread/session or something]) – Stein G. Strindhaug Jun 17 '11 at 07:58
  • Thanks Stein.That is what i wanted to know! Singleton is shared per browser window. – brakebg Jun 17 '11 at 08:04
  • Or probably rather by browser javascript thread, I assume. Possibly some (old) browsers may not properly separate the namespace for javascript threads (although this would be really bad for security). You can run the same app in two tabs in firefox and chrome (and possibly others) without interference, although the any session cookie is probably shared between all tabs/windows for the same domain, making it unlikely that the singleton would be an issue... – Stein G. Strindhaug Jun 17 '11 at 08:51