It's not clear from your question whether you want a single singleton, which is allowed to be of the base class or any subclass, or whether you want separate singletons of the base class and every subclass. I'm guessing it's the former.
If you make the base class constructor store "this" as the singleton instance, in a static field of the base class, you have some of what you may want. You can't easily do lazy instantiation of the singleton, within a getSingleton() method, which people commonly like to do.
To have any sort of hierarchy of singleton classes, they need "protected" constructors. In Java, "protected" implies package access. This reduces the singleton-ness of the classes.
Singleton is, in many opinions, an over-used and mis-used pattern. One of the objections to it is that it doesn't play very nicely in complicated class hierarchies like you're describing. Are you sure you couldn't achieve what you want in a tidier way, without lots of singleton classes? [ April 02, 2007: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Very cool question ... trying to put the mechanics of making and accessing singletons into a base class. I think the answer is still "no" I don't know how to do it.
Following I think are must for singleton.
private constructor static method that you give you the reference of the object.
Neither of those are "musts" for Singleton, though they are far and away the most common elements in Java. Singleton is a design pattern, not a chunk of code, and there are other ways to achieve the two goals: controlling the number of instances (usually one, but maybe not) and providing access. See if you can think of a couple.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Nov 29, 2005
Yes , I agree with you.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com