views:

291

answers:

5

Should a Singleton class be allowed to have children? Should we seal it? What are the pro's and con's?

For being able to inherit from a Singleton class, we would have to make the constructor protected instead of private. Now, that will be fine in c#, but the protected word in java gives both child-classes and package-classes access to the constructor. Which means not only classes that inherit from our Singleton can access the constructor but other classes in the same package can do it.

I'm a bit confused about all this facts. Maybe I am making a big fuss about nothing to worry about too much? Until now, I never had any necessity of trying to inherit from a Singleton, so maybe this is just an academic question!

Thanks

+6  A: 

A better solution is to use an IoC Container framework to handle the "Singleton" (lifetime) aspect of the class. At that point you can use a POJO and simply inherit from it.

EDIT: Some links that may help:

Look for ones that handle lifetime management. You want to be able to configure the container (which acts like a generic factory) so that requests for instances of "Singleton" classes always return the same instance (at least for the same container). You generally only have one container instance at the highest level of your application.

TrueWill
What is an IoC Container Framework? How do I make one?
devoured elysium
IoC == Inversion of Control
Jay Elston
IoC is also described as Dependency Injection (DI)
Stephen C
+1. You should handle the lifecycle of an object (how many instances of it exist) separate from the actual logic/code within the object. Mixing the two leads to ugliness.
matt b
+3  A: 

If you can extend from a singleton, then it means that you can have more than one instance of it. This contradicts the whole singleton idea. Just make it a "normal" class and write the code around it accordingly that you instantiate it only once and use the same instance forever. If you need it to be. Dependency injection can help a lot in this.

BalusC
It does not mean that there can be more than one instance of it. However, there must be a mechanism to ensure subclasses can set their instance as _the_ instance.
zneak
And that's only possible if the constructor is private and the instance is static. All with all it just doesn't make any sense. How hard can it be to instantiate a simple class only once and use it forever in application's lifetime?
BalusC
I think we just misunderstood each other. I meant that extending from a singleton class didn't mean outright that there can be more than one instance of it.
zneak
+3  A: 

Yes, Singletons should be sealed. No, they should not be inherited.

The reason is that the primary (really only) behaviour of a Singleton is to create an instance of itself at some predetermined time. Since this functionality is static, it can't be overridden, so it would have to be duplicated instead. Once you start duplicating, you have multiple instances of a singleton, which doesn't really make sense.

Either that or you end up with race conditions or other conflicts as the "derived" singletons fight for control over the global instance, which not only doesn't make sense, it's dangerous.

There are some hackish workarounds to the Singleton inheritance problem, but that is what they are - hacks. The Singleton pattern is not really suitable for inheritance.

Aaronaught
+1  A: 

If you want to use inheritance in conjunction with the singleton pattern, you should put the inheritable state and behaviour into an abstract base class and define the singletons as (final) subclasses.

Stephen C
+2  A: 

Hmm, sounds like a question many of us got asked at school when learning about Singletons. I'd suggest reading the following links.

http://msmvps.com/blogs/jon_skeet/archive/2006/01/20/singleton-inheritance.aspx http://msdn.microsoft.com/en-us/library/84eaw35x.aspx

Many would strongly discourage inheriting from a singleton, but real-life programming often requires exactly such an approach. I personally would say, don't inherit from a singleton as a rule of thumb, but don't lock yourself into a design which blocks off such an approach. A pragmatic development approach is always the best approach...

code4life
"real-life programming often requires exactly such an approach" [citation needed]
Aaronaught
"Many would strongly discourage inheriting from a singleton" - I'd strongly discourage using Singletons (or other global data) in the first place.
TrueWill
@TrueWill, I see your point, but the realities of a software project forces our technical approach. For instance, I worked on a corporate loans web app for a major bank 5 years ago. It had cleanly separated layers handling hundreds of reporting layers, each layer performing a specific XSLT transforms. However the number of concurrent users grew so much that we physically ran out of memory on the prod server. We were able to chop down memory usage to 1/50th simply by utilizing global static variables for information that needed to be read only 1 time and would be consistent for all sessions.
code4life
@code4life - certainly the need to cache or pool limited resources is a reality. Global statics and Singletons are not the only way to accomplish that. The same goals can be accomplished with dependency injection, especially when a DI/IoC Container framework is used. This can improve testability and maintainability.
TrueWill