• 分享一个单例模型类 Singleton


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

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

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

    namespace Pixysoft.DesignPattern
    {
        
    public class Singleton<T>
        {
            
    private Dictionary<string, T> dict = new Dictionary<string, T>();

            
    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
    <T>)i)._id = id;

                        T it 
    = (T)i;

                        dict.Add(id, it);

                        
    return it;
                    }
                }
            }

            
    public T this[int index]
            {
                
    get
                {
                    
    if (index < 0 || 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<Hello>
        {

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

    看看有什么错误等,希望能够指出。谢谢!

    reborn_zhang@hotmail.com
    美丽人生

  • 相关阅读:
    「小程序JAVA实战」java-sesion的状态会话与无状态会话(38)
    「小程序JAVA实战」小程序 loading 提示框与页面跳转(37)
    「小程序JAVA实战」小程序登录与后端联调(36)
    phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
    基于Bootstrap简单实用的tags标签插件
    html 中几次方,平方米,立方米,下标,上标,删除线等的表示方法
    如何做好App的引导页?(转)
    个推+DCLOUD,推送消息和透传消息
    AXURE在原型设计中的应用
    ***敏捷软件测试--初见
  • 原文地址:https://www.cnblogs.com/zc22/p/1583438.html
Copyright © 2020-2023  润新知