您的位置:首页 > 编程语言 > C语言/C++

设计模式——单例模式C++实现

2016-06-18 21:20 381 查看


引出问题:

设计一个类,我们只能生成该类的一个对象实例。


不好的解法:只适用于单线程环境

因为该类只能生成一个对象实例,那么该类的构造函数必须是私有的,从而避免他人创建实例。在需要的时候创建该类的一个对象。

下面是程序实现:

/*************************************************************************
> File Name: SigleInstance.cpp
> Author:
> Mail:
> Created Time: Sat 18 Jun 2016 06:44:44 PM CST
************************************************************************/

#include <iostream>
using namespace std;

//只能生成一个类对象的类
class SigleInstance
{
public:
//指向类的一个实例对象
static SigleInstance * instance;
private:
//私有的构造函数
SigleInstance(){cout << "执行SigleInstance 构造函数" << endl;}
public:
//创建一个实例对象
static SigleInstance * getSigleInstance(){
if(instance == NULL)
instance = new SigleInstance();

return instance;
}
};

//初始化静态成员变量
SigleInstance * SigleInstance::instance = NULL;

//测试函数
int main()
{

SigleInstance * SigleInstance1 = SigleInstance::getSigleInstance();
SigleInstance * SigleInstance2 = SigleInstance::getSigleInstance();
SigleInstance * SigleInstance3 = SigleInstance::getSigleInstance();

if(SigleInstance1 == SigleInstance2){
cout << "SigleInstance1 == SigleInstance2" << endl;
}

if(SigleInstance1 == SigleInstance3){
cout << "SigleInstance1 == SigleInstance3" << endl;
}

return 0;
}


下面是程序的输出:
执行SigleInstance 构造函数
SigleInstance1 == SigleInstance2
SigleInstance1 == SigleInstance3

从程序的输出可以看出,SigleInstance1和SigleInstance2和SigleInstance3指向的都是同一个对象。


不好的解法2:可以在多线程环境下工作,但是效率比较低

解法1中的代码在单线程的情况下工作正常,但在多线程的环境下就有问题了。如果两个线程同时运行判断instance是否为NULL的if语句,并且instance实例还没有创建时,那么两个线程都会创建一个实例,那么此时就不满足单例模式的要求了。

为了保证在多线程的环境下我们还是只能得到一个实例,需要加上一个同步锁。

下面是实现的程序:

/*************************************************************************
> File Name: SigleInstance.cpp
> Author:
> Mail:
> Created Time: Sat 18 Jun 2016 06:44:44 PM CST
************************************************************************/

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

//只能生成一个类对象的类
class SigleInstance
{
public:
//指向类的一个实例对象
static SigleInstance * instance;

//定义一个互斥锁变量
static pthread_mutex_t instanceMutex;

private:
//私有的构造函数
SigleInstance(){cout << "执行SigleInstance 构造函数" << endl;}
public:
//创建一个实例对象
static SigleInstance * getSigleInstance(){
//获得互斥锁
pthread_mutex_lock(&instanceMutex);

if(instance == NULL)
instance = new SigleInstance();

//睡眠仅仅为了测试其它线程是否处于等待状态,真正的程序是不需要该延迟的
sleep(3);

//释放互斥锁
pthread_mutex_unlock(&instanceMutex);

return instance;
}
};

//初始化静态成员变量
SigleInstance * SigleInstance::instance = NULL;
//初始化互斥锁变量
pthread_mutex_t SigleInstance::instanceMutex = PTHREAD_MUTEX_INITIALIZER;

//线程要执行的函数
void * threadFun(void *arg)
{
SigleInstance * instance = SigleInstance::getSigleInstance();
cout << "theread" << dec << pthread_self() << ", instance's address = " << hex << instance << endl;

pthread_exit(NULL);
}

//测试函数
int main()
{
const int NUM = 3;
pthread_t threadId[NUM];

//创建NUM个线程
for(int i = 0; i < NUM; ++i){
pthread_create(&threadId[i], NULL, threadFun, NULL);
}

for(int i = 0; i < NUM; ++i){
pthread_join(threadId[i], NULL);
}

return 0;
}

下面是程序的输出:

执行SigleInstance 构造函数

theread139706800436992, instance's address = 0x7f10000008c0

theread139706808829696, instance's address = 0x7f10000008c0

theread139706817222400, instance's address = 0x7f10000008c0

从输出结果可以看出,执行了一个构造函数,每个线程获得的实例对象的地址都是相同的,表明只有一个实例对象。并且每个线程之间的等待时间为2秒。

在上面的程序中,因为每个时刻只能有一个线程获得互斥锁,当第一个线程获得互斥锁的时候,其它线程只能等待。当第一个线程获得互斥锁后,发现实例还没有创建时,它会创建一个实例。接着第一个线程释放互斥锁,此时第二个线程可以获得互斥锁,并运行下面的代码,但是此时已经创建了一个实例,所以,第二个线程直接返回,不会再创建一个实例。这样就可以保证在多线程的环境下也只能创建一个实例。

但是解法2不是很完美,因为每次通过获取实例对象的时候都会试图获得互斥锁,但是加锁和解锁是非常耗时的操作,我们应该尽量避免加锁和解锁的操作。具体改进看下面解法3。


改进解法2:加锁前后两次判断实例是否已经存在

下面是实现的程序:

/*************************************************************************
> File Name: SigleInstance.cpp
> Author:
> Mail:
> Created Time: Sat 18 Jun 2016 06:44:44 PM CST
************************************************************************/

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

//只能生成一个类对象的类
class SigleInstance
{
public:
//指向类的一个实例对象
static SigleInstance * instance;

//定义一个互斥锁变量
static pthread_mutex_t instanceMutex;

private:
//私有的构造函数
SigleInstance(){cout << "执行SigleInstance 构造函数" << endl;}
public:
//创建一个实例对象
static SigleInstance * getSigleInstance(){

//如果实例没有创建,则加锁并创建实例
//如果实例已经创建,则直接返回该实例的指针
if(instance == NULL){
//获得互斥锁
pthread_mutex_lock(&instanceMutex);

if(instance == NULL)
instance = new SigleInstance();

//睡眠仅仅为了测试其它线程是否处于等待状态,真正的程序是不需要该延迟的
sleep(3);

//释放互斥锁
pthread_mutex_unlock(&instanceMutex);
}

return instance;
}
};

//初始化静态成员变量
SigleInstance * SigleInstance::instance = NULL;
//初始化互斥锁变量
pthread_mutex_t SigleInstance::instanceMutex = PTHREAD_MUTEX_INITIALIZER;

//线程要执行的函数
void * threadFun(void *arg)
{
SigleInstance * instance = SigleInstance::getSigleInstance();
cout << "theread" << dec << pthread_self() << ", instance's address = " << hex << instance << endl;

pthread_exit(NULL);
}

//测试函数
int main()
{
const int NUM = 3;
pthread_t threadId[NUM];

//创建NUM个线程
for(int i = 0; i < NUM; ++i){
pthread_create(&threadId[i], NULL, threadFun, NULL);
}

for(int i = 0; i < NUM; ++i){
pthread_join(threadId[i], NULL);
}

return 0;
}


下面是程序的输出:

执行SigleInstance 构造函数

theread139991850669824, instance's address = 0x7f525c0008c0

theread139991859062528, instance's address = 0x7f525c0008c0

theread139991842277120, instance's address = 0x7f525c0008c0

从程序的输出情况来看,线程139991842277120是创建该类实例的线程,因为,它经过了睡眠,所以是最后输出。另外两个线程输出比较早,在线程139991842277120创建完实例之后,另外两个线程就返回了,这两个线程没有进行加锁和解锁操作。


解法4:利用静态变量的初始化顺序

这种方法其实是单例模式中的饿汉式方式,上面的方法是单例模式中的懒汉式方式。

这种方法的实例对象,是在类变量instance初始化的时候就创建了该实例,也就是说程序一开始运行,

参考文章:

设计模式——单例模式java实现

下面是程序实现:

/*************************************************************************
> File Name: SigleInstance.cpp
> Author:
> Mail:
> Created Time: Sat 18 Jun 2016 06:44:44 PM CST
************************************************************************/

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

//只能生成一个类对象的类
class SigleInstance
{
public:
//指向类的一个实例对象
static SigleInstance * instance;

private:
//私有的构造函数
SigleInstance(){cout << "执行SigleInstance 构造函数" << endl;}
public:
//创建一个实例对象
static SigleInstance * getSigleInstance(){
return instance;
}
};

//初始化静态成员变量,此时直接创建一个实例对象
SigleInstance * SigleInstance::instance = new SigleInstance();

//线程要执行的函数
void * threadFun(void *arg)
{
SigleInstance * instance = SigleInstance::getSigleInstance();
cout << "theread" << dec << pthread_self() << ", instance's address = " << hex << instance << endl;

pthread_exit(NULL);
}

//测试函数
int main()
{
cout << "main start..." << endl;

const int NUM = 3;
pthread_t threadId[NUM];

//创建NUM个线程
for(int i = 0; i < NUM; ++i){
pthread_create(&threadId[i], NULL, threadFun, NULL);
}

for(int i = 0; i < NUM; ++i){
pthread_join(threadId[i], NULL);
}

return 0;
}


下面是程序的输出:

执行SigleInstance 构造函数

main start...

theread140681870264064, instance's address = 0xbaa010

theread140681878656768, instance's address = 0xbaa010

theread140681887049472, instance's address = 0xbaa010

从输出结果可以看出,在main函数开始执行之前,类实例已经创建完了。

更多详情,请前往本人博客网站:个人博客网站
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 单例模式