【C++设计模式】(三)创建型模式:单例模式

文章目录

(三)创建型模式:单例模式

单例模式在于确保一个类只有一个实例,并提供一个全局访问点来访问该实例。在某些情况下,某些代码组件(如线程池,日志记录器)需要在整个应用程序中共享,使用单例模式可以实现组件资源的复用,并简化系统设计。

单例模式实现方式主要包括饿汉式和懒汉式两种。

饿汉式

饿汉式是指在类加载的时候就创建单例实例,不管后续是否会使用这个实例。

cpp 复制代码
class Singleton {
private:
    static Singleton* instance; // 静态成员变量,属于类本身而不是类的任何特定对象
    
    Singleton() {}              // 私有构造函数防止外部实例化

public:
    static Singleton* getInstance() {  // 全局访问点
        return instance;
    }
};

Singleton* Singleton::instance = new Singleton();  // 在静态成员变量初始化时创建实例

在多线程环境下,饿汉式是线程安全的,因为实例在类加载时就已经创建好了,不存在并发访问创建实例的问题。

示例:

cpp 复制代码
#include <iostream>  
  
class Singleton {  
private:  
    static Singleton instance; // 静态实例,在类加载时构造  
    Singleton() {} // 私有构造函数  
  
public:  
    static Singleton& getInstance() {  
        return instance;  
    }  
  
    void doSomething() {  
        std::cout << "Doing something..." << std::endl;  
    }  
};  
  
Singleton Singleton::instance; // 静态实例初始化  
  
int main() {  
    Singleton& s1 = Singleton::getInstance();  
    Singleton& s2 = Singleton::getInstance();  
  
    if (&s1 == &s2) {  
        std::cout << "s1 and s2 are the same instance" << std::endl;  
    }  
  
    s1.doSomething();  
    return 0;  
}
复制代码
s1 and s2 are the same instance
Doing something...

懒汉式

懒汉式是指在第一次使用时才会创建单例实例,实例的创建被延迟到第一次使用 getInstance() 方法时。

cpp 复制代码
class Singleton {
private:
    static Singleton* instance;
    
    Singleton() {}  

public:
    static Singleton* getInstance() {
        if (instance == nullptr) { // 第一次使用时才会创建单例实例
            instance = new Singleton();
        }
        return instance;
    }
};

Singleton* Singleton::instance = nullptr;

示例:

cpp 复制代码
#include <iostream>  
  
class Singleton {  
private:  
    static Singleton* instance;  
    Singleton() {} // 私有构造函数  
  
public:  
    static Singleton* getInstance() {  
        if (instance == nullptr) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
  
    ~Singleton() {  
        delete instance; // 注意:在程序结束时需要确保只调用一次析构函数  
        instance = nullptr;  
    }  
  
    void doSomething() {  
        std::cout << "Doing something..." << std::endl;  
    }  
};  
  
Singleton* Singleton::instance = nullptr; // 静态成员变量初始化  
  
int main() {  
    Singleton* s1 = Singleton::getInstance();  
    Singleton* s2 = Singleton::getInstance();  
  
    if (s1 == s2) {  
        std::cout << "s1 and s2 are the same instance" << std::endl;  
    }  
  
    s1->doSomething();  
    return 0;  
}
复制代码
s1 and s2 are the same instance
Doing something...

懒汉式在多线程环境下是不安全的,因为多个线程可能同时进入判断条件,导致创建多个实例。因此,需要通过加锁等机制来保证线程安全:

cpp 复制代码
 static std::mutex mtx;
 static Singleton* instance;
 
 Singleton* Singleton::getInstance() {
 	// 使用互斥锁(`std::mutex`)来保证只有一个线程能够创建实例。
     std::lock_guard<std::mutex> lock(mtx);
     if (instance == nullptr) {
         instance = new Singleton();
     }
     return instance;
 }

为了避免每次调用都加锁,产生额外的性能开销,可以在加锁的基础上,进行双重检查:

cpp 复制代码
  static std::mutex mtx;
  static Singleton* instance;
  
  Singleton* Singleton::getInstance() {
      if (instance == nullptr) {
          std::lock_guard<std::mutex> lock(mtx);
          if (instance == nullptr) {
              instance = new Singleton();
          }
      }
      return instance;
  }

饿汉式 v.s. 懒汉式

在饿汉式单例模式中,单例的实例在程序启动时就立即创建。这种方式的好处在于它的简单性和线程安全性(无需额外的同步机制)。

在懒汉式单例模式中,单例的实例是在首次被需要时才被创建。这种方式的好处在于它可以延迟实例的创建,从而减少程序启动时的资源消耗和初始化时间。

相关推荐
大胆飞猪16 小时前
高并发内存池日志
c++·项目
想唱rap17 小时前
C++ string类的使用
开发语言·c++·笔记·算法·新浪微博
胖咕噜的稞达鸭17 小时前
C++中的父继子承(2)多继承菱形继承问题,多继承指针偏移,继承组合分析+高质量习题扫尾继承多态
c语言·开发语言·数据结构·c++·算法·链表·c#
猫头虎17 小时前
OpenAI发布构建AI智能体的实践指南:实用框架、设计模式与最佳实践解析
人工智能·设计模式·开源·aigc·交互·pip·ai-native
昨天的猫17 小时前
项目中原来策略模式这么玩才有意思😁😁😁
设计模式
小苏兮17 小时前
【C++】priority_queue和deque的使用与实现
开发语言·c++·学习
Mr_WangAndy17 小时前
C++设计模式_行为型模式_迭代器模式Iterator
c++·设计模式·迭代器模式
白衣鸽子18 小时前
【基础数据篇】数据遍历大师:Iterator模式
后端·设计模式
muxin-始终如一18 小时前
系统重构过程以及具体方法
设计模式·重构
FL162386312918 小时前
C++基于opencv实现的暗通道的先验图像去雾
c++·opencv·计算机视觉