Implementing The Singleton Design Pattern

Singleton Pattern

This pattern comes under the creational patterns which are widely used in programming constructs. This pattern provides the best way to create objects.

In simple words, the Singleton pattern provides the single instance of the class throughout the lifecycle of the execution. Singleton patterns consist of the single class which create the Instance of that class, a single instance used by the application throughout the lifecycle.

Explanation

  • In many programming constructs sometimes we need only one object of the class to be used by the Application throughout the lifecycle.
  • In Singleton pattern there is only one class which is responsible for the creation of the single instance
  • Only one private and parameter-less constructor, the private constructor,  ensures that no other classes call the constructor of that class to create the object. Another important thing about parameter-less constructor is that  no parameter is passing to that constructor because if we make it private there is no way to pass the parameter.
  • The Singleton class must be sealed/final so that class is not inherited by another class
  • A static and read-only variable which is able to hold the reference of the instance
  • Getter method in case of Java and Property in case of C# is a public static method or public property which is responsible for getting the instances.

Example and Implementation

  • As I mentioned earlier, a good and practical example for Singleton is logging, configuration settings, thread pool, and device driver objects.
  • There are many implementations of the Singleton design pattern.

UML Diagram

UML Diagram

Code

  • First of all, we create the C# console project and then add the class named as “Singleton.”
  • The Singleton class is the class which uses the Singleton design pattern by creating the private static field with return type of that class (Singleton in my case) and then a private constructor and then a static public property which is responsible to get the instance of that class --  a single object.

    code
  • This was the main program.cs class.

    code

First Implementation

  • First implementation is the default implementation of the Singleton pattern which is not  “Thread Safe,” which means in a multithreading environment if both threads are concurrently accessing the Singleton class then both threads get the information about the static field which is null, so the two instance are created accidently in a multithreading environment.

    code

Second Implementation

Second implementation is the threadsafe Implementation in which threads acquire the lock to the Singleton class and then they check the condition one-by-one; the first thread comes to it to check the condition and it finds the field is null and condition is true so they create the instance. Second thread comes then if condition is false it returns the previously created object. In Java we use synchronization and in C# we use lock and a Mutex algorithms to  acquire the lock. Here is the simple implementation of the threadsafe method using the C# lock mechanism,

code
Third Implementation

Third implementation is quite lazy and tries to get thread safety using the double locking mechanism. The one and only single phrase with respect to that implementation is “We don’t use that implementation because of the double locking mechanism which is too expensive,” like in java, synchronization is much too expensive from the performance perspective. So that implementation is not good enough.
code

Fourth Implementation

The fourth implementation is extremely simple using static field without loc, in which we simply declare the static field; plus, with initialization this happens with the help of the static constructor. As we know, the static constructor is called once in the lifecycle of the application so when the constructoris is called the field is initialized once right after the constructor calls,  so there is no chance that this method won't provide the thread safety after all. This implementation also provides the thread safety which is an extremely simple way to achieve this without even using any locking mechanism.

code

Conclusions

The singleton pattern is widely used in Software Architecture. Most of the time there is a need for Singleton when we write the system application which accesses the device drivers. There are four implementations of the Singleton Pattern. The abstract factory and factory builder prototype design patterns use Singleton for their inner implementation. Singleton provides thread safety access to a single object. There is no need to make the object of a Singleton class; only class name is responsible to get the fully initialized object.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now