分享一个单例模型类 Singleton
2009-10-14 18:09
357 查看
每个对象都写单例,单调又无聊。因此我写了个基类,只要集成,就实现了单例。
而且支持多单例(不同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
美丽人生
而且支持多单例(不同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
美丽人生
相关文章推荐
- 分享一个单例模型类 Singleton
- 分享一个单例模型类 Singleton
- 分享一个简单的基础数据库模型
- 分享一个Unity3D点击模型跟随鼠标移动的小脚本(包括屏幕视图到三维空间的坐标转换)
- 分享一个自己写的基于TP的关系模型(三)
- 分享一个单例模型类Singleton代码
- 分享一个自己写的基于TP的关系模型(四)
- 分享一个自己写的基于TP的关系模型(2)
- 万字分享6年经验|成为一个值钱的活动运营,所需的能力模型框架
- 分享一个WebGL开发的网站-用JavaScript + WebGL开发3D模型
- 分享一个自己写的基于TP的关系模型
- 分享一个vueui axios-mock-adapter 中的用法
- 分享一个CSS3介绍网址
- 2016 杭州·云栖大会 PDF 下载的一个分享
- ADO.NET Entity Framework 如何:通过每个层次结构一个表继承以定义模型(实体框架)
- (转)分享一个低功耗项目小小心得
- initramfs 简介,一个新的 initial RAM disks 模型
- Camshift原理 camshift利用目标的颜色直方图模型将图像转换为颜色概率分布图,初始化一个搜索窗的大小和位置,并根据上一帧得到的结果自适应调整搜索窗口的位置和大小,从而定位出当前图像中目标的
- 分享一个自己写的JFinal的BaseController (2)
- caffe finetuning时从源模型到目的模型:将一个layer的参数赋予到多个layer