DEV Community

Cover image for Singleton Design Pattern in C#
MOEEN AHMED
MOEEN AHMED

Posted on

Singleton Design Pattern in C#

What is Singleton design pattern

Singleton design pattern belongs to the creational design pattern, which ensure that only one instance of the class is created and provide a single point of access to the object to all clients in the entire application.

Singleton design Pattern UML Diagram

Singleton design Pattern UML Diagram

The UML diagram describes the singleton class which has one property name Singleton to hold the instance of this singleton class, a private constructor to restrict the initialization from outside the class and a public GetInstance method which is responsible for creating the instance of this singleton class and return to the client code if it is not already initiated.

Why make a class Singleton

To demonstrate why we need to make a class singleton let’s create a simple class with default parameter less constructor, one common method and a static int field to keep track of instance that are created.

Singleton.cs

The count field is set in the constructor which will store the count of instances of this singleton class is created. The PrintDetails method display’s the message when it is called.

Program.cs

Now let’s say we have two client classes (Student, Employee) that are going to invoke this print details method. Student class print the message for the student and employee class will print the message for employee.

Let run the application and see the output.

Image description

The print details method is working fine and the message is printed for both Student and employee. But the issue with the singleton class is that to achieve the common functionality which to print the details we have created two instances of the singleton class. Let say we have to print the details for two more clients then we end up creating more instance of the singleton class.

Implement Singleton design pattern / Make class singleton

The class name is singleton but we have not made any changes till now to achieve the singleton design pattern. The following changes we have to make to achieve singleton behavior in our class.

  • Make class sealed so that any sub class cannot inherit from it
  • Change the public constructor access specifier to private so that Singleton class is not be instantized from outside
  • Add a static private field or property in class to store the instance of the class
  • Add a public property or method to create the instance of the class

Singleton.cs

The default public constructor is changed to private to restrict the direct instantiation of singleton class from outside directly and exposes the public GetInstance property to the client code for instance management. It first checks if the instance is already created then return the instance of single class if not it create it.

Image description

The client code is now change instead of creating the default constructor directly the GetInstance property is invoked to get the instance.

Let run the application again and see the output is same. The difference is instead of creating two instance we make use of single instance to call the common function.

Image description

Thread Safe Singleton Class

With the above changes the singleton behavior is achieved in the Singleton class but it will only work in a single threaded environment. In a multi-threaded environment when multiple threads are trying to instantiate the Singleton class we will end up getting multiple instances.

Add a static read-only property and add a lock check on the code where instance of singleton class is created in the singleton class.

Image description

Image description

Also change client code to to invoke the print details method simultaneously.

Image description

Conclusion

By creating class as singleton can avoid the headache of creation the instance of large objects over and over against for multiple clients during the execution of the program will result in efficient memory management. It is also best suite the situation for example where you want to handle the concurrent access to the shared resources like when you have a SQL database and you want all your clients to use the same connection instead of creation one for each to achieve connection pooling.

Contact Me

Thanks for reading much gratitude. I can write and help you directly. You can connect with me at moeenahmed769@gmail.com on Gmail and https://www.linkedin.com/in/moeen-ahmed/ on LinkedIn.

Top comments (0)