Singleton - how to

Hi. I’d like to use Singletons. Maybe the following singleton pattern class would do this:
(Addendum: doesn’t work, because of the static sInstance for any singleton inheritance)

abstract public class Singleton
{
  private static Singleton sInstance;

  public Singleton()   {
    assert sInstance == null;
    sInstance = this;
  }

  public static Singleton getInstance() {
    assert sInstance != null;
    return sInstance;
  }
}

Now let’s define a class which wants to be a singleton.

class Example extends Singleton
{
  public void method() {
    // bla
  }
}

Finally let’s fetch the instance of the example class

{
  // Once the example class is being created.
  Example mMyexample = new Example();
}

{  
  // Later on, we want to access the singleton class
  Example ex = (Example) Example.getInstanz();  // How to avoid the Cast?
  ex.method();
}

Is it possible to avoid the cast operation?

usually the singleton interface is implemented in the specific class - and not inherited. In that way, no cast is ever needed. If you do choose to implement the singleton pattern using inheritance, you will need to cast.

Usually a private constructor is used (no inheritance).

I see.
This (=no inheritance) has been the way I did it, but… then I thought: Do I really have to implement the above singleton methods in any class which wants to be a singleton?

Obviously I have to do so.
Thank you.

The approach you were attempting is more commonly done in C++, where they have a few more tricks up their sleeves they can use to pull it off. In particular they can use templates to avoid the casting.

You can of course just use all static methods and avoid the singleton object all together. That’s also nice in some cases.

[quote]The approach you were attempting is more commonly done in C++, where they have a few more tricks up their sleeves they can use to pull it off. In particular they can use templates to avoid the casting.
[/quote]
Yes.

[quote]You can of course just use all static methods and avoid the singleton object all together. That’s also nice in some cases.
[/quote]
This is the way I do it now. Any singleton class has its own few lines of code for the handling of the static sInstance. No casting. However I’d still prefer an abstract singleton base class with no casting.

In my original inheritance example there’s a mistake:

abstract public class Singleton 
{ 
  private static Singleton sInstance; 

This won’t work because any inherited class of the Singleton base class will use the same sInstance.

How would a real Singleton base class have to work in Java - with casting then?

But even if static fields could be overridden, you’d still have the “problem” that there’s no multiple inheritance in Java. So your singleton could never descend from anything else, and a Singleton interface wouldn’t work.

I suppose you could have a SingletonFactory that knows how to create instances of certain classes. Whenever you do something like getSingleton(“one.of.my.Classes”), it’d either create the instance or fish it out of a Map. But then you’d have to make all those classes in the same package as the singleton for any kind of privacy, and it’s just ugly. Java can’t really be bent like this.

Thanks, I see.

Well, the manual way is OK, too. It means to have 4 little lines of code + 1 line of var declaration in each class which wants to be a singleton, that’s pretty OK.

Java is good. I like it much.