namespace Quadarax.Foundation.Core.Object
{
///
/// Well-known singleton pattern implementation.
///
///
/// The singleton type must inherit this class. The actual instance type is determined
/// by the only type parameter. The singleton type must implement a default constructor.
///
///
public class Singleton where TInstance : new()
{
private static TInstance? _instance;
private static readonly object _syncRoot = new object();
///
/// Singleton instance
///
public static TInstance Instance
{
get
{
if (_instance == null)
{
lock (_syncRoot)
{
if (_instance == null)
_instance = new TInstance();
}
}
return _instance;
}
}
}
///
/// Well-known singleton pattern implementation.
///
///
/// The singleton type must inherit this class. The actual instance type is determined
/// by the only type parameter. The singleton type must implement a default constructor.
///
///
///
public class Singleton
where TInstance : TInterface, new()
where TInterface : class
{
private static volatile TInterface? _instance;
private static readonly object _syncRoot = new object();
public static TInterface Instance
{
get
{
if (_instance == null)
{
lock (_syncRoot)
{
if (_instance == null)
_instance = new TInstance();
}
}
return _instance;
}
}
}
}