【设计模式】单例模式

单例模式

单例模式是设计模式中比较简单的一种设计模式。
有许多种写法:

第一种 不考虑线程安全

    /// <summary>
    /// First version - not thread-safe
    /// </summary>
    public sealed class Singleton
    {
        /// <summary>
        /// 静态
        /// </summary>
        static Singleton instance = null;
        /// <summary>
        /// 私有构造函数
        /// </summary>
        Singleton()
        { }
        /// <summary>
        /// public property
        /// </summary>
        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }

第二种 线程安全

    /// <summary>
    /// Second version - simple thread-safety
    /// </summary>
    public sealed class Singleton
    {
        static Singleton instance = null;
        /// <summary>
        /// 
        /// </summary>
        static readonly object padlock = new object();

        Singleton()
        { }

        public static Singleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }
        }

    }

第三种 我没看懂

    /// <summary>
    /// Third version - attempted thread-safety using double-check locking
    /// </summary>
    public sealed class Singleton
    {
        static Singleton instance = null;
        /// <summary>
        /// 
        /// </summary>
        static readonly object padlock = new object();

        Singleton()
        { }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new Singleton();
                        }
                        return instance;
                    }
                }

                return instance;
            }
        }

    }

第四种 比较偷懒的写法

    /// <summary>
    /// Fourth version - not quite as lazy, but thread-safe without using locks
    /// </summary>
    public sealed class Singleton
    {
        static readonly Singleton instance = new Singleton();
        /// <summary>
        /// static constructor
        /// </summary>
        static Singleton()
        { }
        public static Singleton Instance
        {
            get { return instance; }
        }
    }

第五种 比较偷懒的写法

    /// <summary>
    /// Fifth version - fully lazy instantiation
    /// </summary>
    public sealed class Singleton
    {
        Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly Singleton instance = new Singleton();
        }
    }
参考文章

Depth in C About Singleton

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值