单利模式(恶汉和装逼的懒汉)

恶汉

class God
{
private static God instance = new God();
private God()
{
}
public static God GetInstance()
{
return instance;
}
}

懒汉

class God
{
private static God instance = null;
private static object locker = new object();
private God()
{
}
public static God GetInstance()
{
if (instance == null)
{
lock (locker)
{
if (instance == null)
{
instance = new God();
}

}
}
return instance;
}
}
原文地址:https://www.cnblogs.com/Tom-yi/p/8660457.html