分享一个单例模型类 Singleton

您所在的位置:网站首页 siganims_generic翻译 分享一个单例模型类 Singleton

分享一个单例模型类 Singleton

2023-02-27 21:15| 来源: 网络整理| 查看: 265

每个对象都写单例,单调又无聊。因此我写了个基类,只要集成,就实现了单例。

而且支持多单例(不同id对应不同的单例)

using  System; using  System.Collections.Generic; using  System.Text;

namespace  Pixysoft.DesignPattern{     public   class  Singleton     {         private  Dictionary  dict  =   new  Dictionary ();

        

private   string  _id  =   null ;

        

private   static   volatile   object  instance;

        

private   static   object  syncRoot  =   new  Object();

        

public   static  T Instance        {             get             {                 if  (instance  ==   null )                {                     lock  (syncRoot)                    {                        instance  =  Activator.CreateInstance( typeof (T));                    }                }

                

return  (T)instance;

            }        }

        

public  T  this [ string  id]        {             get             {                 // 如果是null,表示自己,则直接返回Instance                  if  ( string .IsNullOrEmpty(id))                     return  Instance;

                id 

=  id.Trim().ToUpper();

                

lock  (syncRoot)                {                     if  (dict.ContainsKey(id))                         return  dict[id];

                    

object  i  =  Activator.CreateInstance( typeof (T));

                    ((Singleton

)i)._id  =  id;

                    T it 

=  (T)i;

                    dict.Add(id, it);

                    

return  it;                }            }        }

        

public  T  this [ int  index]        {             get             {                 if  (index   dict.Keys.Count  -   1 )                     return  Instance;

                

int  coutner  =   0 ;

                

foreach  ( string  key  in  dict.Keys)                {                     if  (coutner  >=  index)                         return  dict[key];

                    coutner

++ ;                }

                

return  Instance;

            }        }

        

public   string  SingletonId        {             get  {  return  _id; }        }    }}

使用方法:

     class  testclass    {         private   void  test()        {            Hello.Instance.Test();

            Hello.Instance[

" id " ].Test();        }    }

    

public   class  Hello : Singleton     {

        

public   void  Test()        {            Console.Write( " hello " );        }    }

每个对象都写单例,单调又无聊。因此我写了个基类,只要集成,就实现了单例。

而且支持多单例(不同id对应不同的单例)

using  System; using  System.Collections.Generic; using  System.Text;

namespace  Pixysoft.DesignPattern{     public   class  Singleton     {         private  Dictionary  dict  =   new  Dictionary ();

        

private   string  _id  =   null ;

        

private   static   volatile   object  instance;

        

private   static   object  syncRoot  =   new  Object();

        

public   static  T Instance        {             get             {                 if  (instance  ==   null )                {                     lock  (syncRoot)                    {                        instance  =  Activator.CreateInstance( typeof (T));                    }                }

                

return  (T)instance;

            }        }

        

public  T  this [ string  id]        {             get             {                 // 如果是null,表示自己,则直接返回Instance                  if  ( string .IsNullOrEmpty(id))                     return  Instance;

                id 

=  id.Trim().ToUpper();

                

lock  (syncRoot)                {                     if  (dict.ContainsKey(id))                         return  dict[id];

                    

object  i  =  Activator.CreateInstance( typeof (T));

                    ((Singleton

)i)._id  =  id;

                    T it 

=  (T)i;

                    dict.Add(id, it);

                    

return  it;                }            }        }

        

public  T  this [ int  index]        {             get             {                 if  (index   dict.Keys.Count  -   1 )                     return  Instance;

                

int  coutner  =   0 ;

                

foreach  ( string  key  in  dict.Keys)                {                     if  (coutner  >=  index)                         return  dict[key];

                    coutner

++ ;                }

                

return  Instance;

            }        }

        

public   string  SingletonId        {             get  {  return  _id; }        }    }}

使用方法:

     class  testclass    {         private   void  test()        {            Hello.Instance.Test();

            Hello.Instance[

" id " ].Test();        }    }

    

public   class  Hello : Singleton     {

        

public   void  Test()        {            Console.Write( " hello " );        }    }

每个对象都写单例,单调又无聊。因此我写了个基类,只要集成,就实现了单例。

而且支持多单例(不同id对应不同的单例)

using  System; using  System.Collections.Generic; using  System.Text;

namespace  Pixysoft.DesignPattern{     public   class  Singleton     {         private  Dictionary  dict  =   new  Dictionary ();

        

private   string  _id  =   null ;

        

private   static   volatile   object  instance;

        

private   static   object  syncRoot  =   new  Object();

        

public   static  T Instance        {             get             {                 if  (instance  ==   null )                {                     lock  (syncRoot)                    {                        instance  =  Activator.CreateInstance( typeof (T));                    }                }

                

return  (T)instance;

            }        }

        

public  T  this [ string  id]        {             get             {                 // 如果是null,表示自己,则直接返回Instance                  if  ( string .IsNullOrEmpty(id))                     return  Instance;

                id 

=  id.Trim().ToUpper();

                

lock  (syncRoot)                {                     if  (dict.ContainsKey(id))                         return  dict[id];

                    

object  i  =  Activator.CreateInstance( typeof (T));

                    ((Singleton

)i)._id  =  id;

                    T it 

=  (T)i;

                    dict.Add(id, it);

                    

return  it;                }            }        }

        

public  T  this [ int  index]        {             get             {                 if  (index   dict.Keys.Count  -   1 )                     return  Instance;

                

int  coutner  =   0 ;

                

foreach  ( string  key  in  dict.Keys)                {                     if  (coutner  >=  index)                         return  dict[key];

                    coutner

++ ;                }

                

return  Instance;

            }        }

        

public   string  SingletonId        {             get  {  return  _id; }        }    }}

使用方法:

     class  testclass    {         private   void  test()        {            Hello.Instance.Test();

            Hello.Instance[

" id " ].Test();        }    }

    

public   class  Hello : Singleton     {

        

public   void  Test()        {            Console.Write( " hello " );        }    }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3