设计模式观察-Singleton

本文详细解析了Singleton模式的核心概念,包括实例化的责任归属与唯一性保证方法。通过伪代码展示了不同实现策略,如线程安全、双重锁定、延迟初始化等,并讨论了实例化工作对调用方的统计与授权验证。

本篇先讨论单件 Singleton,单件的目标是保证一个类型只有一个实例,那么由谁来保证实例的唯一性呢?可能的方案有:

a)调用端保证。
调用端调用一个类时,他是不需要也不会去考虑这个类是否已经被实例化的。而且把这样的监管工作交给调用端是很不负责的做法。
b)类型内部保证。

类型内部如何保证?

将实例创建工作放到类型内部,这样类型就可以将实例创建工作监管起来。类型可以知道内部的实例有没有被创建,甚至可以知道创建实例的工作被执行了多少次。

所以个人认为理解单件需要分为两步:

1、 监管工作谁来做?实例的监管工作需要类型自己去做。

2、 监管工作如何做?类型如何保证实例唯一就是技术实现问题了,可以看到的版本有 线程安全的、双重锁定的、延迟初始化的等。

下面使用伪代码逐步分析实例化工作放到类型内部的做法。

调用我,实例我给你

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->classSingleton
{
SingletonInstance
=null;
// 实例化类型Singleton
SingletonGetInstance()
{
Instance
=newSingleton();
returnInstance;
}

}

你只管调用,我保证唯一

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->classSingleton
{
SingletonInstance
=null;
//实例化类型Singleton
SingletonGetInstance()
{
Instance
=newSingleton();
returnInstance;
}
//实例化类型Singleton,实例化时判断类型有没有被创建过,这样就保证了实例的唯一
SingletonGetInstance()
{
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}

}

你们都可以调用,我需要统计调用次数

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->classSingleton
{
SingletonInstance
=null;
publicintCount{get;set;}
//实例化类型Singleton
SingletonGetInstance()
{
Instance
=newSingleton();
returnInstance;
}
//实例化类型Singleton,实例化时判断类型有没有被创建过,这样就保证了实例的唯一
SingletonGetInstance()
{
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}
//实例化类型Singleton,并且加入一个计数器,这样能知道实例化工作被执行了多少次
SingletonGetInstance()
{
Count
++;
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}

}

想使用实例?请出示合法证件

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->classSingleton
{
SingletonInstance
=null;
publicintCount{get;set;}
//实例化类型Singleton
SingletonGetInstance()
{
Instance
=newSingleton();
returnInstance;
}
//实例化类型Singleton,实例化时判断类型有没有被创建过,这样就保证了实例的唯一
SingletonGetInstance()
{
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}
//实例化类型Singleton,并且加入一个计数器,这样能知道实例化工作被执行了多少次
SingletonGetInstance()
{
Count
++;
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}
//实例化类型Singleton,并且接收一个合法的授权,这样可以知道每个授权方的调用次数,使用频率
SingletonGetInstance(stringcaller)
{
//Check调用方合法性验证
if(Check(caller))
{
CallerCount(caller);
if(Instance==null)
{
Instance
=newSingleton();
}
returnInstance;
}
else
returnnull;
}
//记录调用方调用次数
publicvoidCallerCount(stringcaller)
{
//callerCount++
}
publicboolCheck(stringcaller)
{
returntrue;
}
}


欢迎一起讨论!

--------------------------补充-------------------------------

我把几种流行的 Singleton 写法发出来,省的大家再去查资料。

<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->publicclassMySingleton
{
MySingletoninstance
=null;
MySingleton(){}
//简单写法
publicMySingletonIstance
{
get
{
if(instance==null)
{
instance
=newMySingleton();
}
returninstance;
}
}
//线程安全
staticreadonlyobjectobj=newobject();
publicMySingletonSafeInstance
{
get
{
lock(obj)
{
if(instance==null)
instance
=newMySingleton();
returninstance;
}
}
}
//双重锁定节约开销
publicMySingletonLockInstance
{
get
{
if(instance==null)
{
lock(obj)
{
if(instance==null)
instance
=newMySingleton();
}
}
returninstance;
}
}
//静态初始化
staticMySingleton(){}
staticreadonlyMySingletonstaticinstance=newMySingleton();
publicstaticMySingletonStaticInstance
{
get
{
returnstaticinstance;
}
}
//延迟初始化
publicstaticMySingletonlazyInstance
{
get
{
returnLazy.staticinstance;
}
}
classLazy
{
internalstaticreadonlyMySingletonstaticinstance=newMySingleton();
staticLazy(){}
}
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值