Sunday, 13 April 2014

Create a Singleton with different initialization - C#

In this article we are going to see how to create a singleton instance of a class. singleton is design pattern which uses the only single instance of that class through the application when ever called.

Steps to implement :
1. Make the class a Sealed, because it is a singleton there is no need of derivation here.
2. To avoid the creation of new instance of that class outside the class make a private constructor, which           makes the user to restrict from create a object from the outside but we can create instance inside a class.
3. create a static read-only variable which holds the object of that particular class
4. return the instance through the property or method.

Static constructor - Instantiate : Initialize the instance in the static constructor

    sealed class Singleton
    {
        private static readonly Singleton _instance = null;

        static Singleton()
        {           
            _instance = new Singleton();
        }

        private Singleton()
        {

        }

        public static Singleton GetInstance()
        {           
            return _instance;
        }

        public void Process(int a, int b)
        {
            Console.WriteLine(" {0} : {1}", a, b);
        }


    }

Eager - Instantiate : Initialize the instance in the declaration itself

    sealed class Singleton
    {
        private static readonly Singleton _instance = new Singleton();

        private Singleton()
        {

        }

        public static Singleton GetInstance()
        {
            return _instance;
        }

        public void Process(int a, int b)
        {
            Console.WriteLine(" {0} : {1}", a, b);
        }
    }


Lazy - Instantiate : Initialize the instance on the call

    sealed class Singleton
    {
        private static Singleton _instance = null;

        private Singleton()
        {

        }

        public static Singleton GetInstance()
        {
            if (_instance == null)
                _instance = new Singleton();

            return _instance;
        }

        public void Process(int a, int b)
        {
            Console.WriteLine(" {0} : {1}", a, b);
        }


    }

Thread safe - Instantiate : Make a lock to avoid the Thread Safe.

    sealed class Singleton
    {
        private static  Singleton _instance = null;
        private static object _lock = new object();

        private Singleton()
        {

        }

        public static Singleton GetInstance()
        {
            if (_instance == null)
            lock (_lock)
            {
              _instance = new Singleton();
            }

            return _instance;
        }

        public void Process(int a, int b)
        {
            Console.WriteLine(" {0} : {1}", a, b);
        }


    }


Call the singleton class and test whether the two object created are same 

   class Program
    {
        static void Main(string[] args)
        {              
            Singleton obj1 = Singleton.GetInstance();
            Singleton obj2 = Singleton.GetInstance();

            if(obj1.Equals(obj2))
            {
                Console.WriteLine("Same object");
            }
            else
            {
                Console.WriteLine("Different object");
            }

            obj2.Process(2, 1);
            Console.Read();
        }     
    }


Output:
Same object
2 : 1


From this article you can knew what are the various ways we can create a singleton pattern in c#.



No comments:

Post a Comment