Design Patterns - Singleton pattern

----------------------------------------
What is Singleton pattern:-
--------------------------------------------
        1.Creational type of design pattern
        2.Ensures a class has only one instance and provides a global point of access to it.
        3.Example:- [log writer class]
        4.single thread-safe object is shared 

-------------------------------
 ADVANTAGES OF SINGLETON
 -------------------------------
  1.  singleton makes sure every client accesses it with no  deadlock or conflict.
  2. it only allows one instance of a class responsible for sharing the resource, across the application.
------------------------------------------
Singleton class vs. Static methods
 ------------------------------------------
1.A Static Class cannot be extended whereas a singleton class can be extended.
2.A Static Class can still have instances (unwanted instances) whereas a singleton class prevents it.
3.Unlike static classes, we can use singletons as parameters to methods, or objects.

------------------------ 
GUIDELINES
 ---------------------

1.ONE INSTANCE:-It should creates only one instance of the class, created at one point only

2.PRIVATE CONSTRUCTOR:-The singleton's class constructors should be private so that no class can directly instantiate the singleton class.

3.STATIC PROPERTY/METHOD:-There should be a static property/method that takes care of singleton class instantiation and that property should be shared across applications and is solely 
responsible for returning a singleton instance.

4.SEALED CLASS:-The C# singleton class should be sealed so that it 
could not be inherited by any other class.

------------------------
Types of Singleton
------------------------
1. Thread Safety Singleton
      1.thread-safe.
      2.The biggest problem with this is performance; performance suffers since a lock is required
         every time an instance is requested.
 //basic singleton
    public class SingleTonBasic
    {
        private SingleTonBasic()
        {
            Console.WriteLine("Singleton instance is created in CONSTRUCTOR.");
        }

        private static object lockingObject = new object();
        private static SingleTonBasic singleTonObject;
        public static SingleTonBasic Instance
        {
            get
            {
                lock (lockingObject)
                {
                    if (singleTonObject == null)
                    {
                        singleTonObject = new SingleTonBasic();
                        Console.WriteLine("Singleton instance is created in InstanceCreation method");
                    }
                }
                return singleTonObject;
            }
        }
        public void Logwriter()
        {
            Console.WriteLine("singleton logger");
        }
    }

2. Thread Safety Singleton using Double Check Locking

 public class SingleTon
    {
        private SingleTon()
        {
            Console.WriteLine("Singleton instance is created in CONSTRUCTOR.");
        }
        private static object lockingObject = new object();
        private static SingleTon singleTonObject;
        public static SingleTon InstanceCreation()
        {
            if (singleTonObject == null)
            {
                lock (lockingObject)
                {
                    if (singleTonObject == null)
                    {
                        singleTonObject = new SingleTon();
                        Console.WriteLine("Singleton instance is created in InstanceCreation method");
                    }
                }
            }
            return singleTonObject;
        }
    }
3. Thread Safe Singleton without using locks and no lazy instantiation
4. Lazy Singleton :-  By default, Lazy<T> objects are thread-safe

1. By default, Lazy<T> objects are thread-safe
Lazy initialization is a technique that defers the creation of an object until the first time it is needed. 
In other words, initialization of the object happens only on demand.
Benefits:-
1. improves the performance
2 avoid unnecessary load till the point object is accessed
3. Reduces the memory footprint on the start-up
4. faster application load

Non-Lazy or eager loading
1. pre-instantiation of the object

2. commonly used in lower memory footprints


 public sealed class LazySingleton
    {
        static int instanceCounter = 0;
        private static readonly Lazy<LazySingleton> singleInstance = new Lazy<LazySingleton>(() => new LazySingleton()); //private static Singleton singleInstance = null;
        private LazySingleton()
        {
            instanceCounter++;
            Console.WriteLine("Instances created " + instanceCounter);
        }

        public static LazySingleton SingleInstance
        {
            get
            {
                return singleInstance.Value;
            }
        }
        public  void LogMessage(string message)
        {
            Console.WriteLine("Message " + message);
        }

    }

Comments

Popular posts from this blog

JWT - Token based authentication