Introduction
In this post i will explain about what is Singleton in c# and why we need to use this .
Key Points About Singleton Class
Singleton Class
As its name suggest Singleton is used for only a single object creation in OOP. This is very powerful Technic for our programming .
In many situations we came across where we need to create a single object of our class and use this object in whole application every-time Just like in login system and also when interacting with database every time.
Example:
Create object of TestSngleton ............... :-
Explanation:
In the above example first i create a class name TestSingleton and after this define a private constructor for this class, this private constructor restrict user from object creation.
After this we need to declare a static variable for object counts and a static method for object creation of this TestSingleton.
After first object creation static class type variable incremented by 1 and user never create any other object of this class until this object not destroyed or released
But this is not thread safe . If at the same time more than 1 user hit the same object creation call than it is possible that they all are able to create new instance of this class. Because in our static method no restriction for single user at a time.
Thread Safe Singletone Example
This lockCurrentObject take care of object creation from multiple users or threads. Only one thread will create the instance. After the execution of one thread other thread enters under lock and condition will return false because already object created by 1st thread so it will return false and no new object creation will happen.
In many situations we came across where we need to create a single object of our class and use this object in whole application every-time Just like in login system and also when interacting with database every time.
Example:
class TestSingleton
{
private TestSingleton()
{
}
public static TestSingleton objSingleton = null;
public static TestSingleton NewSingletonObject()
{
if(objSingleton == null)
{
objSingleton = new TestSingleton();
}
return objSingleton;
}
}
|
Create object of TestSngleton ............... :-
class Program
{
static void Main(string[] args)
{
TestSingleton.NewSingletonObject();
}
}
|
Explanation:
In the above example first i create a class name TestSingleton and after this define a private constructor for this class, this private constructor restrict user from object creation.
After this we need to declare a static variable for object counts and a static method for object creation of this TestSingleton.
After first object creation static class type variable incremented by 1 and user never create any other object of this class until this object not destroyed or released
But this is not thread safe . If at the same time more than 1 user hit the same object creation call than it is possible that they all are able to create new instance of this class. Because in our static method no restriction for single user at a time.
Thread Safe Singletone Example
class TestSingleton
{
private TestSingleton()
{
}
public static TestSingleton objSingleton = null;
private static readonly object lockCurrentObject = new object();
public static TestSingleton NewSingletonObject()
{
lock (lockCurrentObject)
{
if (objSingleton == null)
{
objSingleton = new TestSingleton();
}
return objSingleton;
}
}
}
|
This lockCurrentObject take care of object creation from multiple users or threads. Only one thread will create the instance. After the execution of one thread other thread enters under lock and condition will return false because already object created by 1st thread so it will return false and no new object creation will happen.
Key Points About Singleton Class
- You did not specify parameters when you create object of Singleton class.
- Singleton class has a private constructor.
- Singleton class use static method for object creation.
- Singleton can implements in different way (None thread Safe, Thread Safe, Double check Locking, Lazy instantiation )
0 comments:
Post a Comment