分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

[附件一:演示程序代码清单]

/*//////////////////////////////////////////////////////////////////

作者:张友邦

时间: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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有