What is Singleton Design Pattern

Singleton Design Pattern is a type of Creational type Pattern.

Singleton Design Pattern is used when we want to ensure that only one object of a particular class is instantiated. This single instance is used to coordinate actions across applications.

Concurrent access to resources is well managed by Singleton Design Pattern.

To implement the Singleton design pattern

  1. Declare all constructors of the class as private to ensure that one instance of a class exists.
  2. To Control Singleton Access, create a static property that will return a single instance of the object
    using System;
    namespace SingletonDesignPattern
    {
        public class Singleton
        {
            private static int counter = 0;
    
            /*Ensures that only instance of a class is created*/
            private static Singleton obj = null;
            public static Singleton GetInstance
            {
                get 
                {
                    if (obj == null)
                        obj = new Singleton();
                    return obj;
                }
            }
    
            private Singleton()
            {
                counter++;
                Console.WriteLine("Counter Values is: " + counter.ToString());
            }
    
            public void Print(string msg)
            {
                Console.WriteLine(msg);
            }
    
            public class DerivedSingleton : Singleton
            {
                public void OutPut(string msg)
                {
                    Console.WriteLine("Derived Class Output");
                }
            }
        }
    }
    
    namespace SingletonDesignPattern
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                Singleton frmEmployee = Singleton.GetInstance;
                frmEmployee.Print("From Employee");
                Singleton frmStudents = Singleton.GetInstance;
                frmStudents.Print("From Students");
            }
        }
    }
    

Why is a Singleton Class Sealed?

If we remove the Sealed Keyword, then we will be able to inherit a class inside the class. i.e. using Nested class.

A nested Class is a class within the class.

Consider the example where the class is not marked as Sealed.

using System;
namespace SingletonDesignPattern
{
    public class Singleton
    {
        private static int counter = 0;
        private static Singleton obj = null;

        public static Singleton GetInstance
        {
            get
            {
                if (obj == null)
                    obj = new Singleton();
                return obj;
            }
        }

        private Singleton()
        {
            counter++;
            Console.WriteLine("Counter Values is: " + counter.ToString());
        }

        public void Print(string msg)
        {
            Console.WriteLine(msg);
        }

        public class DerivedSingleton : Singleton
        {
            public void OutPut(string msg)
            {
                Console.WriteLine("Derived Class Output");
            }
        }
    }
}

DerivedSingleton is a nested class that inherits Singleton Class.

using System;
using static SingletonDesignPattern.Singleton;
namespace SingletonDesignPattern
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Singleton frmEmployee = Singleton.GetInstance;
            frmEmployee.Print("From Employee");
            Singleton frmStudents = Singleton.GetInstance;
            frmStudents.Print("From Students");

            DerivedSingleton fromDerived = new DerivedSingleton();
            Console.WriteLine("From Derived");
        }
    }
}

The counter value will be 2. Thus, multiple instances of Singleton Class are Created using nested Derived Class. Which violates the singleton pattern. So, a Singleton Class is marked as sealed to prevent the class from inheriting within the class also and outside the class.

A private constructor prevents external instantiations of objects, and a sealed class prevents the class from inheritance.