Singleton – One of a Kind Objects (Part I)

SINGLETON PATTERN

  • What is Singleton pattern?
    Singleton Pattern insures that only one instance of a Type is created and that Type provide a global point of access for that instance.
  • How it’s implemented?
    Implementing a singleton type is quite simple (if synchronization is not an issue for you). Type should define a static member variable which will contain reference to type’s instance. Make type’s instance constructor as Private so that no other type can create instance of our singleton class.
    We need to add one static method that will check weather our static instance variable is null or not. If it is null then we will add code so that our static variable will refer to newly created type’s instance. Lastly we will return that static variable.
    This way, there will always be one and only one instance of a type around (just forget about multi-threading for the time being).
  • How it looks when coded?

    internal sealed class MySingleton
    {
    //static variable to hold instance reference
    private static MySingleton onlyInstance;

    private MySingleton();

    //Global i.e. public and static point of access
    public static MySingleton GetMySingletonInstance()
    {
    if (onlyInstance==null)
    {
    onlyInstance=new MySingleton();
    }
    return onlyInstance;
    }
    }

  • How multi threading spoils our Singleton party?
    Multi threaded applications are quite common these days. Now suppose we have multiple threads running in our MySingleton application. I’ll name two of my application threads as AngelThread and DevilThread. AngelThread and DevilThread both requires MySingleton instance, so they both try to call MySingleton.GetMySingletonInstance().
    AngelThread checks onlyInstance for null, as we haven’t created any instance yet this returns true and code next to be executed by AngelThread is,”onlyInstance=new MySingleton();”
    Now, CPU realizes that AngelThread has got enough CPU time slice, let’s switch to DevilThread. Remember AngelThread still haven’t instantiated MySingleton yet. DevilThread also checks onlyInstance for null, and obviously true is returned and DevilThread is just about creating a brand new MySingleton object. DevilThread created the object and returns its reference. Our CPU interrupts and wakes up AngelThread so that it can continue its execution. Aha! AngelThread also creates a brand new object and returns. So we have two objects of MySingleton type. Multi threading ruined our singleton dream.

    to be continued…

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s