Principles and Architecture / Design patterns / Creational
Singleton
Original notes by SunDiaL · View English source
The problem: You need to ensure that an object has only one instance (without making the class static).
The Solution: Have the class hold a static reference to itself
In practice you shouldn't need to do this nowadays, cause the better option is to inject your class as singleton instead, but in case you need to, this is how you do it.
public class SingletonExample
{
private static SingletonExample instance;
public SingletonExample Instance
{
get
{
//only create one instance ever.
if(instance == null)
{
instance = new SingletonExample();
}
return instance;
}
}
//prevent access to the constructor from outside by
//explicitly adding a private one.
private SingletonExample() { }
}