单例模式(懒汉模式、饿汉模式)

单例模式要解决的问题

主要用于解决一个全局使用的类频发的创建和销毁的问题。单例模式下确保某一个类中只有一个实例,并且自行实例化,然后自行提供给整个系统使用。

单例的实现步骤

1、将该类的构造方法定义为私有方法。这样其他程序就无法通过调用该类的构造方法来实例化该类的对象,只能通过类提供的静态方法,得到该类的唯一实例;

2、在该类中提供一个静态方法。当调用这个方法时,如果存在实例,就返回这里实例。如果不存在,就创建这个实例,并返回。

代码实现

1、懒汉模式(线程不安全)

// 经典懒汉模式(用时再创建)
class Singleton // 线程不安全
{
public:
	static Singleton* getInstance() 
	{
		if (instance == NULL)
		{
			instance = new Singleton();
		}
		return instance;
	}
protected:
	Singleton() { cout << "Singleton()" << endl; }
private:
	static Singleton* instance;
};

Singleton* Singleton::instance = nullptr;

在多线程中,可能存在多个线程同时进入,并创建多个实例的情况。因此需要使用锁来确保线程安全。

2、线程安全的懒汉模式

class Singleton2 // 懒汉模式(线程安全)
{
public:
	static Singleton2* getInstance()
	{  // 双重锁机制
		if (instance == NULL)
		{ // 若不为空,说明已经存在实例,为空,则进入后加锁
			mutex.lock();
			if (instance == NULL)
			{ // 再次判断,确保只有一个线程进入
				instance = new Singleton2(); //创建实例
			}
			mutex.unlock(); //解锁
		}
		return instance;
	}
protected:
	Singleton2() { cout << "Singleton2()" << endl; }
private:
	static Singleton2* instance;
	static std::mutex mutex;
};

Singleton2* Singleton2::instance = NULL;
mutex Singleton2::mutex;

通过双重锁就可以确保只存在一个实例。

3、恶汉模式

class Singleton1  // 饿汉模式,基于类加载实现单例(不管用不用,先创建出来)
{
public:
	static Singleton1* getInstance()
	{
		return instance;
	}
protected:
	Singleton1() { cout << "Singleton1()" << endl; }
private:
	static Singleton1* instance;
}; 

// 必须类外初始化,在类加载时就执行,可能产生垃圾对象
Singleton1* Singleton1::instance = new Singleton1();

恶汉模式,本身就是线程安全的,其基于类加载确保实现单例。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值