对于设计模式来说,以前只是看过基础的理论,很多都没有实现和使用过。这段时间看到了别人C++代码中使用了单例模式,发现了很多新的东西,特此总结记录一下。说话比较啰嗦,希望由浅入深,帮助大家理解!
单例模式,顾名思义,即一个类只有一个实例对象。C++一般的方法是将构造函数、拷贝构造函数以及赋值操作符函数声明为private级别,从而阻止用户实例化一个类。那么,如何才能获得该类的对象呢?这时,需要类提供一个public&static的方法,通过该方法获得这个类唯一的一个实例化对象。这就是单例模式基本的一个思想。
下面首先讨论不考虑线程安全的问题(即:单线程环境),这样能体现出单例模式的本质思想。常见的单例模式分为两种:
1、饿汉式:即类产生的时候就创建好实例对象,这是一种空间换时间的方式
2、懒汉式:即在需要的时候,才创建对象,这是一种时间换空间的方式
首先说一下饿汉式:饿汉式的对象在类产生的时候就创建了,一直到程序结束才释放。即对象的生存周期和程序一样长,因此 该实例对象需要存储在内存的全局数据区,故使用static修饰。代码如下(注:类的定义都放在了一个头文件CSingleton.h中,为了节省空间,该类有些实现和定义就放在测试的主文件中,没有单独创建一个CSingleton.cpp文件):
头文件:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static CSingleton myInstance; // 单例对象在这里!
public:
static CSingleton* getInstance()
{
return &myInstance;
}
};
#endif
源文件:
//主文件,用于测试用例的生成
#include<iostream>
#include"CSingleton.h"
using namespace std;
CSingleton CSingleton::myInstance;
int main()
{
CSingleton *ct1 = CSingleton::getInstance();
CSingleton *ct2 = CSingleton::getInstance();
CSingleton *ct3 = CSingleton::getInstance();
return 0;
}
对于饿汉式来说,是
线程安全的。运行结果如下所示:
能够看出,类的实例只有一个,并且正常销毁。这里,问题来了!!!如果在类里面的定义改成如下形式:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static CSingleton *myInstance; // 这里改了!
public:
static CSingleton* getInstance()
{
return myInstance; // 这里也改了!
}
};
#endif
同样源文件改成如下:
//主文件,用于测试用例的生成
#include<iostream>
#include"CSingleton.h"
using namespace std;
CSingleton * CSingleton::myInstance=new CSingleton();
int main()
{
CSingleton *ct1 = CSingleton::getInstance();
CSingleton *ct2 = CSingleton::getInstance();
CSingleton *ct3 = CSingleton::getInstance();
return 0;
}
结果如下:
咦!怎么没有进入析构函数?这里就有问题了,如果单例模式的类中申请了其他资源,就无法释放,导致内存泄漏!
原因:此时全局数据区中,存储的并不是一个实例对象,而是一个实例对象的指针,即一个地址变量而已!实例对象呢?在堆区,因为是通过new得来的!虽然这样能够减小全局数据区的占用,把实例对象这一大坨都放到堆区。可是!如何释放资源呢?
首先能想到的第一个方法:我自己手动释放呀!我在程序结束的时候delete不就可以了?对!这是可以的,程序如下:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static CSingleton *myInstance;
public:
static CSingleton* getInstance()
{
return myInstance;
}
static void releaseInstance() // 这里加了个方法
{
delete myInstance;
}
};
#endif
源文件:
//主文件,用于测试用例的生成
#include<iostream>
#include"CSingleton.h"
using namespace std;
CSingleton * CSingleton::myInstance=new CSingleton();
int main()
{
CSingleton *ct1 = CSingleton::getInstance();
CSingleton *ct2 = CSingleton::getInstance();
CSingleton *ct3 = CSingleton::getInstance();
CSingleton::releaseInstance(); // 手动释放
return 0;
}
运行结果如下
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static CSingleton *myInstance;
public:
static CSingleton* getInstance()
{
return myInstance;
}
private:
// 定义一个内部类
class CGarbo{
public:
CGarbo(){};
~CGarbo()
{
if (nullptr != myInstance)
{
delete myInstance;
myInstance = nullptr;
}
}
};
// 定义一个内部类的静态对象
// 当该对象销毁时,顺带就释放myInstance指向的堆区资源
static CGarbo m_garbo;
};
#endif
源文件如下:
//主文件,用于测试用例的生成
#include<iostream>
#include"CSingleton.h"
using namespace std;
CSingleton * CSingleton::myInstance=new CSingleton();
CSingleton::CGarbo CSingleton::m_garbo; // 注意这里!!!
int main()
{
CSingleton *ct1 = CSingleton::getInstance();
CSingleton *ct2 = CSingleton::getInstance();
CSingleton *ct3 = CSingleton::getInstance();
return 0;
}
运行结果如下:
可见能够正常释放堆区申请的资源了!问题解决!这里又会想到,C++11中把boost库中的智能指针变成标准库的东西。智能指针可以在引用计数为0的时候自动释放内存,方便使用者管理内存,为什么不尝试用一下智能指针呢?现在修改代码如下(不可以正常运行):
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
#include<memory>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static shared_ptr<CSingleton> myInstance;
public:
static shared_ptr<CSingleton> getInstance()
{
return myInstance;
}
};
#endif
主文件
//主文件,用于测试用例的生成
#include<iostream>
#include<memory>
#include"CSingleton.h"
using namespace std;
shared_ptr<CSingleton> CSingleton::myInstance(new CSingleton());
int main()
{
shared_ptr<CSingleton> ct1 = CSingleton::getInstance();
shared_ptr<CSingleton> ct2 = CSingleton::getInstance();
shared_ptr<CSingleton> ct3 = CSingleton::getInstance();
return 0;
}
结果编译都过不了。仔细一看,原来智能指针shared_ptr无法访问私有化的析构函数。当 shared_ptr内部的引用计数为零时,会自动调用所指对象的析构函数来释放内存。然而,此时单例模式类的析构函数为private,故出现编译错误。如何修改呢?当然,最简单的方法是把析构函数变成public。但是如果某用户不小心手残,显式调用了析构函数,这不就悲剧了。第二种方法,就是不通过析构函数来释放对象的资源。怎么办呢?不要忘了shared_ptr在定义的时候可以指定删除器(deleter)。可是通过测试,无法直接传入析构函数。我现在想到的一个方法是,在单例函数内部再定义一个Destory函数,该函数也要为static的,即通过类名可直接调用。当然,在使用该单例类的时候,也需要使用shared_ptr获取单一实例对象。代码如下:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
#include<memory>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static void Destory(CSingleton *){ cout << "在这里销毁单例对象!" << endl; };//注意这里
static shared_ptr<CSingleton> myInstance;
public:
static shared_ptr<CSingleton> getInstance()
{
return myInstance;
}
};
#endif
主文件
//主文件,用于测试用例的生成
#include<iostream>
#include<memory>
#include"CSingleton.h"
using namespace std;
shared_ptr<CSingleton> CSingleton::myInstance(new CSingleton(), CSingleton::Destory);
int main()
{
shared_ptr<CSingleton> ct1 = CSingleton::getInstance();
shared_ptr<CSingleton> ct2 = CSingleton::getInstance();
shared_ptr<CSingleton> ct3 = CSingleton::getInstance();
return 0;
}
运行结果如下:
删除器声明时(即static void Destory(CSingleton *)),需要传入该对象的指针,否则编译出错。这里仅作说明,用不上该指针,故没有给形参取名字。详细说明一下,通过去掉形参的声明,出错后,就能定位到 头文件<memory>中释放对象的位置,代码如下:
template<class _Ux>
void _Resetp(_Ux *_Px)
{ // release, take ownership of _Px
_TRY_BEGIN // allocate control block and reset
_Resetp0(_Px, new _Ref_count<_Ux>(_Px));
_CATCH_ALL // allocation failed, delete resource
delete _Px;
_RERAISE;
_CATCH_END
}
template<class _Ux,
class _Dx>
void _Resetp(_Ux *_Px, _Dx _Dt)
{ // release, take ownership of _Px, deleter _Dt
_TRY_BEGIN // allocate control block and reset
_Resetp0(_Px, new _Ref_count_del<_Ux, _Dx>(_Px, _Dt));
_CATCH_ALL // allocation failed, delete resource
_Dt(_Px);
_RERAISE;
_CATCH_END
}
可以看出,上面代码中第一个_Resetp就是没有指明deleter时调用的函数。第二个_Resetp是指明deleter时调用的函数,此时deleter _Dt需要接收一个参数_Px(即指向shared_ptr内部对象的指针),从而释放
shared_ptr内部对象的内容。
扯远了!
回归正题,到这里,我们可以看到:饿汉式的单例模式原理很简单,也很好写,并且线程安全,不需要考虑线程同步!
然后,聊一聊懒汉式单例模式。
懒汉式单例模式,是在第一次调用getInstance()的时候,才创建实例对象。想到这里,是不是直接把对象定义为static,然后放在getInstance()中。第一次进入该函数,就创建实例对象,然后一直到程序结束,释放该对象。动手试一试。代码如下:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
public:
static CSingleton * getInstance()
{
static CSingleton myInstance;
return &myInstance;
}
};
#endif
主文件
//主文件,用于测试用例的生成
#include<iostream>
#include"CSingleton.h"
using namespace std;
int main()
{
CSingleton * ct1 = CSingleton::getInstance();
CSingleton * ct2 = CSingleton::getInstance();
CSingleton * ct3 = CSingleton::getInstance();
return 0;
}
运行结果如下:
程序正常运行。此时,如果想把对象放在堆区,也可以这么实现:
// 饿汉式单例的实现
#ifndef C_SINGLETON_H
#define C_SINGLETON_H
#include<iostream>
#include<memory>
using namespace std;
class CSingleton
{
private:
CSingleton(){ cout << "单例对象创建!" << endl; };
CSingleton(const CSingleton &);
CSingleton& operator=(const CSingleton &);
~CSingleton(){ cout << "单例对象销毁!" << endl; };
static CSingleton *myInstance;
public:
static CSingleton * getInstance()
{
if (nullptr == myInstance)
{
myInstance = new CSingleton();
}
return myInstance;
}
private:
// 定义一个内部类
class CGarbo{
public:
CGarbo(){};
~CGarbo()
{
if (nullptr != myInstance)
{
delete myInstance;
myInstance = nullptr;
}
}
};
// 定义一个内部类的静态对象
// 当该对象销毁时,顺带就释放myInstance指向的堆区资源
static CGarbo m_garbo;
};
#endif
主文件如下:
//主文件,用于测试用例的生成
#include<iostream>
#include<memory>
#include"CSingleton.h"
using namespace std;
CSingleton * CSingleton::myInstance = nullptr;
CSingleton::CGarbo CSingleton::m_garbo;
int main()
{
CSingleton * ct1 = CSingleton::getInstance();
CSingleton * ct2 = CSingleton::getInstance();
CSingleton * ct3 = CSingleton::getInstance();
return 0;
}
运行结果如下:
咳咳!!
对于懒汉式这两种情况,当调用getInstance()函数时,如果对象还没产生(第一种状态),就需要产生对象,然后返回对象指针。如果对象已经存在了(第二种状态),就直接返回对象指针。当单线程时,没有问题。但是,多线程情况下,如果一个函数中不同状态有不同操作,就要考虑线程同步的问题了。因此,我们需要修改一下getInstance中的实现。
举例如下:
第一种懒汉式:
static CSingleton * getInstance()
{
lock();
static CSingleton myInstance;
unlock();
return &myInstance;
}
第二种懒汉式
static CSingleton * getInstance()
{
if (nullptr == myInstance)
{
lock();// 需要自己采用适当的互斥方式
if (nullptr == myInstance)
{
myInstance = new CSingleton();
}
unlock();
}
return myInstance;
}
注意!由于线程和使用的操作系统有关,因此这里的lock()和unlock()函数仅作说明示意,并未实现。都是常见的线程同步方法,可以查询其他资料来实现。这里不再赘述。
当然,懒汉式的实现也可以采用shared_ptr来实现。但是很多思想与实现方法和饿汉式的有一定重复,因此这里也不再给出。
结束语
本人水平有限,如果代码有问题或者更好的方法,欢迎留言!大家一起讨论,一起进步!