A singleton is a design pattern used when only one instance of an object is created and shared; that is, it only allows one instance of itself to be created. Any attempt to create another instance simply returns a reference to the first one. Singleton classes are created by defining all class constructors as private. In addition, a private static member is created as the same type of the class, along with a public static member that returns an instance of the class. Here is a basic example:
public class SingletonExample {
private static SingletonExample _Instance;
private SingletonExample () { }
public static SingletonExample GetInstance() {
if (_Instance == null) {
_Instance = new SingletonExample ();
}
return _Instance;
}
}
No comments:
Post a Comment