设计模式之单例模式
来源:互联网 发布:淘宝客独家是什么意思 编辑:程序博客网 时间:2024/05/19 16:33
// 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点;
class Singelton
{
public:
static Singelton* GetInstance()
{
if (m_Instance == NULL)
{
m_Instance = new Singelton();
}
return m_Instance;
}
static Singelton *m_Instance;
double m_dTest;
private:
class GC // 垃圾回收类
{
public:
GC()
{
if (m_Instance == NULL)
{
m_Instance = new Singelton();
m_Instance->m_dTest = 1;
}
}
~GC()
{
cout<<"GC destruction"<<endl;
// We can destory all the resouce here, eg:db connector, file handle and so on
if (m_Instance != NULL)
{
delete m_Instance;
m_Instance = NULL;
cout<<"Singleton destruction"<<endl;
system("pause");//不暂停程序会自动退出,看不清输出信息
}
}
};
static GC gc; //垃圾回收类的静态成员
};
// 客户端代码
#include "Singelton.h"
Singelton* Singelton::m_Instance = NULL; // 注意静态变量类外初始化
Singelton::GC Singelton::gc;
// 保证一个类只有一个实例,并且提供一个访问它的全局访问点;
int _tmain(int argc, _TCHAR* argv[])
{
Singelton* s1 = Singelton::GetInstance();
Singelton* s2 = Singelton::GetInstance();
s1->Test();
s1->m_dTest = 1;
if(s1 == s2)
cout<<"ok"<<endl;
else
cout<<"no"<<endl;
return 0;
}
- 设计模式之 单例设计模式
- 设计模式之 单例设计模式
- 设计模式之单例设计模式
- 设计模式之-----------单例设计模式
- 设计模式之:单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之单例设计模式
- 设计模式之-单例设计模式
- 设计模式之单例设计模式 标签: 设计模式
- 设计模式之单例
- 设计模式之单例
- 设计模式之 单例
- 51nod oj 1050 循环数组最大子段和 【DP】
- js中的preventDefault与stopPropagation详解
- PHP接口的概念跟接口语言与接口的应用场景
- skip-grant-tables:非常有用的mysql启动参数
- 价格区间选择案例 吸附功能
- 设计模式之单例模式
- python打印红色
- PHP类的自动加载
- active nodes不显示
- 循环
- lightoj 1058 - Parallelogram Counting (几何,平行四边形)
- React中实现一个全局的参数
- jQuery中return false,e.preventDefault(),e.stopPropagation()的区别
- TODO-MVP源码解析