Thread overview
Singleton
Apr 08, 2005
turpeine
Apr 08, 2005
Georg Wrede
Apr 11, 2005
Benji Smith
April 08, 2005
It would be great, if there could be singleton class inside language. Or is there?

singleton SFoo
{
this();
}


..

SFoo a();
SFoo b();

if (&a==&b) // this would be true


April 08, 2005
turpeine@lut.fi wrote:
> It would be great, if there could be singleton class inside language. Or is
> there?

Would you care to elaborate on the merits of having an in-built singleton class?

Also, (in general,) for such things to become a part of the language, there would have to be reasons like:

- hard to work around
- brittleness of work around
- major usefulness of new syntax
- substantial increase of stability/clearness of written code

or other weighty issues.

There may be some, but you'd have to explain them. (At least to thickies like me. :-) )
April 11, 2005
turpeine@lut.fi wrote:
> It would be great, if there could be singleton class inside language. Or is
> there?

Here's the way I would do it, using current syntax:

###################################################

class MySingleton {

  public static Object myObject = null;

  static {
    // perform some sort of initialization on myObject
  }

  // Private ctor prevents object from direct instantiation
  private MySingleton() {}

  public static getSingletor() {
    return myObject;
  }
}

###################################################

What's wrong with that?

A singleton is a simple pattern (like a factory) that can be **easily** implemented, without changing the language at all.

--BenjiSmith