Rollin_s
Rollin_s

Reputation: 1983

Generalized Singleton base class in Java

In C++, I understand that it is possible to create a Singleton base class using templates. It is described in the book, Modern C++ Design, by Andrei Alexandrescu.

Can something like this be achieved in Java?

Upvotes: 2

Views: 4304

Answers (5)

Mehmet Mustafa Demir
Mehmet Mustafa Demir

Reputation: 97

You can use a static HashMap(or Hashtable for thread safety) to store all instances for different templates as @reed-copsey mentioned. Example Singleton class:

public class Singleton<T extends Parent> {
    private static HashMap<Class, Singleton> instanceMap = new HashMap<Class, Singleton>();

    private Singleton() {

    }

    public static <T> Singleton getInstance(Class<T> tClass) {
        if (!instanceMap.containsKey(tClass))
            instanceMap.put(tClass, new Singleton());
        return instanceMap.get(tClass);
    }

}

You can use this singleton as:

public static void main(String[] args) {
    Singleton instance1 = Singleton.getInstance(Child1.class);
    Singleton instance2 = Singleton.getInstance(Child1.class);
    assert instance1 == instance2;

    Singleton instance3 = Singleton.getInstance(Child2.class);
    assert instance1 != instance3;
}

Singleton class will return different instance for different class types.

Upvotes: 1

ConcernedOfTunbridgeWells
ConcernedOfTunbridgeWells

Reputation: 66622

In 1997 I tried to do this with JDK 1.0.2 and came to the conclusion that it was not possible to make an inheritable singleton. As far as I can tell Java has not gained any new features that would allow this.

However, doing this manually does not involve a lot of boilerplate code, maybe 10 lines or so per class. If you need to make some sort of anonymous factory mechanism the singletons could implement an interface with placeholders for the instantiation protocol.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564423

I have yet to see a satisfactory solution for this in Java. There are many examples using templates in C++ or generics in .NET, but Java's generics implementation has some limitations with static members that make this difficult.

The best option I've seen is to use a static HashMap, and a factory class. This allows you to make a single factory that can serve multiple Singletons of any class.

For an implementation, see this post.

Upvotes: 3

erickson
erickson

Reputation: 269687

Because Java's generics are not reified, it isn't possible to implement a generalized Singleton in the same way. Because the generic types are "erased" from the compiled code, the singleton would have to be explicitly initialized at runtime.

The upshot is, a Singleton template would not provide much value in Java.

Upvotes: 3

Aaron Digulla
Aaron Digulla

Reputation: 328614

Yes, but all approaches have their flaws. They are either expensive (using synchronized) or make testing impossible (final static).

See the links to the right:

"State of the art" is to use a container or factory which creates the singletons as needed and keeps them in some kind of cache. See Spring, Guice, etc.

Upvotes: 2

Related Questions