c++中设计模式----单例模式
来源:互联网 发布:驱动人生mac版 编辑:程序博客网 时间:2024/04/30 10:56
单例模式保证一个类只有一个实例,并提供全局访问接口
//Singleton.h
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
class CSingleton
{
public:
virtual ~CSingleton();
static CSingleton *GetInstance();//提供全局访问接口
protected:
private:
CSingleton();
static bool bInstanced;//判断是否实例化了。
};
#endif
//Singleton.cpp
#include"Singleton.h"
#include<iostream>
using namespace std;
CSingleton::CSingleton()
{
cout<<"CSingleton()"<<endl;
}
CSingleton::~CSingleton()
{
cout<<"~CSingleton()"<<endl;
}
bool CSingleton::bInstanced=false;
CSingleton * CSingleton::GetInstance()
{
if(!bInstanced)
{
bInstanced=true;
return new CSingleton();
}
return NULL;
}
//test.cpp
#include<iostream>
#include"Singleton.h"
using namespace std;
void main()
{
CSingleton *p1=CSingleton::GetInstance();
CSingleton *p2=CSingleton::GetInstance();
if(p1!=p2)
{
cout<<"single CSingleton object!,test ok!!"<<endl;
}
else
{
cout<<"multiple CSingleton object!,test failed!!"<<endl;;
}
delete p1;
delete p2;
}
- (C#)设计模式 之 单例模式
- 设计模式--单例模式 C++实现
- 设计模式之单例模式(C++)
- 设计模式(C++) -----单例模式
- 【设计模式C++】单例模式
- 单例设计模式(C#) 简单模式
- 单例设计模式(C#) 懒汉模式
- C#--设计模式之单例模式
- c++中设计模式----单例模式
- 23中设计模式单例模式
- 单例模式的设计(C++)
- Objective-C 设计模式之单例
- Objective-c中的单例设计模式
- 设计模式,objective-c单例
- Objective-c ;单例设计模式
- Objective-c 单例设计模式
- objective c单例设计模式
- Objective-c 单例设计模式
- 转】DB2导入导出使用"|"作分隔符的问题
- 2012.9.10~11
- 七夕情书之恋恋红尘
- 黑马程序员——TCP客户端,服务端互相发送信息
- Filter与Interceptor
- c++中设计模式----单例模式
- “想赢”和“敢输”
- Mac OS X 下安装Subclipse
- Java中十个常见的违规编码
- HashCode相关类容
- 异常积累:org.hibernate.hql.ast.QuerySyntaxError: User is not mapped. [from User where username=?]
- PHP strip_tags 怎么 保留多个HTML标签
- 【ios】UIScrollView 原理
- lucene学习笔记之Analyzer和Search