Holi Sale. Get upto 40% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Singleton Design Pattern

Singleton Design Pattern

19 Jan 2024
Intermediate
200K Views
8 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

Singleton Design Pattern - C#: An Overview

Singleton pattern falls under the 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 Design Pattern Tutorial, I would like to share what is Singleton pattern and how is it works.

What is the Singleton Design Pattern?

The singleton design pattern in C# is one of the most popular. In this pattern, a class has only one instance in the program, which serves as its global point of access. In other terms, a singleton is a class that only enables one instance of itself to be generated and typically provides easy access to that instance.

There are several approaches to implementing the singleton pattern in C#. The following are some common characteristics of a singleton pattern.

  • A single constructor that is private and has no parameters
  • The class has been sealed
  • A static variable that holds a reference to the single produced instance
  • A public and static method of obtaining the reference to the produced instance

Advantages of Singleton Design Pattern

  • The singleton pattern can be used to implement interfaces.
  • Can be lazy-loaded and has static initialization.
  • It helps to conceal dependence.
  • It gives a single point of access to a specific instance, making it simple to manage.

Disadvantages of Singleton Design Pattern

  • Unit testing is rather tough because it brings a global state into an application.
  • Locking reduces the opportunity for parallelism within a program.

Singleton class vs. Static methods

  • A static class cannot be extended, although a singleton class can.
  • A static class cannot be initialized, whereas a singleton class may.
  • When a program that has a static class is loaded, the CLR automatically loads it.

How to Implement Singleton Pattern in C# code

There are various approaches to implementing the Singleton Pattern in C#.

  • No thread-safe singleton.
  • Thread-Safety Singleton.
  • Thread-Safety Singleton with Double-Check Locking.
  • Thread-safe without a lock.
  • Using.NET 4's Lazy<T> type.

Singleton Pattern - UML Diagram & Implementation

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

Singleton Pattern - UML Diagram

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 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;
 }
 }
 }
}

Eager initialization instantiates the singleton instance upon class loading, ensuring its availability but potentially wasting resources even when unneeded. Lazy initialization delays instance creation until the first request, conserving resources but perhaps causing a little delay upon first access. Thread-safe initialization (double-checked locking) ensures secure access in multithreaded contexts by synchronizing instance creation, preventing multiple instances, and reducing performance overhead.

Singleton Pattern - Example

Singleton Pattern - Example

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();
 }
}

The code uses a thread-safe singleton pattern for the class "Singleton," which ensures that only one instance exists and is shared throughout the program. It implements double-checked locking to reduce performance overhead while ensuring thread safety during instance formation. The singleton object stores server information (name and IP) and has a method for displaying it.

Output

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
Summary

The Singleton design pattern in C# assures that a class has only one instance across an application. It provides a single point of access, streamlines management, and includes features such as lazy loading and interface implementation. However, it can present issues like as unit testing difficulty and reduced parallelism. Use it when a single instance with controlled access is required, but be aware of the potential downsides.

FAQs

Q1. Is MVC a singleton design pattern?

You were confused about architectural and design patterns. In this example, they differ. MVC is an architectural pattern, whereas Singleton is a design pattern.

Q2. How do I call a singleton in C#?

All you have to do is pass a delegate to the constructor that calls the Singleton constructor, which can be accomplished most easily via a lambda expression. It's simple and effective. If necessary, you may also use the IsValueCreated attribute to determine whether or not the instance has been created.

Q3. Is the singleton pattern good or bad?

The singleton pattern ensures that just one instance of a class exists at any one time. However, when utilizing singletons, many developers do not want to achieve this purpose. Singletons are similar to the wonderful things in life; they are not harmful if consumed in moderation.

Q4. Where is Singleton used?

The singleton design restricts class instantiation and ensures that only one instance exists in the Java Virtual Machine. The singleton class must provide a global access point for retrieving the class object. The singleton design is used for logging, driver objects, caching, and thread pooling.

Q5. Where is the singleton pattern applied in real life?

Practically, singletons can be utilized where external hardware resource use limits are required, such as with hardware printers, where the print spooler can be made a singleton to avoid numerous concurrent accesses and deadlock. Loggers employ singleton classes to generate log files.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this