Hi folks, In this tutorial, we are going to cover a very interesting topic Singleton Design Pattern. This is the most simple design pattern which comes the under Creational Pattern of Gang Of Four (GOF) design pattern in .net. A Single Design Pattern ensures that at any given point of time only one instance of an object is created.

What is Singleton Design Pattern?

A Singleton Design Pattern is the most simple design pattern which ensure that a class has only one instance of it and provide a global point of access to it.

When and Where to use Singleton Design Pattern?

  • When you want to create only one instance of a class.
  • You can use this pattern when you want to share common data accross application.
  • To preserve the global state of a type.
  • When you want to cache the object in-memory and resue it through the application.

Scenario where Single Design Pattern can be used:

  • We can use a Singleton Design Pattern while making a Database Connection for better performance.
  • I/O operations are a heavy task. By using the single instance of the logger class we can write the important information to the file as logs.

Example of Singleton Design Pattern:

1- Eager Initialization:

public class SingletonDemo


{
private static SingletonDemo instance = new SingletonDemo();
private SingletonDemo() { }
 
public static SingletonDemo GetInstance
{
get
{
return instance;
}
}
}

2- Lazy Initialization:

public class SingletonDemo
{
private static SingletonDemo instance = null;
private SingletonDemo() { }
 
public static SingletonDemo GetInstance
{
get
{
if (instance == null)
instance = new SingletonDemo ();
 
return instance;
}
}
}


3- Thread-safe (Double-checked Locking) initialization:

public class Singleton
{
private static Singleton instance = null;
private Singleton() { }
private static object lockThis = new object();
 
public static Singleton GetInstance
{
get
{
lock (lockThis)
{
if (instance == null)
instance = new Singleton();
 
return instance;
}
}
}
}

Complete Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;



public class Calculator
{
private Calculator()
{
Console.WriteLine("Single Instance");
}
private static object syncLock = new object();
private static Calculator instance = null;
public static Calculator Instance
{
get
{
lock (syncLock)
{
if (instance == null)
{
instance = new Calculator();
}
}
return instance;
}
}
public double Num1 { get; set; }
public double Num2 { get; set; }
public double Addition()
{
return Num1 + Num2;
}
public double Subtraction()
{
return Num1 - Num2;
}
public double Multiplication()
{
return Num1 * Num2;
}
public double Division()
{
return Num1 / Num2;
}
} 
class Program
{
 
 
static void Main(string[] args)
{

Calculator.Instance.Num1 = 20;
Calculator.Instance.Num2 = 5;
Console.WriteLine("Addition : " + Calculator.Instance.Addition());
Console.WriteLine("Subtraction : " + Calculator.Instance.Subtraction());
Console.WriteLine("Multiplication : " + Calculator.Instance.Multiplication());
Console.WriteLine("Division : " + Calculator.Instance.Division());
Console.WriteLine("\n----------------------\n");
Calculator.Instance.Num2 =20;
Console.WriteLine("Addition : " + Calculator.Instance.Addition());
Console.WriteLine("Subtraction : " + Calculator.Instance.Subtraction());
Console.WriteLine("Multiplication : " + Calculator.Instance.Multiplication());
Console.WriteLine("Division : " + Calculator.Instance.Division()); 
Console.ReadLine();
 
}
 
}

Output:

Singleton Design Pattern

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback, Suggestions are always welcome .

Thank You.

Leave a Comment