What is singleton Pattern?
Singleton pattern is the design pattern that restrictions the number of instances of the class to one any time.
That is any time only one instance of the class will be existed and this instance has global point of access. This pattern will be helpful when we need exactly one object is required not more or less.
How to implement?
If we want to implement singleton pattern we need to consider following:
- If we want to restrict object creating (instantiation) we should make the constructor of the class to private.
- If we want to create object of class having private constructor then we should have one Method in the class to create object and returns the object, let us call this method as Object Creator.
- We should able to call the Object Creator of the class with out object of the class (Why because we are calling this creator to create object so we don’t have any object at this moment). So that Object Creator should be public and static so that we can call the method with out object from the outside of the class
- We need one singleton class variable in the class to hold the singleton object created by the creator this should not be accessed from outside so it should be private, and it should accessed from Object Creator method which is static so this variable should be static.
In short if we want to implement singleton pattern we need a class with only private constructor(s), one static private variable to hold the singleton object and with public static method which create the object and returns to the caller.
Following is the sample code for the singleton design pattern:
Code
- class SingletonClass
- {
- /// <summary>
- /// This object stores the singleton object
- /// </summary>
- private static SingletonClass _singletonClassObject;
- //private static SingletonClass _singletonClassObject = new SingletonClass();
- private static object syncLock = new object();
- /// <summary>
- /// This constructor should be private so that we can restrict users to create the object of this class
- /// </summary>
- private SingletonClass()
- {
- //Constructor login
- }
- /// <summary>
- /// This is Object creator of the singleton class. We need to call this outside of the class with out object of this class so this should be public & static
- /// </summary>
- /// <returns></returns>
- public static SingletonClass GetSingletonClassObject()
- {
- lock (syncLock) // this is for synchronize lock to confirm no to code paths calls this method object creation method at a time.
{ - if (_singletonClassObject == null)
- {
- _singletonClassObject = new SingletonClass();
- }
- }
- return _singletonClassObject;
- }
- public void privateMethod()
- {
- //Private method logic here
- }
- }
- Better to use the lazy lazy initialization
What is lazy initialization?? above way of checking for the _singletonClassObject is null or not and creating object is called lazy initilization instead of creating object like this we can create object like the below also, this is called eager initialization- private static SingletonClass _singletonClassObject = new SingletonClass();
- public static SingletonClass GetSingletonClassObject() {return _singletonClassObject; }
Usually we will create singleton object for holding costly/heavily resources so better to create object when ever we required instead of creating object initial block(while initialization).
- Better to use synchronization while creating singleton object. Better to use above object creator instead of following
- public static SingletonClass GetSingletonClassObject()
- {
- if (_singletonClassObject == null) { _singletonClassObject = new SingletonClass(); } return _singletonClassObject;
- }
- Net work resources manager.
- File management system.
- Error longing in the application.
- Database access.
Hope this post will be helpful for understanding singleton pattern.

Access ViolationPosted Oct 16, 2014, 1:50 PM EDT
Code is not thread-safe, because check for null is out of lock scope. I would suggest using .Net's built in Lazy<T> class.
Arunava BhattacharjeePosted Oct 15, 2014, 7:33 PM EDT
Good...welcome to the community....try to include WHY and WHEN also....like "Why we will chose singleton pattern and when?"..it will be much helpful for the readers :)