Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton class for an object with parameters

I realized that I should have only one instance of an object called StdSchedulerFactory running at a time. So far I instantiated the object like this

StdSchedulerFactory sf = new StdSchedulerFactory(properties);

And properties is a NameValueCollection. How can I write a Singleton class for this object so that the variable sf will always have one instance throughout the program?

like image 413
disasterkid Avatar asked Oct 22 '14 17:10

disasterkid


1 Answers

Part of the Singleton pattern is typically a private constructor, so that other classes can not make new instances.

The workaround for parameters coming from outside the class is to add a "Init" or "Configure" function:

public static void Configure(NameValueCollection properties)
{
}

Of course, if you forget to call this function, you may get behavior you don't want; so you may want to set a "Configured" flag or something like that so your other functions can react appropriately if this function has not yet been called.

like image 120
BradleyDotNET Avatar answered Nov 02 '22 05:11

BradleyDotNET