zoukankan      html  css  js  c++  java
  • 单例模式通用类

    namespace System
    {
        /// <summary>
        /// 为指定的实例创建有线程安全的单例模式。实例必须有一个公开的,无参数的构造方法,并且能正确的被实例化。
        /// </summary>
        /// <typeparam name="T">作为单例的对象。</typeparam>
        public static class Singleton<T>
           where T : class
        {
            static volatile T _instance;
            static object _lock = new object();
    
            /// <summary>
    
            /// 为指定对象创建实例。
    
            /// </summary>
    
            public static T CreateInstance()
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = Activator.CreateInstance<T>();
                        }
                    }
                }
                return _instance;
            }
        }
    }
    

      

  • 相关阅读:
    类例程_java战斗程序
    "类"的讲稿
    象棋中“车”的攻击范围_C#
    面向对象_方法_例题
    入门例子
    MyBatis
    MyBatis
    MyBatis
    Spring
    Hibernate学习
  • 原文地址:https://www.cnblogs.com/XuPengLB/p/8022300.html
Copyright © 2011-2022 走看看