Wednesday 18 December 2019

Design Patterns - Singleton Design Pattern

Objective: Ensure a class only has one instance, and provides a global point of access to it.

Characteristics.

  • The class must be sealed.
  • A single constructor, that is private and parameterless.
  • A static variable that holds a reference to the single created instance, if any.
  • A public static means of getting the reference to the single created instance, creating one if necessary.

Non Thread Safe Singleton Design Pattern
namespace DesignPatterns
{
    public sealed class Singleton
    {
        // This static variable holds one instance of a class
        private static Singleton instance;

        // Private constructor will control the instantiation
        private Singleton() { }

        // Property responsible to create the single instace and return
        public static Singleton Instance
        {
            get
            {
                // If the static instance is null which means the instance is not yet created.
                if (instance == null)
                {
                    // Creats the static instance with the private constructor.
                    // This is the lazy object instance creation, 
                    // this will creates the object when the property is been called.
                    instance = new Singleton();
                }

                // The instace is ready, the static instance will return from here.
                return instance;
            }
        }

        // Other class methods.....
    }
}
This will make sure the single instance is created but it is not thread safe.
Thread Safe Singleton Design Pattern
public sealed class Singleton
    {
        private static readonly Singleton instance;

        // a static constructor is guaranteed to be called only once per AppDomain, 
        // and only when it’s needed (a static member is called or an instance of that object is created).
        static Singleton()
        {
            instance = new Singleton();
        }

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }

        // Other class methods.....
    }
Benefits of Singleton Design Pattern:

1. Instance Controlling: This prevents other objects from instantiating their own copies of the Singleton object, ensures that all objects access with the single instance.

2. Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.
3. Helpful in controls concurrent access to a shared resource like logging, registry settings, file system, windows manager etc.
4. Managing the multi-thread Pool.
5. It can be lazy loaded.
6. It has Static Initialization.
7. It helps to hide dependencies.
8. It provides a single point of access to a particular instance, so it is easy to maintain.

Example:

Consider logging process in an application. The log files are shared globally and bigger in size. Your intention is want to flush, sync and close it properly. This is an example of a single shared resource that has to be managed.

No comments: