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
- {
-
-
-
- private static SingletonClass _singletonClassObject;
-
- private static object syncLock = new object();
-
-
-
-
- private SingletonClass()
- {
-
- }
-
-
-
-
-
- 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()
- {
-
- }
- }
What are the best practices? - 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; }
Why we need lazy initialization?
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;
- }
But this method should be avoid. Why? Singleton pattern means creating object of the class only once. If we didn’t use the synchronization mechanism then there might be chance to create more than one object if more than one code path calls the Object creator same time. so better to use lock for the object creation block. What is the Use? There are some situations which we can use only one instance of the Class will be required more than one instances may not be allowed like: - Net work resources manager.
- File management system.
- Error longing in the application.
- Database access.
For the situations like theses where we can create only instance of the class we can user singleton pattern.
Hope this post will be helpful for understanding singleton pattern.