Which is the best way to use generics to write an abstract base class or an Interface for a Singleton?
I've many lazy created singletons in my code. Lazy means they create themself when the first user class declares the singleton class. It looks like this:
1 2 3 4 5 6 7 8 9 10
| public class TheSingelton { private static TheSingelton sInstance = new TheSingelton();
private TheSingelton() {}
public static TheSingelton getInstance() { return sInstance; } |
The con is that for every class which wants to be a Singleton it has to double these lines with its own type.
In C++ I used to use templates for these kind of things. I guess with generics we finally can have a real Singleton base abstract class or Interface in Java, too?
(I'd prefer an abstract class because even the sInstance shall be inside the base class, not in the derivate.)