设计模式学习--Singleton泛型类

    /// <summary>
    /// Singleton泛型类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public sealed class Singleton<T> where T : new()
    {
        private static T instance = new T();

        private static object lockHelper = new object();

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

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <param name="value"></param>
        public static T GetInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new T();
                    }
                }
            }

            return instance;
        }      

    }

  

posted on 2014-12-25 11:49  荆小轲  阅读(103)  评论(0编辑  收藏  举报

导航