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
- }
- }
Access ViolationPosted Oct 16, 2014, 5:50 PM
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, 11:33 PM
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 :)