创建型模式:单件模式
2015-09-13 22:59
260 查看
单件模式是为了保证类只有一个实体,并为它提供一个全局唯一的访问点。
class singleton
{
public:
~singleton(){}
static singleton* getInstance()
{
if(_instance == NULL)
{
_instance = new singleton();
}
return _instance;
private:
static singleton *_instance;
private:
singleton(){ }
singleton(const singleton&){ }
singleton& operator=(const singleton &){}
};
singleton *singleton::_instance = NULL;
说明:
使用时只需调用singleton::getInstance()既可以获取到singleton的指针,但是使用完后要自己释放内存,解决的办法有:
(1)手动调用delete singleton::getInstance()
(2)注册atexit()函数以释放内存
(3)使用智能指针
(4)利用c++内嵌类和一个静态成员实现自动释放机制
在多线程环境下使用,如果大量线程调用到new时,可能会造成内存泄露,并且有可能前后获取的singleton对象不一致,解决的办法是用锁机制。
#include <memory>
#include <iostream>
#include <windows.h>
using namespace std;
class lockguard
{
private:
CRITICAL_SECTION m_cs;
public:
lockguard() { InitializeCriticalSection(&m_cs); }
~lockguard() { DeleteCriticalSection(&m_cs); }
class cguard
{
public:
cguard(lockguard &lg) : m_lg(lg) { m_lg.guard(); }
~cguard() { m_lg.unguard(); }
private:
lockguard &m_lg;
};
private:
void guard() { EnterCriticalSection(&m_cs); }
void unguard() { LeaveCriticalSection(&m_cs); }
friend class lockguard::cguard;
};
class singleton
{
public:
~singleton() {}
static singleton* getInstance()
{
if(_instance == NULL)
{
lockguard::cguard gd(_lg);
if(NULL == _instance)
{
static clearer clr;
_instance = new singleton();
}
}
return _instance;
}
private:
static lockguard _lg;
static singleton *_instance;
private:
singleton() {}
singleton(const singleton&){ }
singleton& operator=(const singleton &) { }
class clearer
{
public:
clearer(){}
~clearer()
{
if(singleton::getInstance())
{
delete singleton::getInstance();
}
}
};
};
singleton *singleton::_instance = NULL;
lockguard singleton::_lg;
class singleton
{
public:
~singleton(){}
static singleton* getInstance()
{
if(_instance == NULL)
{
_instance = new singleton();
}
return _instance;
private:
static singleton *_instance;
private:
singleton(){ }
singleton(const singleton&){ }
singleton& operator=(const singleton &){}
};
singleton *singleton::_instance = NULL;
说明:
使用时只需调用singleton::getInstance()既可以获取到singleton的指针,但是使用完后要自己释放内存,解决的办法有:
(1)手动调用delete singleton::getInstance()
(2)注册atexit()函数以释放内存
(3)使用智能指针
(4)利用c++内嵌类和一个静态成员实现自动释放机制
在多线程环境下使用,如果大量线程调用到new时,可能会造成内存泄露,并且有可能前后获取的singleton对象不一致,解决的办法是用锁机制。
#include <memory>
#include <iostream>
#include <windows.h>
using namespace std;
class lockguard
{
private:
CRITICAL_SECTION m_cs;
public:
lockguard() { InitializeCriticalSection(&m_cs); }
~lockguard() { DeleteCriticalSection(&m_cs); }
class cguard
{
public:
cguard(lockguard &lg) : m_lg(lg) { m_lg.guard(); }
~cguard() { m_lg.unguard(); }
private:
lockguard &m_lg;
};
private:
void guard() { EnterCriticalSection(&m_cs); }
void unguard() { LeaveCriticalSection(&m_cs); }
friend class lockguard::cguard;
};
class singleton
{
public:
~singleton() {}
static singleton* getInstance()
{
if(_instance == NULL)
{
lockguard::cguard gd(_lg);
if(NULL == _instance)
{
static clearer clr;
_instance = new singleton();
}
}
return _instance;
}
private:
static lockguard _lg;
static singleton *_instance;
private:
singleton() {}
singleton(const singleton&){ }
singleton& operator=(const singleton &) { }
class clearer
{
public:
clearer(){}
~clearer()
{
if(singleton::getInstance())
{
delete singleton::getInstance();
}
}
};
};
singleton *singleton::_instance = NULL;
lockguard singleton::_lg;
相关文章推荐
- cocoapods导致Xcode崩溃的解决办法
- ORACLE数据库事务隔离级别
- javaScript概述
- java学习感想
- 算法导论—二分图
- 支付宝登陆界面(手势解锁的实现)
- 事件
- 面向对象
- 基于开源库jsoncpp的json字符串解析
- 启动Tomcat时遇到的 java.lang.IllegalArgumentException: Can't convert argument: null 异常
- Hibernate事务
- SPRING事务隔离级别及传播行为
- configure: error: newly created file is older than distributed files!
- Android开发的三重境界
- 开始记录我的程序员学习生涯--小小的我,也有大大的梦想;
- 公务员考试
- [LeetCode][JavaScript]Binary Tree Preorder Traversal
- TCP/IP
- centos安装hadoop-1.2.1
- ubuntu server设置静态IP和DNS