码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式代码

时间:2015-05-01 22:33:38      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

单例模式1:

技术分享
    /// <summary>
    /// 单例
    /// </summary>
    public sealed class Singleton
    {
        /// <summary>
        ////// </summary>
        private static object padlock = new object();

        /// <summary>
        /// 单例字段
        /// </summary>
        private static Singleton instance = null;

        /// <summary>
        /// 私有构造函数
        /// </summary>
        private Singleton()
        {
        }

        /// <summary>
        /// 实例
        /// </summary>
        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (padlock)
                    {
                        if (instance == null)
                        {
                            instance = new Singleton();
                        }
                    }
                }

                return instance;
            }
        }
    }
View Code

 单例模式2:

技术分享
 /// <summary>
    /// 单例
    /// </summary>
    public sealed class Singleton
    {
        /// <summary>
        /// 实例
        /// </summary>
        private static Singleton instance = new Singleton();

        /// <summary>
        /// 私有构造函数
        /// </summary>
        private Singleton()
        {
        }

        /// <summary>
        /// 实例
        /// </summary>
        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    }
View Code

 

设计模式代码

标签:

原文地址:http://www.cnblogs.com/fengjin/p/4471125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!