闽公网安备 35020302035485号
提高系统的可维护性:设计模式遵循一定的设计原则,如开闭原则、里氏代换原则等,这些原则有助于降低系统各部分的耦合度,提高系统的可扩展性和可维护性。
/// <summary>
/// 饿汉式单例模式
/// </summary>
public class SingletonEager
{
private SingletonEager() { }
private static readonly SingletonEager _instance = new SingletonEager();
public static SingletonEager Instance
{
get { return _instance; }
}
public void DoSomething()
{
Console.WriteLine("饿汉式单例模式.");
}
}
懒汉式单例模式 /// <summary>
/// 懒汉式单例模式
/// </summary>
public class SingletonLazy
{
private SingletonLazy() { }
private static SingletonLazy? _instance;
private static readonly object _lockObj = new object();
public static SingletonLazy Instance
{
get
{
if (_instance == null)
{
lock (_lockObj)
{
if (_instance == null)
{
_instance = new SingletonLazy();
}
}
}
return _instance;
}
}
public void DoSomething()
{
Console.WriteLine("懒汉式单例模式.");
}
}
懒加载单例模式 /// <summary>
/// 堆代码 duidaima.com
/// 懒加载单例模式
/// </summary>
public sealed class SingletonByLazy
{
private static readonly Lazy<SingletonByLazy> _lazy = new Lazy<SingletonByLazy>(() => new SingletonByLazy());
public static SingletonByLazy Instance { get { return _lazy.Value; } }
private SingletonByLazy() { }
public void DoSomething()
{
Console.WriteLine("懒加载单例模式.");
}
}