Friday, May 10, 2019

C# : Design Pattern : Different flavor of Singleton design pattern implementation


Below code is showing different implementation of Singleton design pattern for Example

Thread safe singleton implementation:

 public sealed class Singleton
    {
        private static Singleton instance = null;
        private static readonly object locker = new object();
        private Singleton()
        {

        }
        public static Singleton GetInstance
        {
            get
            {
                if (instance == null) //double locking.
                {
                    lock (locker)//each time it will lock weather object create is required or not.
                                 //so to avoid unnecessary locking we will use double locking pattern
                                 // means apply lock only when instance is null. we will check instance==null before locking.
                    {
                        if (instance == null)
                        {
                            instance = new Singleton();
                            return instance;
                        }
                    }
                }
                return instance;
            }
        /*
       . other methods, properties of singleton class.
       .
       .
       .
       */
        }
    }

Note : class is sealed because no one should be able to inherit it. locker object is static and readonly so only one instance object will be present which will be used for locking purpose.
we are checking instance ==null two times to avoid unnecessary locking, locking will be applicable only if instance is null.

Eager loading Singleton Pattern:

 public sealed class SingletonEagerLoading
    {
        private static readonly SingletonEagerLoading instance = new SingletonEagerLoading();
        int counter = 0;
        private SingletonEagerLoading()
        {
            counter++;
            Console.WriteLine("No of instance=" + counter);
        }
        public static SingletonEagerLoading GetInstance
        {
            get { return instance; }
        }
     
        /*
        . other methods, properties of singleton class.
        .
        .
        .
        */

    }
// Note :No need to implement locking as we are creating instance at the time of declaration so framework will take care of instantiation and concurrency. (i.e. this implementation is Thread safe)

Lazy Loading Singleton Pattern :

public sealed class SingletonLazyLoading
    {
        private static readonly Lazy instance = new         Lazy(() => new SingletonLazyLoading());
        int counter = 0;
        private SingletonLazyLoading()
        {
            counter++;
            Console.WriteLine("No of instance=" + counter);
        }
        public static SingletonLazyLoading GetInstance
        {
            get { return instance.Value; }
        }

        /* other methods, properties of singleton class.*/
        public static void Main() //To Test
        {
            SingletonLazyLoading obj1 = SingletonLazyLoading.GetInstance;
            SingletonLazyLoading obj2 = SingletonLazyLoading.GetInstance;

        }

    }

No comments:

Post a Comment

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...