当前位置: 首页 > news >正文

「软件设计模式」单例模式

深入解析单例模式:从思想到C++实战实现

一、设计模式与单例模式思想

1.1 设计模式的价值

设计模式是软件工程领域的经验结晶,如同建筑领域的经典蓝图。它们提供了经过验证的解决方案模板,能有效解决以下问题:

  • 提高代码复用性
  • 提升系统可维护性
  • 增强代码可读性
  • 降低模块耦合度

1.2 单例模式核心思想

单例模式(Singleton Pattern)确保一个类:

  1. 仅有一个实例存在
  2. 提供全局访问点
  3. 严格控制实例化过程

适用场景包括:

  • 配置管理器
  • 日志记录器
  • 线程池
  • 数据库连接池
  • 硬件接口访问

二、C++实现方案对比

2.1 基础懒汉式(线程不安全)


#include <iostream>
using namespace std;
// 基础懒汉模式,非线程安全
class Singleton {
public:static Singleton* getInstance() {if (!instance) {instance = new Singleton();}return instance;}void doSomething() {cout << "Doing something..." << endl;}// 删除拷贝构造函数和赋值运算符Singleton(const Singleton&) = delete;Singleton& operator=(const Singleton&) = delete;private:Singleton() = default;~Singleton() = default;static Singleton* instance;
};// 初始化静态成员
Singleton* Singleton::instance = nullptr;#include <thread>
#include <vector>void threadFunction(int i) {Singleton* singleton = Singleton::getInstance();cout << "Thread " << i << " Singleton address: " << singleton << endl;singleton->doSomething();
}int main() {const int numThreads = 10;std::vector<std::thread> threads;for (int i = 0; i < numThreads; ++i) {threads.emplace_back(threadFunction, i);}for (auto& thread : threads) {thread.join();}return 0;
}

 运行结果:可以看出非线程安全的,创建了两个实例。

2.2 线程安全双检锁(C++11+)


 

#include <iostream>
#include <mutex>
#include <thread>
#include <vector>using namespace std;
class ThreadSafeSingleton {
public:static ThreadSafeSingleton* getInstance() {if (!instance) {std::lock_guard<std::mutex> lock(mutex);if (!instance) {instance = new ThreadSafeSingleton();}}return instance;}void doSomething() {cout << "Doing something..." << endl;}private:static ThreadSafeSingleton* instance;static std::mutex mutex;
};// 初始化静态成员
ThreadSafeSingleton* ThreadSafeSingleton::instance = nullptr;
std::mutex ThreadSafeSingleton::mutex;

#include <thread>
#include <vector>
#include "lazy_single_thread_safe.h"void LazySafeThreadFunction(int i) {ThreadSafeSingleton* singleton = ThreadSafeSingleton::getInstance();cout << "Thread Safe" << i << " Singleton address: " << singleton << endl;singleton->doSomething();
}// 线程安全懒汉模式 demo
int main() {const int numThreads = 10;std::vector<std::thread> threads1;for (int i = 0; i < numThreads; ++i) {threads1.emplace_back(LazySafeThreadFunction, i);}for (auto& thread : threads1) {thread.join();}return 0;
}

 运行结果:线程安全。

2.3 现代C++实现(最优方案)


#include <iostream>
using namespace std;/****  饿汉式是否 Lazy 初始化:否是否多线程安全:是描述:这种方式比较常用,但容易产生垃圾对象。优点:没有加锁,执行效率会提高。缺点:类加载时就初始化,浪费内存。它基于 classloader 机制避免了多线程的同步问题,不过,instance在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法,但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 instance 显然没有达到 lazy loading 的效果。
*/
#include <iostream>class EagerSingleton {
public:// 获取单例实例的静态方法static EagerSingleton* getInstance() {static EagerSingleton instance; // 在第一次调用时创建实例return &instance;}// 删除拷贝构造函数和赋值运算符,防止复制EagerSingleton(const EagerSingleton&) = delete;EagerSingleton& operator=(const EagerSingleton&) = delete;// 示例方法void doSomething() {cout << "Doing something..." << endl;}private:// 私有构造函数,防止外部实例化EagerSingleton() {std::cout << "EagerSingleton instance created." << std::endl;}
};

运行结果:

方案对比表:

特性基础懒汉式双检锁现代实现
线程安全✔️✔️
延迟初始化✔️✔️✔️
自动析构✔️
C++标准要求-≥11≥11
实现复杂度简单中等简单

三、关键实现细节解析

3.1 线程安全保证

  • 现代实现方案利用C++11的静态变量初始化特性
  • 编译器保证静态局部变量的线程安全
  • 双检锁方案需要内存屏障支持

3.2 资源管理

  • 现代方案自动处理析构
  • 指针方案需要自定义销毁逻辑
  • 可结合智能指针优化:
#include <memory>class SmartSingleton {
public:static SmartSingleton& getInstance() {static auto instance = std::make_shared<SmartSingleton>();return *instance;}// ...其他成员...
};

3.3 继承扩展方案

template <typename T>
class InheritableSingleton {
public:static T& getInstance() {static T instance;return instance;}protected:InheritableSingleton() = default;virtual ~InheritableSingleton() = default;
};class MyLogger : public InheritableSingleton<MyLogger> {friend class InheritableSingleton<MyLogger>;// 具体实现...
};

 

四、最佳实践与陷阱规避

4.1 使用建议

  1. 优先选择现代实现方案
  2. 明确单例的生命周期
  3. 做好线程安全测试
  4. 考虑依赖注入替代方案

4.2 常见陷阱

  • 循环依赖问题
  • 测试困难(使用虚函数增加可测试性)
  • 多线程环境下的初始化竞争
  • 异常安全性问题

4.3 性能考量

  • 现代方案无锁设计效率最高
  • 双检锁方案需要权衡锁开销
  • 饿汉式初始化可能影响启动速度

五、演进与替代方案

5.1 单例模式演进

  • 多例模式(Multiton)
  • 线程局部单例
  • 集群环境单例

5.2 现代替代方案

  • 依赖注入容器
  • 全局命名空间函数(权衡使用)
  • 服务定位器模式

六、总结

单例模式作为创建型模式的代表,在特定场景下能有效管理系统资源。但需要注意:

  • 不要滥用导致全局状态污染
  • 优先考虑依赖注入等现代方案
  • 关注线程安全和生命周期管理

正确使用单例模式,可以构建出高效、可控的软件系统。但记住,好的架构应该是灵活可扩展的,而不是充满各种全局状态的"单例陷阱"。

示例代码已在GCC 13.1和Clang 16.0测试通过,建议使用C++17及以上标准编译。实际项目中请根据具体需求选择合适的实现方案。


http://www.mrgr.cn/news/90694.html

相关文章:

  • Oracle DBA 诊断及统计工具-2
  • Linux运维——系统管理
  • Python教程:@staticmethod静态方法的简单示例
  • J6 X8B/X3C切换HDR各帧图像
  • 【每日关注】科技圈重要动态
  • MySQL中类似PostgreSQL中的string_agg函数--GROUP_CONCAT函数的使用
  • linux的三剑客和进程处理
  • 200个Python练手项目源码
  • rocketmq-netty通信设计-request和response
  • 【DeepSeek】Deepseek辅组编程-通过卫星轨道计算终端距离、相对速度和多普勒频移
  • 【Stable Diffusion部署至GNU/Linux】安装流程
  • Ubuntu启动geteck/jetlinks实战:Docker启动
  • Proxmox VE 8.3 qm 方式导入ESXi Linux OVA UEFI模式虚拟机
  • C++类和对象进阶:运算符重载深度详解
  • 在vscode中拉取gitee里的项目并运行
  • Python----PyQt开发(PyQt高级:手搓一个文件浏览器)
  • Druid GetConnectionTimeoutException解决方案之一
  • vue-model如何自定义指令,及批量注册自定义指令
  • 【GRPO】GRPO原理原文翻译
  • docker学习---第3步:docker实操大模型
  • Hive增量迁移方案与实操PB级
  • Linux初始化 配置yum源
  • 大数据学习之PB级百战出行网约车二
  • poi 将图片写入到excel文件中
  • vue开发06:前端通过webpack配置代理处理跨域问题
  • NumPy中生成和堆叠数组、生成切片的特殊对象:np.r_ np.c_ np.s_