单例模式的C++简单实现

一、懒汉模式

具体例子如下:

/**
  *		懒汉式单例模式
  */

#include <iostream>

using namespace std;

/* 定义Singleton类,用来表示单例模式 */
class Singleton
{
public:
	static Singleton *getInstance();
private:
	Singleton();
	Singleton(const Singleton &other);
	Singleton &operator=(const Singleton &other);

	static Singleton *instance;
};

/* 实现Singleton类中的方法和静态数据成员 */
Singleton *Singleton::getInstance()
{
	return instance;
}

Singleton::Singleton(){cout << "Singleton()" << endl;}
Singleton::Singleton(const Singleton &other){cout << "Singleton(const Singleton &other)" << endl;}
Singleton &Singleton::operator=(const Singleton &other){cout << "Singleton &operator=(const Singleton &other)" << endl;}

Singleton *Singleton::instance = new Singleton();


/* 程序入口 */
int main()
{
	Singleton* s1 = Singleton::getInstance();
	Singleton* s2 = Singleton::getInstance();
	Singleton* s3 = Singleton::getInstance();

	cout << "s1 address is " << s1 << endl;
	cout << "s2 address is " << s2 << endl;
	cout << "s3 address is " << s3 << endl;
	
	return 0;
}
注意有内存泄漏。

二、饿汉模式

具体实现如下:

/**
  *	饿汉式单例模式
  */

#include <iostream>
#include <pthread.h>

using namespace std;

/* 定义Singleton类,用来表示单例模式 */
class Singleton
{
public:
	static Singleton *getInstance();
private:
	Singleton();
	Singleton(const Singleton &other);
	Singleton &operator=(const Singleton &other);

	static Singleton *instance;
	static pthread_mutex_t mutex;

	/* 定义一个内嵌类用于清除Singleton实例 */
	class DestroySingleton
	{
	public:
		~DestroySingleton();
	};

	static DestroySingleton destroyInstance;
};

/* 实现Singleton类中的方法和静态数据成员 */
Singleton *Singleton::getInstance()
{
	if(NULL == instance)
	{
		pthread_mutex_lock(&mutex);			/* 上锁 */
		if(NULL == instance)
		{
			instance = new Singleton();
		}
		pthread_mutex_unlock(&mutex);		/* 解锁 */
	}
	return instance;
}

Singleton::Singleton(){cout << "Singleton()" << endl;}
Singleton::Singleton(const Singleton &other){cout << "Singleton(const Singleton &other)" << endl;}
Singleton &Singleton::operator=(const Singleton &other){cout << "Singleton &operator=(const Singleton &other)" << endl;}

Singleton *Singleton::instance = NULL;
pthread_mutex_t Singleton::mutex = PTHREAD_MUTEX_INITIALIZER;
Singleton::DestroySingleton Singleton::destroyInstance;


/* 实现内部类DestroySingleton的析构函数 */
Singleton::DestroySingleton::~DestroySingleton()
{
	if(NULL != Singleton::instance)
	{
		delete Singleton::instance;
		Singleton::instance = NULL;		/* 防止野指针 */
		cout << "~DestroySingleton()" << endl;
	}
}


/* 程序入口 */
int main()
{
	Singleton* s1 = Singleton::getInstance();
	Singleton* s2 = Singleton::getInstance();
	Singleton* s3 = Singleton::getInstance();

	cout << "s1 address is " << s1 << endl;
	cout << "s2 address is " << s2 << endl;
	cout << "s3 address is " << s3 << endl;
	
	return 0;
}
注意多线程的同步问题,加锁与解锁。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值