本文详细介绍实例编程 C++ c# 分别实现单件模式
C#
1)
public sealed class Singleton
{
static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
2) 线程安全
public sealed class Singleton
{
static Singleton instance = null;
static readonly object lockObj = new object();
private Singleton()
{
}
public static Singleton Instance
{
get
{
lock (lockObj)
{
if (instance == null)
{
instance = new Singleton();
}
}
return instance;
}
}
}
C++:
1)
class Singleton
{
public:
static Singleton * Instance()
{
if( 0== _instance)
{
_instance = new Singleton;
}
return _instance;
}
protected:
Singleton(){}
virtual ~Singleton(void){}
static Singleton* _instance;
};
2) 利用智能指针进行垃圾回收
class Singleton3) 线程安全
{
public:
~Singleton(){}
static Singleton* Instance()
{
if(!pInstance.get())
{
pInstance = std::auto_ptr(new Singleton());
}
return pInstance.get();
}
protected:
Singleton(){}
private:
static std::auto_ptr pInstance;
};
来源:博客网 责编:豆豆技术应用
- 身份证函数 查看身份证地区信息(2)
- 身份证函数 查看身份证地区信息(1)
- 编程实现Windows关机、重启、注销
- 编程实例 一个简单的智能感知效果
- VB.net2008与ACCESS创建登录程序
- 实例编程 C++ c# 分别实现单件模式
- C#调用Delphi写的动态链接库
- ASP.Net程式中调用FCKeditor接口
- 伏特加感染下载器修改时间下病毒
- 超级巡警与360安全卫士的三项比较