单例模式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DanLiMoShi
{
// 中国的历史上一般都是一个朝代一个皇帝,有两个皇帝的话,必然要PK出一个皇帝出来
class Minister
{
static void Main(string[] args)
{
//第一天
Emperor emperor = Emperor.getInstance();
Emperor.EmperorInfo();
//第二天
Emperor emperor1 = Emperor.getInstance();
Emperor.EmperorInfo();
//第三天
Emperor emperor2 = Emperor.getInstance();
Emperor.EmperorInfo();
Console.ReadKey();
}
}
public class Emperor
{
private static readonly Emperor staticEmperor = new Emperor();
//定义一个皇帝放在那里,然后给这个皇帝名字

private Emperor()
{
//世俗和道德约束你,目的就是不让你产生第二个皇帝
}
public static Emperor getInstance()
{

//参考写法
//if(staticEmperor==null)
//{
// lock(_lock)
// {
// if(staticEmperor==null)
// {
// staticEmperor=new Emperor();
// }
// }
//}
//return staticEmperor;

return staticEmperor;
}
public static void EmperorInfo()
{
Console.WriteLine("我是千古的一帝");
}

}

//大臣是天天要面见皇帝,今天见的皇帝和昨天的,前天不一样那就出问题了!

}

原文地址:https://www.cnblogs.com/lifesteven/p/3578908.html