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

单例设计模式

应用场景

当多个类都需要调用某一个类的一些公共接口,同时不想创建多个该类的对象,可以考虑将该类封装为一个单例模式。

特点

单例模式的特点:
1.只有一个单例对象。因而构造函数需要私有化,不能被外界访问。同时也不能进行对象的拷贝和赋值操作,故而也需要将拷贝构造函数和赋值运算符重载函数删除或者私有。
2.提供对外获取实例对象的公共访问接口。其它类可以通过这个接口获取单例对象。

设计模式分类

单例模式可以分为懒汉模式和饿汉模式。

所谓懒汉模式,就是当程序运行过程中需要单例对象的时候再创建。

所谓饿汉模式,就是定义类的时候就创建单例对象。

懒汉设计模式

1.懒汉设计模式下的单例模式,如果项目很简单,不需要考虑多线程的问题,可以这样编写懒汉模式下的单例模式。

cpp 复制代码
class Singleton
{
private:
	Singleton()=default;
	Singleton(const Singleton&)=delete;
	Singleton& operator=(const Singleton&) = delete;
public:
	static Singleton* getInstance() 
	{
		if (m_pInstance == nullptr)
		{
			m_pInstance = new Singleton;
		}
		return m_pInstance;
	}
private:
	static Singleton *m_pInstance;
	static mutex m_mutex;
};
Singleton *Singleton::m_pInstance = nullptr;
mutex Singleton::m_mutex;

2.下面是懒汉设计模式下的单例模式示例,这里考虑多线程,故而引入双重检查锁来保护单例对象的创建,是线程安全的。

cpp 复制代码
class Singleton
{
private:
	Singleton()=default;
	Singleton(const Singleton&)=delete;
	Singleton& operator=(const Singleton&) = delete;
public:
	static Singleton* getInstance() 
	{//双重检查锁定
		if (m_pInstance == nullptr)//注意这里的判断条件
		{
			lock_guard<mutex> locker(m_mutex);//注意这里的参数
			if (m_pInstance == nullptr)
			{
				m_pInstance = new Singleton;
			}
		}
		return m_pInstance;
	}
private:
	static Singleton *m_pInstance;
	static mutex m_mutex;
};
Singleton *Singleton::m_pInstance = nullptr;
mutex Singleton::m_mutex;

3.还可以在支持C++11的编译器基础之上,使用static创建懒汉模式下的单例对象。(并发程序中会等待变量初始化完成)也是线程安全的。

cpp 复制代码
class Singleton 
{
private:
	Singleton() = default;
	Singleton(const Singleton&) = delete;
	Singleton& operator=(const Singleton &) = delete;
public:
	static Singleton* getInstance() 
	{
		static Singleton instance;
		return &instance;//注意返回的指针,若返回变量对象,会调用拷贝构造函数出现错误
	}
};

饿汉设计模式

这里提供两种编写的方式,但无论哪种,都是先创建实例对象。

1.以指针的形式创建对象。

cpp 复制代码
class Singleton
{
private:
	Singleton() = default;
	Singleton(const Singleton&) = delete;
	Singleton& operator=(const Singleton&) = delete;
public:
	static Singleton* getInstance()
	{
		return m_pInstance;
	}
private:
	static Singleton *m_pInstance;
};
Singleton* Singleton::m_pInstance = new Singleton;

2.以对象的方式创建。

cpp 复制代码
class Singleton 
{
private:
	Singleton() = default;
	Singleton(const Singleton&) = delete;
	Singleton& operator=(const Singleton&) = delete;
public:
	static Singleton* getInstance() 
	{
		return &m_instance;
	}
private:
	static Singleton m_instance;
};
Singleton Singleton::m_instance;

使用

编写的测试代码

这里创建两次单例对象,输出对象的地址,看看是否是同一个实例。

cpp 复制代码
Singleton *p1 = Singleton::getInstance();
Singleton *p2 = Singleton::getInstance();
cout << "p1:" << p1 << " p2:" << p2 << endl;

运行结果

注意:本文没有给出编程过程中实际需要的头文件,测试代码也只给出用于测试的部分。

相关推荐
不当菜虚困几秒前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式
Niuguangshuo3 分钟前
Python设计模式:MVC模式
python·设计模式·mvc
YHY_13s8 分钟前
访问者模式
c++·访问者模式
我也不曾来过136 分钟前
list底层原理
数据结构·c++·list
Lei活在当下37 分钟前
【现代 Android APP 架构】01. APP 架构综述
android·设计模式·架构
前端大白话39 分钟前
震惊!90%前端工程师都踩过的坑!computed属性vs methods到底该怎么选?一文揭秘高效开发密码
前端·vue.js·设计模式
A charmer42 分钟前
C++ 日志系统实战第三步:熟悉掌握各种设计模式
c++·日志系统
前端大白话42 分钟前
前端必看!figure标签在响应式图片排版中的王炸操作,grid/flex布局实战指南
前端·设计模式·html
Ethon_王1 小时前
STL容器适配器详解:queue篇
c++
ApeAssistant1 小时前
Spring + 设计模式 (十四) 行为型 - 观察者模式
spring·设计模式