恶汉

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;
}
}

 

posted on 2018-03-27 21:47  Hai_yi  阅读(145)  评论(1编辑  收藏  举报