1. 将构造函数私有化
2. 在类中定义一个静态的指向本类型的指针变量
3. 定义一个返回值为类指针的静态成员函数
- 四种单例模式的实现
- 1.全局类+单例类(全局类负责回收单例类对象堆空间)
#include <iostream>
using namespace std;
class singleinstance{
public:
static singleinstance * getInstance();
friend class AutoRelease;
private:
static singleinstance* _pinstance;
singleinstance();
~singleinstance();
};
class AutoRelease{
public:
AutoRelease(singleinstance*);
~AutoRelease();
private:
singleinstance* _psingle;
};
AutoRelease::AutoRelease(singleinstance* p)
:_psingle(p)
{
cout << "AutoRelease()" << endl;
}
AutoRelease::~AutoRelease()
{
if(_psingle)
{
delete _psingle;
_psingle = nullptr;
}
cout << "~AutoRelease()" << endl;
}
singleinstance * singleinstance::_pinstance = nullptr;
singleinstance::singleinstance()
{
cout << "singleinstance()" << endl;
}
singleinstance::~singleinstance()
{
cout << "~singleinstance()" << endl;
}
singleinstance* singleinstance::getInstance()
{
if( _pinstance == nullptr )
_pinstance = new singleinstance();
return _pinstance;
}
- 2.单例类+嵌套类(嵌套类负责回收单例类对象堆空间)
#include <iostream>
using namespace std;
class singleinstance
{
public:
static singleinstance *getInstance();
private:
class AutoRelease;
static AutoRelease rel;
static singleinstance *_pinstance;
singleinstance();
~singleinstance();
};
class singleinstance::AutoRelease
{
public:
AutoRelease();
~AutoRelease();
};
singleinstance::AutoRelease::AutoRelease()
{
cout << "AutoRelease()" << endl;
}
singleinstance::AutoRelease::~AutoRelease()
{
if (_pinstance)
{
delete _pinstance;
_pinstance = nullptr;
}
cout << "~AutoRelease()" << endl;
}
singleinstance *singleinstance::_pinstance = nullptr;
singleinstance::AutoRelease singleinstance::rel;
singleinstance::singleinstance()
{
cout << "singleinstance()" << endl;
}
singleinstance::~singleinstance()
{
cout << "~singleinstance()" << endl;
}
singleinstance *singleinstance::getInstance()
{
rel;
if (_pinstance == nullptr)
{
_pinstance = new singleinstance();
}
return _pinstance;
}
- 3.单例类+atexit
#include <iostream>
using namespace std;
class singleinstance
{
public:
static singleinstance *getInstance();
static void destroy();
private:
static singleinstance *_pinstance;
singleinstance();
~singleinstance();
};
singleinstance *singleinstance::_pinstance = nullptr;
singleinstance::singleinstance()
{
cout << "singleinstance()" << endl;
}
singleinstance::~singleinstance()
{
cout << "~singleinstance()" << endl;
}
singleinstance* singleinstance::getInstance()
{
if(!_pinstance){
_pinstance = new singleinstance();
atexit( destroy );
}
cout << "singleinstance::getInstance()" << endl;
return _pinstance;
}
- 4.单例类+pthread_once+atexit
#include <iostream>
using namespace std;
class singleinstance
{
public:
static singleinstance *getInstance();
static void destroy();
static void init();
private:
static pthread_once_t _once;
static singleinstance *_pinstance;
singleinstance();
~singleinstance();
};
singleinstance *singleinstance::_pinstance = nullptr;
singleinstance::singleinstance()
{
cout << "singleinstance()" << endl;
}
singleinstance::~singleinstance()
{
cout << "~singleinstance()" << endl;
}
pthread_once_t singleinstance::_once = PTHREAD_ONCE_INIT;
singleinstance *singleinstance::getInstance()
{
pthread_once(&_once, init);
cout << "singleinstance::getInstance()" << endl;
return _pinstance;
}
void singleinstance::init()
{
if (_pinstance == nullptr)
_pinstance = new singleinstance();
atexit(destroy);
cout << "singleinstance::init" << endl;
}
void singleinstance::destroy()
{
if (_pinstance)
{
delete _pinstance;
_pinstance = nullptr;
}
cout << "singleinstance::destroy" << endl;
}
标签:pinstance,模式,AutoRelease,singleinstance,static,单例,class
From: https://www.cnblogs.com/skycrash/p/17494191.html