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
相关推荐
炸膛坦客1 小时前
单片机/C/C++八股:(二十四)编译文件( .bin 和 .hex ,包括 .elf 和 .axf )
c语言·c++·单片机
CaffeinePro1 小时前
四⼤极简架构原则KISS/DRY/YAGNI/LOD
设计模式·架构
cpp_25012 小时前
P10098 [ROIR 2023] 地铁建设 (Day 2)
数据结构·c++·算法·贪心·二分答案·洛谷题解
霸道流氓气质3 小时前
SpringBoot中设计模式组合使用示例-策略、模板、观察者、门面、工厂、单例。
spring boot·后端·设计模式
hehelm3 小时前
IO 多路复用 — Reactor
linux·服务器·网络·数据库·c++
灵晔君4 小时前
【C++】二叉搜索树
开发语言·c++
ttod_qzstudio4 小时前
【软考设计模式】观察者模式:一对多依赖的自动通知与订阅解耦精讲
观察者模式·设计模式
鱼子星_4 小时前
【C++】内存管理:内存分布,new/delete的使用及细节处理,new/delete的底层,定位new表达式
开发语言·c++·笔记
鱼子星_4 小时前
【C++】string(上):string的基本使用
c++·笔记·字符串
神仙别闹5 小时前
编写基于C++ Huffman 算法的无损压缩程序和解压程序
java·c++·算法