Singleton design pattern creates a class where single instance of class can be created throughout the application. Where ever object of that class is required same instance is used.
public sealed class Singleton
{
private static Singleton _instance = null;
private Singleton()
{
}
public static Singleton GetInstance()
{
lock (_instance)
{
if (_instance == null)
_instance = new Singleton();
}
return _instance;
}
}
Note:
1. Singleton class should be sealed class so that it should not be derived further.
2. Singleton class constructor should be private to avoid class instance creation.
3. Multiple threading is handle through locking.
4. Singleton design pattern uses static property or method to create single instance.