Tuesday, May 31, 2016

Singleton Pattern


Singleton pattern ensures a class can have only single instance during the lifetime of an application.

When to use Singleton Pattern?

When you want to create single object only once during application life you can go with Singleton Pattern. The implementation and use of singleton pattern is very easy. See below example.

Code –
public class Singleton
{
    //private static member.
    private static Singleton instance;
   
    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }
   
    //This static method responsible for creation of instance of this class.
    public static Singleton GetInstance()
    {
        //create new instance only in first call, rest of the time it will return existing object
        if (instance == null)
            instance = new Singleton();

       return instance;
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}
class Program
{
    static void Main(string[] args)
    {
        Singleton singleObject =  Singleton.GetInstance();
        singleObject.Display();
        Console.Read();
    }
}

Output –



Another way (via Property) to implement singleton pattern.


public class Singleton
{
    //private static member.
    private static Singleton instance;

    //Private constructor prevents instance creation of class from outside.
    private Singleton() { }

    //This static method responsible for creation of instance of this class.
    public static Singleton Instance
    {
        get
        {
            //create new instance only in first call, rest of the time it will return existing object
            if (instance == null)
                instance = new Singleton();

            return instance;
        }
    }

    public void Display()
    {
        Console.WriteLine("Display Method called from Singleton Class...");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Singleton singleObject = Singleton.Instance;
        singleObject.Display();
        Console.Read();
    }
}

Output –

Same as above example.

Advantages - 

  • Class can perform additional functionalities other than creation of an object.
  • This is referred as Lazy Initialization approach since instance of an object is not created until user call instance property or method. This approach avoids instantiating singletons class when application starts.

Disadvantage -

  • Singleton pattern is not thread safe by default. You need to write additional code to make it thread safe. I'll post article for Thread safe singleton pattern in few days.

Hope you liked this article. Please leave your feedback.

References –


See Also –

1 comment:

  1. Hi Mitesh,
    Thanks for your writings on your blog. I have enjoyed all of your WPF articles and used it as a cheetsheet for interviews. And i'll use the design patterns cheatsheet too :). Please cover other design patterns. Thanks again Mitesh.

    ReplyDelete