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

一、概念

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

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

二、饿汉式

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

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;
}

运行结果为:

相关推荐
zh_xuan7 小时前
c++ 单例模式
开发语言·c++·单例模式
西北大程序猿18 小时前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
找不到、了3 天前
实现单例模式的常见方式
java·开发语言·单例模式
不愧是你呀5 天前
C++中单例模式详解
网络·c++·windows·单例模式
变身缎带6 天前
Unity中的MonoSingleton<T>与Singleton<T>
unity·单例模式·c#·游戏引擎
小吴同学·7 天前
OPC Client第6讲(wxwidgets):Logger.h日志记录文件(单例模式);登录后的主界面
开发语言·c++·单例模式·wxwidgets
勤奋的知更鸟8 天前
Java 单例模式详解
java·开发语言·单例模式
ailinghao8 天前
单例模式的类和静态方法的类的区别和使用场景
flutter·单例模式
XiaoLeisj8 天前
【JUC】深入解析 JUC 并发编程:单例模式、懒汉模式、饿汉模式、及懒汉模式线程安全问题解析和使用 volatile 解决内存可见性问题与指令重排序问题
javascript·安全·单例模式
charlie1145141919 天前
从C++编程入手设计模式1——单例模式
c++·单例模式·设计模式·架构·线程安全