单例模式

 class Person
    {
        public string Name { get; set; }
        //1. 私有化构造函数
        //2. 提供1个私有的静态的Person类型的变量
        //3. 提供1个公共的静态的方法 用于返回上面的变量. 
        private static Person p;

        public static Person GetSingle()
        {
            if (p == null)
            {
                p = new Person();
            }
            return p;
        } 
        private Person()
        {

        } 
    }

  

双重锁机制,保证线程安全
namespace Singleton
{
    public class Singleton
    {
        //定义一个私有的静态全局变量来保存该类的唯一实例
        private static Singleton singleton;
        //定义一个只读静态对象
        //且这个对象是在程序运行时创建的
        private static readonly object syncObject = new object();
        ///
        /// 构造函数必须是私有的
        /// 这样在外部便无法使用 new 来创建该类的实例
        ///
        private Singleton()
        { }
        ///
        /// 定义一个全局访问点
        /// 设置为静态方法
        /// 则在类的外部便无需实例化就可以调用该方法
        ///
        ///
        public static Singleton GetInstance()
        {
            //这里可以保证只实例化一次
            //即在第一次调用时实例化
            //以后调用便不会再实例化
            //第一重 singleton == null
            if (singleton == null)
            {
                lock (syncObject)
                {
                    //第二重 singleton == null
                    if (singleton == null)
                    {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }
}
原文地址:https://www.cnblogs.com/sumg/p/3754063.html