单例模式--饿汉/懒汉模式--C++

1.单例模式

简介 – 独一无二,全局唯一

单例模式是设计模式中最简单的形式之一。这一模式的目的是使得类的一个对象成为系统中的唯一实例。要实现这一点,可以从客户端对其进行实例化开始。因此需要用一种只允许生成对象类的唯一实例的机制,“阻止”所有想要生成对象的访问。使用工厂方法来限制实例化过程。这个方法应该是静态方法(类方法),因为让类的实例去生成另一个唯一实例毫无意义。单例模式_百度百科 (baidu.com)

使用场景

适用于类只能实例化一个对象的时候,节省内存;需要频繁实例化然后销毁的对象;创建对象耗时过多或资源耗时过多,但又经常用到的对象;

1.windows的任务管理器、回收站

2.网站的计数器;

3.应用程序的日志应用;

4.多线程的线程池;

5.操作系统的文件系统;

实现方式

1.懒汉式 – 使用的时候才实例化

构造函数私有化,对外提供一个接口;

在第一次调用的时候new

1).线程不安全的懒汉式单例模式

// 线程不安全的单例模式
// 时间:2023-10-26
// 作者:@conceal
#include <iostream>

using namespace std;
// 创建一个校长类
class president
{
public:
    // 提供一个外部访问的接口
    static president* get_instance()
    {
        if (instance == NULL)
        {
            instance = new president();
        }
        return instance;
    }
    // 设置名字
    void set_name(string name)
    {
        this->name = name;
    }
    // 设置年龄
    void set_age(int age)
    {
        this->age = age;
    }
    // 打印信息
    void print_info()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
    }

private:
    president(){};
    static president* instance;
    // 防止拷贝构造和赋值
    president(const president&);
    president& operator=(const president&);
    // 属性,名字,年龄
    string name;
    int age;
};
// 初始化静态变量
president* president::instance = NULL;

int main()
{
    president* p1 = president::get_instance();
    p1->set_name("conceal");
    p1->set_age(18);
    p1->print_info();
    president* p2 = president::get_instance();
    p2->set_name("conceal2");
    p2->print_info();
    p1->print_info();
    return 0;
}

输出

name: conceal
age: 18
name: conceal2
age: 18
name: conceal2
age: 18

2).线程安全的懒汉式单例模式:在多线程的情况下,对于单例的初始化可能会出现问题,导致线程不安全,现在通过加锁的方式实现初始化时的线程安全。

// 线程安全的单例模式
// 时间:2023-10-26
// 作者:@conceal
#include <iostream>
#include <mutex>
#include <thread>
using namespace std;
// 创建一个校长类
class president
{
public:
    // 提供一个外部访问的接口
    static president* get_instance()
    {
        if(NULL == instance){
            // 加锁
            m_mutex.lock();
            if (instance == NULL)
            {
                instance = new president();
            }
            // 解锁
            m_mutex.unlock();
        }
        
        return instance;
    }
    // 设置名字
    void set_name(string name)
    {
        this->name = name;
    }
    // 设置年龄
    void set_age(int age)
    {
        this->age = age;
    }
    // 打印信息
    void print_info()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
    }

private:
    president(){};
    static president* instance;
    // 防止拷贝构造和赋值
    president(const president&);
    president& operator=(const president&);
    // 属性,名字,年龄,加锁
    static mutex m_mutex;
    string name;
    int age;
};
// 初始化静态变量
president* president::instance = NULL;
mutex president::m_mutex;


void func(string name, int age);
void func(string name, int age){
    president* p = president::get_instance();
    p->set_name(name);
    p->set_age(age);
    p->print_info();
}

int main()
{
    std::thread t1(func, "conceal", 18);
    std::thread t2(func, "conceal2", 19);
    t1.join();
    t2.join();
    
    return 0;
}

运行命令

g++ -std=c++11 -o demo.o demo.cpp -pthread
2.饿汉式 – 程序启动就创建一个唯一的实例对象

初始化的时候就new

// 单例模式的饿汉式
// 时间:2023-10-26
// 作者:@conceal
#include <iostream>

using namespace std;

// 创建一个校长类
class president
{
public:

    // 提供一个外部访问的接口
    static president* get_instance()
    {
        return instance;
    }
    // 设置名字
    void set_name(string name)
    {
        this->name = name;
    }
    // 设置年龄
    void set_age(int age)
    {
        this->age = age;
    }
    // 打印信息
    void print_info()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
    }
private:
    president(){};
    static president* instance;
    // 防止拷贝构造和赋值
    president(const president&);
    president& operator=(const president&);
    // 属性,名字,年龄
    string name;
    int age;
};
// 初始化静态变量
president* president::instance = new president();

int main()
{
    president* p1 = president::get_instance();
    p1->set_name("conceal");
    p1->set_age(18);
    p1->print_info();
    president* p2 = president::get_instance();
    p2->set_name("conceal2");
    p2->print_info();
    p1->print_info();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值