zoukankan      html  css  js  c++  java
  • 一个泛型的单例模式

     public static class SingleInstanceFactory
            {
                
    private static Hashtable ht = new Hashtable();
                
    private static object syncObject = new object();

                
    public static T GetSingleInstance<T>() where T : new()
                {
                    
    string key = typeof(T).ToString();

                    T t 
    = default(T);

                    
    lock (syncObject)
                    {

                        
    if (ht.ContainsKey(key))
                        {
                            t 
    = (T)ht[key];
                        }
                        
    else
                        {
                            
    lock (syncObject)
                            {
                                t 
    = Activator.CreateInstance<T>();
                                ht.Add(key, t);
                            }
                        }
                    }

                    
    return t;
                }
            }
  • 相关阅读:
    Nacos(六):多环境下如何“管理”及“隔离”配置和服务
    nginx 反向代理配置(二)
    nginx 反向代理配置(一)
    nginx的access_log与error_log
    MySQL 慢查询日志
    php-fpm 慢日志查询
    理解 OAuth2.0
    如何在 Apache 里修改 PHP 配置
    Go-常见的面试题(一)
    Go 嵌入类型
  • 原文地址:https://www.cnblogs.com/sskset/p/2097026.html
Copyright © 2011-2022 走看看