I'm implementing a notification framework for one of my projects. As i want it to be very generic, the user can use several transport layers, so that he doesn't really need to care about using one delivery method (lets say WCF) or another (for eg ActiveMQ). The interface the user has access is of course independent of the delivery method (WCF or ActiveMQ). Still, the two classes (consumer and producer) implements singletons, so they actually use default constructors (meaning, no parameters). My problem is that i would like to have one parameter, the delivery method the user want to use. But as far as i know, singleton only use default constructors? which is normal, as there should be no point of using singletons with parameters. So, what are my options here? not to create a singleton? create a method to set the delivery method?
Sebastian
You can certainly have parameters with singletons, except instead of passing the parameter into a constructor, you are passing it into the getInstance() method. Your overridden constructor needs to be private of course for a true singleton implementation. My example is written in Java but applies for C# as well.
Example:
Singleton s = Singleton.getInstance(42);
In the Singleton code:
private Singleton() {
}
private Singleton(int num) {
//set num
}
public getInstance(int num) {
//singleton code (calls the private non-default constructor if an object
//does not already exist)
}