I am here to continue the learning series regarding Design Patterns. Today we will go through one of the popular design pattern called Singleton.
In case you have not had a look at our first article, go through the following link:
Before talking about its implementation let’s begin with some fundamental questions as in the following.
Use of the Singleton Pattern
As the name suggests, the Singleton Pattern allows only one instance of a class to be created.
When do we need to have only one instance of a class?
There are many possible requirements for a instance of a class but they all tend to have the one objective that we don’t want to change the state of the object or we want to keep the class stateless.
A simple example could be that you want to load some master data at once and let the consumers of the data make a call to the Singleton class instead of each consumer making various calls by creating a new instance.
In general, in any complex enterprise application, Repository and Data Access Layer classes can be seen as a Singleton since typically we don’t want them to maintain the state in these layers.
Some other example could be cross-cutting concerns like Logging, Configuration, Caching and so forth that can also be implemented as a Singleton since we want a single and global point of access to these classes.
Apart from the core consideration explained above, I have seen that developers, mostly not so experienced sometimes, create unnecessarily instances that creates not just an overhead to memory but also impacts the overall performance of an application.
Why not Static classes?
There can be several reasons why to not use a static class however I can think of a few as follows.
- There can be cases where you want to implement interfaces (maybe to implement IOC, I will explain IOC later) in a class that can be done in a Singleton implementation but not in the static one.
- If required, you can use a singleton class as a method parameter whereas you cannot do that with a static class.
Special care for Singleton classes
We need to take special care for Singleton classes. The idea of a state of a class comes with some extra care that means we need to handle synchronization issues in multi-threaded environments.
Enough theory, now let’s talk about implementation.
Let’s have a look at the most basic implementation.
In the first example below, we have implemented a Singleton with Lazy loading since the instance will not be created until the caller calls the GetInstance method for the first time.
- public class SingletonClass
- {
- private static SingletonClass instance = null;
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- if (instance == null)
- {
- instance = new SingletonClass();
- }
- return instance;
- }
- }
- }
Let’s try to fix the sync issue that may arise in multi-threaded environments. For this, we will use a double-lock mechanism.
- public class SingletonClass
- {
- private static SingletonClass instance = null;
- private static object lockMe = new object();
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- if (instance == null)
- {
- lock(lockMe)
- {
- if (instance == null)
- {
- instance = new SingletonClass();
- }
- }
- }
- return instance;
- }
- }
- }
And in the last, Singleton with static initializations. Please note that the .NET Framework guarantees thread safety for static initialization so we don’t need extra care for sync issues however we may not get the benefit of lazy loading of objects here.
- public class SingletonClass
- {
- private static SingletonClass instance = new SingletonClass();
- private SingletonClass() {}
- public static SingletonClass GetInstance
- {
- get
- {
- return instance;
- }
- }
- }
Conclusion:


Ganapati PanapanaPosted Sep 14, 2017, 10:53 AM EDT
Nice one !! Thanks Lot!
Prakash TripathiTOP 100Posted Nov 6, 2016, 7:21 AM EST
Not a problem Dario Benevento. Keep reading and commenting.
Dario BeneventoPosted Oct 31, 2016, 5:14 AM EDT
Thanx a lot!
Prakash TripathiTOP 100Posted Sep 5, 2016, 7:23 AM EDT
Thnx Prasanna for reading.
Prasanna MuraliPosted Sep 5, 2016, 7:13 AM EDT
Nice post......
Prakash TripathiTOP 100Posted Jul 18, 2016, 11:55 PM EDT
@Dario, the issue with source code file is resolved now. You may download the same (Demo.zip)
Prakash TripathiTOP 100Posted Jun 18, 2016, 12:31 AM EDT
Dario Benevento....I will reach out to the web site support team as well to fix the broken link.
Prakash TripathiTOP 100Posted Jun 17, 2016, 8:34 PM EDT
@Dario...looks like there is some issue. However the good part is that majority of code is added in the article itself so you may simply copy paste to code file and make a call to instance method from Main
Dario BeneventoPosted Jun 17, 2016, 3:59 AM EDT
Hello! The zip file attached is not available. Thanx
Maruthi PalllamalliPosted Jun 16, 2016, 9:57 PM EDT
if (instance == null) why should i check this for every time / call ? For more information can you please check this http://www.c-sharpcorner.com/blogs/property-based-singleton-design-pattern-in-c-sharp1
Prakash TripathiTOP 100Posted Apr 16, 2016, 10:15 PM EDT
Ok. @Aghazadeh. Even we can modify this one to accept parameter and create that number of object but that will not be singleton I believe.
Mohammad AghazadehPosted Apr 16, 2016, 11:16 AM EDT
Hi everone, another type of singelton pattern exists with multi singelton pattern that use for create specific number of singelton object.
Mohammad AghazadehPosted Apr 16, 2016, 11:14 AM EDT
Hi anybody,
Prakash TripathiTOP 100Posted Apr 10, 2016, 11:41 PM EDT
Thnx Humayun.
Humayun Kabir MamunPosted Apr 10, 2016, 11:37 PM EDT
Nice...
Prakash TripathiTOP 100Posted Apr 10, 2016, 10:54 PM EDT
Thnx Ghyath.
Ghyath SerhalPosted Apr 10, 2016, 9:49 PM EDT
Nice Article
Prakash TripathiTOP 100Posted Apr 9, 2016, 6:39 AM EDT
Thnx Nagaraj.
Kuppurasu NagarajTOP 100Posted Apr 9, 2016, 6:34 AM EDT
Nice article..
Prakash TripathiTOP 100Posted Mar 11, 2016, 12:06 AM EST
Thnx Amit.
Amit Kumar SinghPosted Mar 10, 2016, 11:49 PM EST
Nice one
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:49 PM EST
Thnx Kumaresh.
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:48 PM EST
Thnx Karthiga.
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:48 PM EST
Thnx Ankur.
Prakash TripathiTOP 100Posted Mar 6, 2016, 5:48 PM EST
Thnx Humayun.
Kumaresh RajalingamPosted Mar 6, 2016, 3:28 PM EST
good one
Kumaresh RajalingamPosted Mar 6, 2016, 3:27 PM EST
Nice explanation
Sr KarthigaPosted Mar 6, 2016, 3:16 PM EST
good one
Sr KarthigaPosted Mar 6, 2016, 3:16 PM EST
Nice explanation
Prakash TripathiTOP 100Posted Dec 30, 2015, 7:51 AM EST
Thnx Rupesh.
Rupesh KahanePosted Dec 30, 2015, 7:40 AM EST
very informative for beginners..:)
Ankur MistryTOP 100Posted Dec 30, 2015, 4:00 AM EST
Nice Share
Humayun Kabir MamunPosted Dec 13, 2015, 8:26 PM EST
Nice...
Prakash TripathiTOP 100Posted Jul 14, 2015, 12:40 AM EDT
Thnx Santhakumar for liking this.
Santhakumar MunuswamyPosted Jul 13, 2015, 11:08 AM EDT
Nice article! Thanks for sharing