Skip to main content

Singleton Design Pattern

 What is Singleton design pattern?

    Of the Gang of Four design patterns, Singleton is the Creational design pattern used while creation of the object of the class. The creational design pattern is a technique to ensure that there is strictly only one instance of a particular class in your program.

Why Singleton?

    Suppose that your classes are using external resources like a database server, or a web Api etc, it would be a real problem if multiple objects try to use those resources at the same time. In such cases, singleton design pattern can help in overcoming the drawback in the above scenarios.

Real world example with code

    Suppose your program need to log the changes made in some data into a log file. Generally, in absence of the pattern, multiple objects may try to access the same log file which could create conflict. With a singleton log class object, you don't need to worry about several different objects trying to access the same class.

    First, Lets consider the below code where Singleton pattern is not implemented.


In the above code, n number of Logger class objects could be created. Hence when multiple simultaneous requests come for accessing the SaveLog method, problems may occur if the log file for saving is accessed at the same time by multiple request.

Now lets use Singleton Design Pattern to create a Logger class that can be instantiated only once. Each update request from the Customer class will point to the single Logger object.

Please refer the below code where Singleton Logger class is used. 


Lets review the Logger class in the above code.

Firstly, we made the Logger class as 'Sealed'. This means this class cannot be inherited any more.

Secondly, we created static Logger variable _logger. Since this variable is static, only one instance of the variable exists for the Logger class. 

The GetLogger() method first checks if the _logger  variable is null. If it is null, that means no instance of the Logger class has been yet created. Hence it will create new instance, assign it to the _logger variable  and return the _logger variable .

Now, another check is 'lock' check.

What is a lock?

    In a multi-threading environment, multiple threads can try to access the same object at a same time. The lock statement acquires the mutual-exclusion lock for a given object, executes a statement block, and then releases the lock. While a lock is held, the thread that holds the lock can again acquire and release the lock. Any other thread is blocked from acquiring the lock and waits until the lock is released.

In the above code, _lockObject  is simply an object and we are locking the object by using the lock statement. Anything that is executed inside the lock will happen only for one calling object at a time. Hence we ensure that only single instance of Logger class is present throughout the program at a time.

Even if you have multiple objects, on multiple threads, calling the GetLogger method, one of them will be “first”. This first object will see that the _logger is null, then attempt to get a lock on the _lockObject variable. Only one object at a time can have a lock on an object. Any other objects that try to lock the _lockObject variable will be queued up, until the first object releases its lock.

Conclusion

Here, we implemented the Singleton Design pattern to log updates in a log file. This pattern can be utilized in no of different scenarios like single Database connectivity object etc. I will try to implement the singleton pattern for database connection in the future blog.


Comments

Popular posts from this blog

C# Types

C# Types Guys please find below the diagram of type system in C# Please comment if you need the explanation of the Type system in details with easy examples. Click to view larger size

Difference between .NET Framework & .NET Core

 Microsoft released .NET Framework 1.0 as its software framework for Windows platform. Since then it has been continuously updating it to meet the technological advancements. In June 2017, Microsoft redefined the core architecture of .NET Framework to simplify development, testing and deployment. The company released .NET core 1.0 along with ASP .NET Core 1.0 with Entity Framework. .NET Core does have some learning curve in it.     As of Oct 2020, the latest version of .NET Framework is 4.7. Microsoft is unlikely to release new versions of this framework in the future. The latest version of .NET Core is 3.0 Understanding the major differences between the 2 platforms: Open Source -               .NET Framework was released as a licensed and proprietary framework. But the company has released .NET Core as open source framework. Hence individuals can build apps on .NET Core without paying any license fees. Cross Platform -  ...

Code to live ! Live to code !

One more tech site to add up the pile ? Every time I wanted to learn something or moreover understand the actual concept and reasoning of code, I surfed up like 100 websites to really understand why and what of logic . I ended up in the web to search engine keywords like 'C# reflection for dummies',  'Dependency Injection explanation to child', 'Design pattern example for newbie' etc.           Learn to code perfectly is really not a child's play. You need self motivation and continuous learning to keep up your interest as well as umm...your Job . I always wondered if there was one site that really and in really really simplest way could solve my doubts. If for a concept i search 5 or more websites, in each of them i learn something which is did not understood previously. I really wanted to take notes from all of these sites and combine them to make a concept in truly readable and amateur format. This blog seemed to be perfect way to start. Why...