views:

232

answers:

7

Hello all

I have some class it is a singleton we have this class in already several applications and it is used there as a singleton.

Now i am writing some new application and i need several instances of that class , what is a best practice to have sevaral instances of it ?

By deriving from it and making the private constructur to be public, I work with c# ?

Or there can be someother idea here ?

Thanks

+12  A: 

Simple: don't make it a singleton. The word 'single' is there for a reason.

BoltClock
+4  A: 

It would seem to me that if you need several instances of this class, then you would handle it like any other and you would remove anything in the class that forces it to be a singleton, which then it would simply cease to be a singleton.

Philip Regan
+3  A: 

You might consider a factory class that is configurable to always return the same instance or give you different instances based on configuration or other criteria.

Neil Whitaker
Liked your answer but the multiton is exactly what I need .
Night Walker
A: 

What you're asking for is very contradictory--could you define what you mean by Singleton to make sure we're not using different vocabulary?

If the existing object is a true static singleton then you likely won't be able to instantiate multiple instances; in order to do so you would need to load each instance (and it's callers) in their own AppDomain which is not trivial and would be a terribly large tradeoff/hack just to get multiple Singletons.

STW
+9  A: 

There is supposed to be only one Singleton. If you have more than one, it's not a Singleton.

Maybe a Multiton is what you want?

NullUserException
+1 Never heard of that pattern! Seems really interesting.
BoltClock
This is exactly what I need , never heard about it .
Night Walker
A: 

It really depends on how exactly is the class implemented as a singleton.

If the class has a private default constructor and the singleton instance is create through a static factory method, your only option is to derive from the class (as long as it's not sealed) and provide a public constructor on the derived class to create multiple instances.

If the class has a public constructor, and the singleton usage is just a guidance, but not enforced, you can just create as many instances as you have.

Note however, that if the class was designed as a singleton, it's quite likely that its implementation makes that assumption internally, so having multiple instances might have unexpected side effects. You should really ensure that the class is implemented so that is allows such usage scenario.

Franci Penov
+1  A: 

A singleton is a design pattern which provides you two guarantees:

  • that exactly one instance of the class will exist, and
  • that this instance is globally accessible.

If you remove the first requirement, what you have is no longer a singleton. It is a plain, old-fashioned global.

So call it by its real name. What you need is a simple global variable. You can wrap it in some kind of lazy initialization logic if you need it, but it's a global, not a singleton.

Apart from this, Singletons are a really really bad idea. Don't use them in the first place.

jalf