disasterkid
disasterkid

Reputation: 7278

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?

Upvotes: 7

Views: 7926

Answers (3)

vidriduch
vidriduch

Reputation: 4863

this is my two favorite way implementing simple singleton pattern. The second one is just easier when debugging :)

public sealed class SingletonOne
{
    private static readonly Lazy<SingletonOne> instance = new Lazy<SingletonOne>(() => new SingletonOne());

    private Lazy<Controller> controller = new Lazy<Controller>(() => new Controller(properties));

    private static object properties = null;

    public static SingletonOne Instance { get { return instance.Value; } }

    public Controller GetController(object properties)
    {
        SingletonOne.properties = properties;

        return this.controller.Value;
    }
}

public sealed class SingletonTwo
{
    private static readonly SingletonTwo instance = new SingletonTwo();

    private Controller controller;

    private static object properties = null;

    public static SingletonTwo Instance
    { 
        get 
        { 
            return SingletonTwo.instance; 
        } 
    }

    public Controller GetController(object properties)
    {
        SingletonTwo.properties = properties;

        if(this.controller == null)
        {
            this.controller = new Controller(SingletonTwo.properties);
        }

        return this.controller;
    }
}

public class Controller 
{
    public Controller(object properties) { }
}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61349

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.

Upvotes: 9

Cam Bruce
Cam Bruce

Reputation: 5689

Here is a basic Singleton implementation. It is not thread-safe.

public sealed class StdSchedulerFactory
{
   private static readonly StdSchedulerFactory instance;
   private NameValueCollection _properties;

   private StdSchedulerFactory(NameValueCollection properties)
   {
       _properties = properties;
   }

   public static StdSchedulerFactory GetInstance(NameValueCollection properties)
   {
      if (instance == null)
      {
         instance = new StdSchedulerFactory(properties);
      }
      else
      {
         return instance;
      }
   }
}

Upvotes: 2

Related Questions