1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
/// <summary>
/// Classe de base pour un singleton avec constructeur par défaut.
private ConcreteSingleton() { }
/// </summary>
/// <typeparam name="T">Type du singleton hérité</typeparam>
public abstract class Singleton<T>
where T : Singleton<T>
{
protected static readonly object mylock = new object();
protected static T instance;
protected Singleton() { }
public static T GetInstance()
{
lock ((mylock))
{
if (instance == null)
instance = (T)Activator.CreateInstance(typeof(T), true);
return instance;
}
}
}
public class ConcreteSingleton : Singleton<ConcreteSingleton>
{
private ConcreteSingleton() { }
} |