恶汉

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  阅读(142)  评论(0编辑  收藏  举报