[附件一:演示程序代码清单]
/*//////////////////////////////////////////////////////////////////
作者:张友邦
时间: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