分享一个单例模型类Singleton代码
2014-02-24 19:12
267 查看
相关代码:
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");
}
}看看有什么错误等,希望能够指出。
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");
}
}看看有什么错误等,希望能够指出。
相关文章推荐
- 分享一个自定义的 console 类,让你不再纠结JS中的调试代码的兼容
- 分享过滤条件中增加一个自定义过滤变量插件代码
- 分享一个单例模型类 Singleton
- 一个很好用的在线编辑、展示、分享、交流JavaScript 代码的平台
- 8086汇编语言自学经验分享 针对CX只有一个,所以两层循环与栈结合的启发代码
- Shell脚本实现判断IP地址是否在一个ip段内代码分享
- 一个IP摄像头视频流生产发送模型的C代码实现
- 分享一个可下拉也可输入的控件(添加截图和示例代码)
- Java编程一个随机数产生模块代码分享
- 分享一个面向EntityFramework的代码生成工具(源码)
- 分享一个很实用的java代码,可以将以逗号分隔的数字以从大到小的方式排序后返回
- 分享一个有关代码生成的网站
- 分享基于分布式Http长连接框架--代码模型
- 分享一个查看android各个版本代码的网站
- 分享一个可以生成各种进制格式IP的小工具实例代码
- Python实现的一个找零钱的小程序代码分享
- 一个典型的PHP分页实例代码分享
- 一个JQuery操作Table的代码分享
- 分享一个好玩的python爬虫代码