Singleton Design Pattern in C#

 

Singleton design pattern is useful whenever we want to maintain only one instance of the class and that instance creation also class has to take care. That means in singleton design pattern class itself provides instance of it.

 

The basic rules while creating Singleton design pattern is Class needs to have private constructor to avoid the creating object from outside, Class should declare with sealed keyword to avoid the inheriting from other class and finally class should declare as public because it has to accessible for other class.

 

As shown below lets create singleton design pattern by following above rules.

 

public sealed class Company

{

        private static Company singleton;

 

        ///<summary>

        /// private constructor to avoid the instantiating the class outside

        ///</summary>

        private Company() { }

 

        public static Company GetInstance()

        {

            if (singleton == null)

            {

                singleton = new Company();

            }

            return singleton;

        }

}

 

As shown above we created the Company class has singleton class. Here we are creating the instance of the Company class through Lazy initialize, means instance of the class created whenever GetInstance() method is called. Even though if you call the Company class for other purpose the instance will not create.

 

Lazy initialize is not thread-safe because as shown above in multi-threaded environment if both threads executes if (singleton == null) at same time, both will find as singleton == null. So two instances will create which is not the basic concept of Singleton.

 

To avoid the creating of two instances we will use locking mechanism as shown below.

 

public sealed class Company

{

        private static Company singleton;

        private static readonly object objlock = new object();

 

        ///<summary>

        /// private constructor to avoid the instantiating the class outside

        ///</summary>

        private Company() { }

 

        public static Company GetInstance()

        {

            if (singleton == null)

            {

                lock (objlock)

                {

                    singleton = new Company();

                }

            }

            return singleton;

        }

}

 

As shown above we created one object whenever class gets called and locking that object. So only one thread can execute the code within the locking. So we can avoid multi instances here, but it is very lazy because the instance will not create until GetInstance() method called.

 

We can avoid the lazy initialize by using Eager initialize with thread-safe environment. Let’s create the instance of singleton class by using Eager initialize as shown below.

 

public sealed class Company

{

        private static readonly Company singleton = new Company();

 

        ///<summary>

        /// private constructor to avoid the instantiating the class outside

        ///</summary>

        private Company() { }

 

        public static Company GetInstance()

        {

            return singleton;

        }

}

 

As shown above we are creating the instance of the class whenever any request comes for Company class, this is called Eager initialize in Singleton design patter. As instance is declared as static it will create only one instance even though multiple requests came at the same time.