What is singleton software design patterns ?

 


Introduction of Singleton Design Pattern

        What is Singleton Design Pattern?

        Rules or Check list of Singleton Design Pattern?

        Where do we use Singleton Design pattern?

        Demo Code Example:

        Normal Singleton Design Pattern

        Without Thread Safe Singleton Design Pattern

        Thread Safe Singleton Design Pattern

 

What is Singleton Design Pattern?

  1. Singleton pattern is one of the simplest or lazy design patterns, general for all languages. Example C#, C++, Java and all OOPs supporting languages. This is type of creational design pattern.
  2. This pattern involves a single class which is responsible to create an object while making sure that only single object gets created.
  3. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.

 

Rules or Check list of Singleton Design Pattern

Before starting singleton design patterns, we have to follow below roles or check list

  1.  Create Singleton class in C# or C++ OR Java and etc.
  2.  Create a private static variable in the " Singleton" class.
  1.  Create a public static method or function in the Singleton class.
  1.  Create constructors to be private.
  1.  Clients may only use the accessor (Public) function to manipulate the Singleton.

 

Where we can use Singleton pattern?

        Use the Singleton pattern in your project, when a class in your program should have just a single instance available to all clients or whole project or etc..;

        for example,

        A single database connection object shared by different parts of the program.

        A single log object shared by different parts of the program.

 

Demo Code Examples:


Normal Singleton Design Pattern
Without Thread Safe Singleton Design Pattern
Thread Safe Singleton Design Pattern






Please find below Normal Singleton design pattern code : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NormalSingletonDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            int value= Singleton.GetSingletonInstence().AddTwoValues();
            Console.Write("Normal Singleton Design Pattern response " + value);

            Console.ReadLine();
        }
    }
    //Check list point 1
    public class Singleton
    {
        //Check list pont 4
        private Singleton()
        { }

        //Check list pint 2
        private static Singleton instence;
        //Check list point 3
        public static Singleton GetSingletonInstence()
        {
            if (instence == null)
            {
                instence = new Singleton();
            }

            return instence;
        }

        public int AddTwoValues()
        {
            return 2 + 3;
        }

    }

Without thread safe Singleton Design Pattern :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WithoutThreadSafeSingletonDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(funct1);
            t1.Start();

            Thread t2 = new Thread(funct2);
            t2.Start();

            Thread t3 = new Thread(funct3);
            t3.Start();

            Console.WriteLine("All threads complete");
            Console.ReadLine();
        }
        static void funct1()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(1, 2);
            Console.Write("Without thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
        static void funct2()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(3, 4);
            Console.Write("Without thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
        static void funct3()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(5, 6);
            Console.Write("Without thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
    }

   
    //Check point 1
    public class Singleton
    {
        //Check point 4
        private Singleton()
        {
            Thread.Sleep(100);
        }
     
        public static int instencecount { get; set; } = 0;

        //Check point 2
        private static Singleton instence;
        //Check point 3
        public static Singleton GetSingletonInstence()
        {
            if (instence == null)
            {
                instencecount = instencecount + 1;
                Console.Write("instence count" + instencecount + "\r\n");
                instence = new Singleton();
            }
            return instence;
        }

        public int AddTwoValues(int a, int b)
        {
            return a + b;
        }

    }
}

With Thread safe Singleton Design Pattern : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WithThreadSafeNormalSingletonDesignPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(funct1);
            t1.Start();

            Thread t2 = new Thread(funct2);
            t2.Start();

            Thread t3 = new Thread(funct3);
            t3.Start();

            Console.WriteLine("All threads complete");
            Console.ReadLine();
        }
        static void funct1()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(1, 2);
            Console.Write("With thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
        static void funct2()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(3, 4);
            Console.Write("With Thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
        static void funct3()
        {
            int value = Singleton.GetSingletonInstence().AddTwoValues(5, 6);
            Console.Write("With Thread safe Singleton Design Pattern response " + value + "\n\r");
            Console.Write("instence count " + Singleton.instencecount + "\n\r");
        }
    }

    //Check list 1
    public class Singleton
    {
        //Check list 4
        private Singleton()
        {
            Thread.Sleep(500);
        }
        public static int instencecount { get; set; } = 0;

        //Check list 2
        private static Singleton instence;

        private static object objLock = new object();

        //Check list 3
        public static Singleton GetSingletonInstence()
        {
            if (instence == null)
            {
                lock (objLock)//Thread safe
                {
                    if (instence == null)
                    {
                        instencecount = instencecount + 1;
                        Console.Write("instence count" + instencecount + "\r\n");
                        instence = new Singleton();
                    }
                }
            }
            return instence;
        }

        public int AddTwoValues(int a, int b)
        {
            return a + b;
        }

    }
}

Comments

Popular posts from this blog

Software Programming Teacher - Introduction