Singleton模式的C++实现研究(示例代码)

C++单例模式的四种实现及测试

[附件一:演示程序代码清单]<?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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值