【单例模式(饿汉式和懒汉式)】

一、概念

单例模式就是一个类只能有一个实例,并且提供一个访问它的全局访问点。

通常通过私有化构造函数来实现只能通过类的内部创建实例。

二、饿汉式

饿汉式是单例模式中的一种,其特点为:在定义是就立即创建类的实例(真的饿了),但饿汉式是线程安全的,其核心代码如下:

cpp 复制代码
class Singleton{
private:
    Singleton(){}
    static Singleton* m_instance;
public:
    static Singleton* getInstance(){
        return m_instance;
    }
};
Singleton* Singleton::m_instance = new Singleton;

完整实例:

cpp 复制代码
#include <iostream>

using namespace std;

class Singleton {
    static Singleton* singleton;
    Singleton()
    {
        cout << "这是一个无参构造" << endl;
    }

    ~Singleton()
    {
        cout << "这是析构" << endl;
    }
public:
    static Singleton* getinstence()
    {
        return singleton;
    }

    void show_info()
    {
        cout << this << endl;
    }

    //将编译器自动提供的拷贝构造与等号运算符重载移除掉
    Singleton(const Singleton& other) = delete;
    void operator=(const Singleton& other) = delete;
};

Singleton* Singleton::singleton = new Singleton;

int main()
{
    Singleton* s1 = Singleton::getinstence();
    s1->show_info();
    Singleton* s2 = Singleton::getinstence();
    s2->show_info();
    Singleton* s3 = Singleton::getinstence();
    s3->show_info();

    return 0;
}

运行结果:

三、懒汉式

懒汉式也是单例模式的一种,其特点为:在需要用到的时候才会创建实例,具有懒加载的功能,其是线程不安全的,代码如下:

cpp 复制代码
class Singleton{
private:
    Singleton(){}
    static Singleton* m_instance;
public:
    static Singleton* getInstance(){
        if(m_instance == nullptr){
            m_instance = new Singleton;
        }
        return m_instance;
    }
};
Singleton* Singleton::m_instance = nullptr;

完整示例如下:

cpp 复制代码
#include <iostream>

using namespace std;

class Singleton {
    static Singleton* singleton;
    Singleton()
    {
        cout << "这是一个无参构造" << endl;
    }

    ~Singleton()
    {
        cout << "这是析构" << endl;
    }
public:
    static Singleton* getinstence()
    {
        if (singleton == nullptr) {
            singleton = new Singleton;
        }
        return singleton;
    }

    void show_info()
    {
        cout << this << endl;
    }

    //将编译器自动提供的拷贝构造与等号运算符重载移除掉
    Singleton(const Singleton& other) = delete;
    void operator=(const Singleton& other) = delete;
};

Singleton* Singleton::singleton = nullptr;

int main()
{
    Singleton* s1 = Singleton::getinstence();
    s1->show_info();
    Singleton* s2 = Singleton::getinstence();
    s2->show_info();
    Singleton* s3 = Singleton::getinstence();
    s3->show_info();

    return 0;
}

运行结果为:

相关推荐
biter down12 小时前
C++ 单例模式:饿汉与懒汉模式
开发语言·c++·单例模式
mingshili19 小时前
[架构设计] 依赖注入优于单例模式
单例模式·架构设计
一只大袋鼠19 小时前
并发编程(二十三):单例模式(二):静态/非静态方法:单例内存优化关键
java·单例模式·并发编程
一叶飘零_sweeeet19 小时前
volatile 关键字深度拆解:从内存屏障底层到单例模式的工业级架构设计
单例模式·volatile
一只大袋鼠20 小时前
并发编程(二十四):单例模式(三):构造方法私有:单例模式的 “第一道防线”
java·单例模式·并发编程
一只大袋鼠2 天前
并发编程(二十二):单例模式:从基础实现到 Spring Web 实战
java·spring·单例模式·并发编程
Real-Staok3 天前
(集合)C / C++ 设计模式综合
单例模式·设计模式·代理模式
冉佳驹3 天前
C++11 ——— 线程库与单例模式的原理、实现及线程安全设计
c++·单例模式·饿汉模式·懒汉模式·c++线程库·c++互斥锁·c++条件变量
Yupureki4 天前
《C++实战项目-高并发内存池》4.CentralCache构造
c语言·开发语言·c++·单例模式·github
阿珊和她的猫4 天前
单例模式:确保唯一性与全局访问的设计方案
单例模式·状态模式