The singleton pattern is a software design pattern that is used to restrict instantiation of a class to one object. This is useful when we require exactly one object of a class to perform our operations. In this pattern we ensure that the class has only one instance and we provide a global point of access to this object
public class Singleton
{
// Static object of the Singleton class.
private static volatile Singleton _instance = null;
///
/// The static method to provide global access to the singleton object.
///
/// Singleton object of class Singleton.
public static Singleton Instance()
{
if (_instance == null)
{
lock (typeof(Singleton))
{
_instance = new Singleton();
}
}
return _instance;
}
///
/// The constructor is defined private in nature to restrict access.
///
private Singleton() { }
}
No comments:
Post a Comment