Singleton Design Pattern in C#

In this article, We will discuss :

  • What is Singleton design pattern ?
  • How to implement Singleton design pattern in C# ?
  • Effects of multithreading on singleton pattern
  • Implementing Double checked locking for singleton
  • Early instantiation of singleton
  • Fully Lazy instantiation of singleton
  • Implementing Singleton using Generics
  • Lazy instantiation of singleton
  • Ways of breaking singleton
  • Uses of Singleton pattern
  • Singleton as Anti-pattern
  • Difference between static class and SIngleton Pattern
  • Summary

Singleton Design Pattern:

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

Standard Singleton implementation

To ensure the class has only one instance, we mark the constructor as private. So, we can only instantiate this class from within the class. More On CSharp

We create a static variable that will hold the instance of the class.

Then, we create a static method that provides the instance of the singleton class. This method checks if an instance of the singleton class is available. It creates an instance, if its not available; Otherwise, it returns the available instance.

Issues with the standard implementation

The standard singleton implementation code works fine in Single threaded environment.

But in multithreaded environment, the GetInstance() code breaks :

If two threads enter the if condition at the same time, then two instances of Singleton will be created.

Handling Multithreading issue with standard singleton implementation

We can synchronize the method. So that only one thread can access it at a time.In the below implementation, the thread locks out the shared object, and then checks whether or not the instance has been created before creating the instance.

This solves the multithreading issue. But it is slow since only one thread can access GetInstance() method at a time.

We can use following approaches for this :

1 : Double checked locking

In this approach, we first check if the instance is created. If not, we synchronize.

2 : Early instance creation

We can choose to create the instance of Singleton class when the class is loaded.This is thread-safe without using locking.

3 : Fully Lazy Instantiation

Here, instantiation is triggered by the first reference to the static member of the GetInstance class, which only occurs in Instance.

4 : Singleton Implementation using Generics

This will give you a single instance and will effectively be lazy, because the static constructor doesn’t get called until Build() is called.

5 : using Lazy type

You need to pass a delegate to the constructor which calls the Singleton constructor – which is done most easily with a lambda expression.

Breaking Singleton questions you may get in interviews :

Even though we avoided multiple instance creation of singleton class by using Double checked locking or Eager instance creation, instances can still be created by :
– cloning
– reflection
– Sub-classing singleton class

How to avoid Singleton instance creation by cloning ?

We can create a copy of an object using clone() method.

To avoid creating a clone of our singleton class, we can do the following :

– Implement MethodwiseClone()

– Override the clone() method and throw CloneNotSupportedException from it.

How to avoid Singleton class instance creation through Reflection ?

To avoid instance creation through reflection, throw an exception from constructor if it already has an instance.

How to avoid a subclass creating singleton instance ?

If you just provide a private constructor, a subclass cannot instantiate it to create another instance.

Uses of Singleton Design Pattern :

Logger :

Singleton pattern is a good option for the Logger class when we want to create one log file with the logged messages. If we have more than one instances of Logger in the application, a new log will be created with every instance.

Cache :

We can use Singleton pattern to implement caches as that provides a single global access to it.

Why singleton pattern is considered an Anti-pattern ?

– Singletons aren’t easy to handle with unit tests. You can’t control their instantiation and they may retain state across invocations.
– Memory allocated to an Singleton can’t be freed.
– In multithreaded environment, access to the singleton object may have to be guarded (e.g. via synchronization).
– Singletons promote tight coupling between classes, so it is hard to test

Difference between Static class and Singleton Pattern:

– In c# a static class cannot implement an interface. When a single instance class needs to implement an interface for some business reason or IoC purposes, you can use the Singleton pattern without a static class.
– You can clone the object of Singleton but, you can not clone the static class object
– Singleton object stores in Heap but, static object stores in stack
– A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded

Summary:

In this article, we have discussed:

  • Different ways for Singleton Implementation in C#
  • Uses of Singleton Pattern
  • Why SIngleton Pattern is considered as Anti Pattern?
  • Difference between Static class and SIngleton Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s