• 分享一个单例模型类Singleton代码


    相关代码:

    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");
            }
        }

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

  • 相关阅读:
    分布式任务框架elastic-job 学习笔记
    spring 基础知识复习
    Spring MVC 搭建
    python引入自定义模块
    python操作Excel读写--使用xlrd
    selenium2.0关于python的常用函数
    从零开始学Python07作业源码:虚拟人生(仅供参考)
    通过System.getProperties()获取系统参数
    Android Studio启动后出现cannot bind to 127.0.0.1:5037 10048的解决办法
    开发环境入门 linux基础 (部分)awk 赋值变量 if
  • 原文地址:https://www.cnblogs.com/wwwzzg168/p/3565191.html
Copyright © 2020-2023  润新知