Singleton Design Pattern - C#

22 Aug 2022
Intermediate
198K Views

Singleton pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is a pattern that is one of the simplest design patterns. This pattern ensures that a class has only one instance. In this article, I would like to share what is Singleton pattern and how is it works?

What is Singleton Pattern?

Singleton Design pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.

A singleton design pattern in C# is one of the widely used & most popular design patterns around the corner. By using this design pattern, a class will have only a single instance of the program that provides a global point of access to it seamlessly. In other words, we can say that the singleton pattern follows is a class that allows only a single instance of itself to be created and usually gives simple access to that instance.

Singleton Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Singleton design pattern is given below:

The classes and objects in the above UML class diagram are as follows:

  1. Singleton

    This is a class that is responsible for creating and maintaining its own unique instance.

C# - Implementation Code

//eager initialization of singleton
public class Singleton
{
 private static Singleton instance = new Singleton();
 private Singleton() { }

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

////lazy initialization of singleton
public class Singleton
{
 private static Singleton instance = null;
 private Singleton() { }

 public static Singleton GetInstance
 {
 get
 {
 if (instance == null)
 instance = new Singleton();

 return instance;
 }
 }
}

////Thread-safe (Double-checked Locking) initialization of singleton
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;
 }
 }
 }
}

Singleton Pattern - Example

Who is what?

The classes and objects in the above class diagram can be identified as follows:

  1. Singleton - Singleton class

C# - Sample Code

/// <summary>
/// The 'Singleton' class
/// </summary>
public class Singleton
{
 // .NET guarantees thread safety for static initialization
 private static Singleton instance = null;
 private string Name{get;set;}
 private string IP{get;set;}
 private Singleton()
 {
 //To DO: Remove below line
 Console.WriteLine("Singleton Intance");

 Name = "Server1";
 IP = "192.168.1.23";
 }
 // Lock synchronization object
 private static object syncLock = new object();

 public static Singleton Instance
 {
 get
 {
 // Support multithreaded applications through
 // 'Double checked locking' pattern which (once
 // the instance exists) avoids locking each
 // time the method is invoked
 lock (syncLock)
 {
 if (Singleton.instance == null)
 Singleton.instance = new Singleton();

 return Singleton.instance;
 }
 }
 }

 public void Show()
 {
 Console.WriteLine("Server Information is : Name={0} & IP={1}", IP, Name);
 }

}

/// <summary>
/// Singleton Pattern Demo
/// </summary>
/// 
class Program
{
 static void Main(string[] args)
 {
 Singleton.Instance.Show();
 Singleton.Instance.Show();

 Console.ReadKey();
 }
}

Singleton Pattern Demo - Output

When to use it?

  1. Exactly one instance of a class is required.

  2. Controlled access to a single object is necessary.

    When we required no thread-safe singleton design pattern

Read More Articles Related to Design Pattern
What do you think?

The crucial and the most important advantage of using the singleton design pattern in C# is that it takes complete care of the concurrent access to the shared resource across the application seamlessly. By means of sharing resources, it means that if we are going to share any of the resources with multiple clients simultaneously, then the concurrent access to that resource will be completely managed by the singleton design pattern in an effective way as it provides a way to single global point of access to a particular instance so that it will be pretty easy to maintain.

I hope you will enjoy the Singleton Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Learn to Crack Your Technical Interview

Accept cookies & close this