C++设计模式代码--单例模式

参考:5. 单例模式(Singleton) (yuque.com)

1、什么是单例模式

保证一个类只有一个实例,并提供一个访问该实例的全局节点;

2、什么情况下需要单例模式

某个类的对象在软件运行之初就创建,并且在软件的很多地方都需要读写这个类的信息;使用单例模式的话,类对象就只要软件启动的时候创建一次,软件停止的时候释放;

3、优点

1)保证类只有一个实列;

2)有一个指向该实例的全局节点;

4、缺点

暂没经验

5、线程安全的懒汉单例模式
Singleton.h:
cpp 复制代码
#ifndef SINGLETON_H_
#define SINGLETON_H_

#include <iostream>
#include <string>
#include <mutex>

// 配置类 
class SingletonConfig {
 public:
    static SingletonConfig* GetInstance() {
        if (instance_ == nullptr) {
            // 加锁保证多个线程并发调用getInstance()时只会创建一个实例
            m_mutex_.lock();
            if (instance_ == nullptr) {
                instance_ = new SingletonConfig();
            }
            m_mutex_.unlock();
        }
        return instance_;
    }
 private:
    SingletonConfig() {}
    static SingletonConfig* instance_;
    static std::mutex m_mutex_;
};

// 管理类
class SingletonManager {
 public:
    static SingletonManager* GetInstance() {
        if (instance_ == nullptr) {
            // 加锁保证多个线程并发调用getInstance()时只会创建一个实例
            m_mutex_.lock();
            if (instance_ == nullptr) {
                instance_ = new SingletonManager();
            }
            m_mutex_.unlock();
        }
        return instance_;
    }
 private:
    SingletonManager() {}
    static SingletonManager* instance_;
    static std::mutex m_mutex_;
};

#endif  // SINGLETON_H_
Singleton.cpp:
cpp 复制代码
#include "Singleton.h"

// 静态变量instance初始化不要放在头文件中, 如果多个文件包含singleton.h会出现重复定义问题
SingletonConfig* SingletonConfig::instance_ = nullptr;
std::mutex SingletonConfig::m_mutex_;

SingletonManager* SingletonManager::instance_ = nullptr;
std::mutex SingletonManager::m_mutex_;
main.cpp
cpp 复制代码
#include <iostream>
#include "Singleton.h"

int main() {
    SingletonConfig *s1 = SingletonConfig::GetInstance();
    SingletonConfig *s2 = SingletonConfig::GetInstance();

    std::cout << "s1地址: " << s1 << std::endl;
    std::cout << "s2地址: " << s2 << std::endl;

    SingletonManager *s3 = SingletonManager::GetInstance();
    SingletonManager *s4 = SingletonManager::GetInstance();

    std::cout << "s3地址: " << s3 << std::endl;
    std::cout << "s4地址: " << s4 << std::endl;

    return 0;
}
编译运行
cpp 复制代码
$ g++ -g main.cpp Singleton.cpp -std=c++11 -o singleton
$ ./singleton 
s1地址: 0x5594b0f12e70
s2地址: 0x5594b0f12e70
s3地址: 0x5594b0f132a0
s4地址: 0x5594b0f132a0

此单例模式代码是比较常规的,使用和理解起来也方便,但是如果有多个类需要使用单例模式的话,那么每个类都需要定义一份单例类代码,这种情况下,进一步优化的话,可以定义单例类模板;

6、升级版:线程安全的懒汉单例类模板
Config.h
cpp 复制代码
#ifndef CONFIG_H
#define CONFIG_H

#include "iostream"
#include "Singleton.h"

struct stuInfo{
    int index;
    std::string name;
    int age;
};

class Config
{
public:
    Config();
    ~Config();
    int getIndex() { return m_stuInfo.index; }
    std::string getName() { return m_stuInfo.name; }
    int getAge() { return m_stuInfo.age; }

    void init();
    void loadConfig();
    void syncConfig();

private:
    stuInfo m_stuInfo;
};

#define SingletonStuInfo    Singleton<Config>::GetInstance()

#endif // CONFIG_H
Singleton.h
cpp 复制代码
#ifndef SINGLETON_H_
#define SINGLETON_H_

#include <iostream>
#include <string>
#include <mutex>

template <class T>
class Singleton {
 public:
    static T* GetInstance();

 private:
    Singleton() {}
    static T* m_instance;
    static std::mutex m_mutex;
};

template<class T>
T* Singleton<T>::m_instance = nullptr;
template<class T>
std::mutex Singleton<T>::m_mutex;

template <class T>
T* Singleton<T>::GetInstance() {
    if (m_instance == nullptr) {
        // 加锁保证多个线程并发调用getInstance()时只会创建一个实例
        m_mutex.lock();
        if (m_instance == nullptr) {
            m_instance = new T();
        }
        m_mutex.unlock();
    }
    return m_instance;
}

#endif  // SINGLETON_H_
Config.cpp
cpp 复制代码
#include "Config.h"

Config::Config()
{

}

Config::~Config()
{

}

void Config::init()
{
    m_stuInfo.index = 0;
    m_stuInfo.name = "zhangsan";
    m_stuInfo.age = 20;
}

void Config::loadConfig()
{

}

void Config::syncConfig()
{

}
main.cpp
cpp 复制代码
#include <iostream>
#include "Singleton.h"
#include "Config.h"

int main() {
    SingletonStuInfo->init();

    std::cout << "index: " << SingletonStuInfo->getIndex() << std::endl;
    std::cout << "name: " << SingletonStuInfo->getName() << std::endl;
    std::cout << "age: " << SingletonStuInfo->getAge() << std::endl;

    return 0;
}
编译运行
bash 复制代码
$ g++ -g main.cpp Singleton.cpp Config.cpp -std=c++11 -o singleton
$ ./singleton 
index: 0
name: zhangsan
age: 20
相关推荐
charlie1145141914 分钟前
通用GUI编程技术——图形渲染实战(三十三)——Direct2D与Win32/GDI互操作:渐进迁移实战
c++·图形渲染·gui·win32
文祐9 分钟前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
回忆2012初秋1 小时前
工厂方法模式完整实现:GPS转换
设计模式·工厂方法模式
墨尘笔尖2 小时前
最大最小值降采样算法的优化
c++·算法
2301_771717213 小时前
DLC(Double-check Locking)与volatile的详解
单例模式
YIN_尹3 小时前
【Linux系统编程】进程地址空间
linux·c++
EverestVIP3 小时前
C++中空类通常大小为1的原理
c++
网域小星球4 小时前
C++ 从 0 入门(六)|C++ 面试必知:运算符重载、异常处理、动态内存进阶(终极补充)
开发语言·c++·面试
胡志辉的博客4 小时前
多智能体协作,不是多开几个 Agent:从中介者模式看 OpenClaw 和 Hermes Agent
人工智能·设计模式·ai·agent·中介者模式·openclaw·herman
晚会者荣4 小时前
红黑树的插入(有图)
c++