[附件一:演示程序代码清单]<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
/*//////////////////////////////////////////////////////////////////
作者:张友邦
时间:2002年10月9日
描述:实现Singleton
/*//////////////////////////////////////////////////////////////////
#include <iostream.h>
#include <tchar.h>
////////////////////////////////////////////////////////////////////
//第一种实现(使用模板函数)
class MySingleton1
{
private:
MySingleton1(){ cout << _T("Construct MySingleton1") << endl; }
MySingleton1 & operator =(const MySingleton1&){}
template <typename T>
friend T& GetInstanceRef();
public:
~MySingleton1(){ cout << _T("Destroy MySingleton1") << endl; }
public:
void DoSomething(){ cout << _T("Do something here in MySingleton1") << endl; }
};
template <typename T>
T& GetInstanceRef()
{
static T _instance;
return _instance;
}
template <typename T>
T* GetInstancePtr()
{
return &GetInstanceRef<T>();
}
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//第二种实现(使用模板类)
template <typename T>
class SingletonWraper
{
public:
static T& GetInstanceRef()
{
static T _instance;
return _instance;
}
static const T& GetInstanceConst()
{
return GetInstanceRef();
}
static T* GetInstancePtr()
{
return &GetInstanceRef();
}
};
#define DEFINE_SINGLETON(ClassName); \
public: \
friend class SingletonWraper<ClassName>; \
typedef class SingletonWraper<ClassName> SingletonWraper; \
typedef SingletonWraper SingletonInterface; \
private: \
const ClassName& operator=(const ClassName&) \
{ \
return SingletonInterface::GetInstanceRef(); \
} \
ClassName(const ClassName&); \
private: \
static void operator delete(void *p, size_t n) \
{ \
throw -1; \
}//End of define DECLARE_SINGLETON(ClassName);
class MySingleton2
{
DEFINE_SINGLETON(MySingleton2);
private:
MySingleton2(){ cout << _T("Construct MySingleton2") << endl; }
public:
~MySingleton2(){ cout << _T("Destroy MySingleton2") << endl; }
public:
void DoSomething(){ cout << _T("Do something here in MySingleton2") << " " << endl; }
};
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//第三种实现(由类自身实现,自动销毁对象,相比之下,它更简单)
#define DECLARE_SINGLETON(ClassName); \
public: \
static ClassName& GetInstanceRef() \
{ \
static ClassName _instance; \
return _instance; \
} \
static const ClassName& GetInstanceConst() \
{ \
return GetInstanceRef(); \
} \
static ClassName* GetInstancePtr() \
{ \
return &GetInstanceRef(); \
} \
const ClassName& operator=(const ClassName&) \
{ \
return GetInstanceRef(); \
} \
private: \
ClassName(const ClassName&); \
static void operator delete(void *p, size_t n) \
{ \
throw -1; \
}//End of define DECLARE_SINGLETON(ClassName);
class MySingleton3
{
DECLARE_SINGLETON(MySingleton3);
private:
MySingleton3(){ cout << _T("Construct MySingleton3") << endl; ID = 0; }
public:
int ID;
~MySingleton3(){ cout << _T("Destroy MySingleton3") << endl; }
void DoSomething(){ cout << _T("Do something here in MySingleton3, ID = ") << ID << endl; }
};
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//第四种实现(《Design Patterns》里的,做了一些修改)
//(由类自身实现,手动与自动销毁对象)
#define ALLOW_SINGLETON(ClassName); \
private: \
static ClassName* _instance; \
\
public: \
static ClassName& GetInstanceRef() \
{ \
if (_instance == 0) \
_instance = new ClassName; \
return *_instance; \
} \
static ClassName* GetInstancePtr() \
{ \
return &GetInstanceRef(); \
} \
static ReleaseInstance() \
{ \
if (_instance != 0) \
{ \
delete _instance; \
_instance = 0; \
} \
} //End of ALLOW_SINGLETON(ClassName);
#define IMPLEMENT_SINGLETON(ClassName); \
ClassName* ClassName::_instance = 0; \
static class DestructHelper_##ClassName \
{ \
public: \
~DestructHelper_##ClassName(){ ClassName::ReleaseInstance(); } \
} DestructHelperInstance_##ClassName;
//End of IMPLEMENT_SINGLE(ClassName);
class MySingleton4
{
private:
MySingleton4(){ cout << _T("Construct MySingleton4") << endl; } //构造函数私有
~MySingleton4(){ cout << _T("Destroy MySingleton4") << endl; } //析构函数放哪里都可以
ALLOW_SINGLETON(MySingleton4);
public:
void DoSomething(){ cout << _T("Do something here in MySingleton4") << endl; }
};
IMPLEMENT_SINGLETON(MySingleton4);
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//测试
void _tmain(int argc, char *argv[])
{
//测试第一种实现
cout << _T("**************Test of the first implementation***************") << endl;
MySingleton1* myobj1;
myobj1 = GetInstancePtr<MySingleton1>();
myobj1->DoSomething();
GetInstanceRef<MySingleton1>().DoSomething();
//测试第二种实现
cout << endl << _T("**************Test of the second implementation**************") << endl;
MySingleton2* myobj2;
myobj2 = SingletonWraper<MySingleton2>::GetInstancePtr();
myobj2->DoSomething();
//MySingleton2 myobj22(*myobj2); //Error
MySingleton2::SingletonInterface::GetInstanceRef().DoSomething();
//测试第三种实现
cout << endl << _T("**************Test of the third implementation***************") << endl;
MySingleton3 *myobj3 = MySingleton3::GetInstancePtr();
myobj3->ID = 1;
myobj3->DoSomething();
MySingleton3& myobj33 = MySingleton3::GetInstanceRef();
myobj33 = *myobj3;
try
{
delete myobj3;
}
catch(...)
{
cout << _T("Your object cannot be deleted.") << endl;
}
myobj33.ID = 2;
myobj33.DoSomething();
myobj3->DoSomething();
//测试第四种实现
cout << endl << _T("**************Test of the fourth implementation**************") << endl;
MySingleton4 *myobj4 = MySingleton4::GetInstancePtr();
myobj4->DoSomething();
MySingleton4::GetInstanceRef().DoSomething();
cout << _T("**********************End of all testing*********************") << endl << endl;
cout << _T("Following is the Automatic Garbage Collection process:") << endl << endl;
}
////////////////////////////////////////////////////////////////////
[附件二:演示程序运行结果]
**************Test of the first implementation***************
Construct MySingleton1
Do something here in MySingleton1
Do something here in MySingleton1
**************Test of the second implementation**************
Construct MySingleton2
Do something here in MySingleton2
Do something here in MySingleton2
**************Test of the third implementation***************
Construct MySingleton3
Do something here in MySingleton3, ID = 1
Destroy MySingleton3
Your object cannot be deleted.
Do something here in MySingleton3, ID = 2
Do something here in MySingleton3, ID = 2
**************Test of the fourth implementation**************
Construct MySingleton4
Do something here in MySingleton4
Do something here in MySingleton4
**********************End of all testing*********************
Following is the Automatic Garbage Collection process:
Destroy MySingleton3
Destroy MySingleton2
Destroy MySingleton1
Destroy MySingleton4
分享到:
相关推荐
单例模式通过类本身来管理其唯一实例,这种特性提供了解决问题的方法。唯一的实例是类的一个普通对象,但设计这个类时,让它只能创建一个实例并提供对此实例的全局访问。唯一实例类Singleton在静态成员函数中隐藏...
很多开发人员在刚开始学Python 时,都考虑过像 c++ 那样来实现 singleton 模式,但后来会发现 c++ 是 c++,Python 是 Python,不能简单的进行模仿。 Python 中常见的方法是借助 global 变量,或者 class 变量来实现...
这是关于设计模式的c++代码,涵盖了abstractFactory, Builder, Adapter, Components,Singleton,Prototype,Bridge 还有未完的部分 将下次再传。
C++设计模式代码: (VC工程) Creational Patterns Abstract Factory Creates an instance of several families of classes Builder Separates object construction from its representation Factory Method ...
第十六章 SINGLETON模式和MONOSTATE模式 第十七章 NULL OBJECT模式 第十八章 薪水支付案例研究:第一次迭代开始 第十九章 薪水支付案例研究:实现 第Ⅳ部分 打包薪水支付系统 第二十章 包的设计原则 第二十一章 ...
第16章 SINGLETON模式和MONOSTATE模式 第17章 NULL OBJECT模式 第18章 薪水支付案例研究:第一次迭代开始 第19章 薪水支付案例研究:实现 第四部分 打包薪水支付系统 第20章 包的设计原则 第21章 FACTORY模式 ...
部代码研究时,这点尤其明显,因此,如果你不具备设计模式的基础知识(GoF 的设计模式),你很难快速的理解 J2EE。不能理解 J2EE,如何能灵活应用? 3.J2EE 只是适合企业计算应用的框架软件,但是 GoF 的设计模式几乎...
ASP.NET 2.0 正式版中无刷新页面的开发(示例代码的补充) ASP.NET2.0中themes、Skins轻松实现网站换肤! ASP.NET 2.0 中的代码隐藏和编译 ASP.NET 2.0 Language Swithcer and Theme Swicher 多语言转换和多样式主题...
附件中是java实现全部的设计模式,包含代码和工程(jbuilder工程),值得收藏. 此目录里包括了一书中所有23种设计模式的实现(Java 版)源码 关于代码的几点说明: 1. 代码为根据个人对Design Pattern的学习理解写...
2.5线程安全的Singleton 实现.. . . . . . . . . . . . . . . . . . . . . . . . . 48 2.6sleep(3) 不是同步原语. . . .. . . . . . . . . . . . . . . . . . . . . . . . 50 2.7归纳与总结. . . . . . . . . . . . ...