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

devtools/2024/9/23 10:30:44/

一、概念

单例模式就是一个类只能有一个实例,并且提供一个访问它的全局访问点。
通常通过私有化构造函数来实现只能通过类的内部创建实例。

二、饿汉式

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

class Singleton{
private:Singleton(){}static Singleton* m_instance;
public:static Singleton* getInstance(){return m_instance;}
};
Singleton* Singleton::m_instance = new Singleton;

完整实例:

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

运行结果:
在这里插入图片描述

三、懒汉式

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

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;

完整示例如下:

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

运行结果为:
在这里插入图片描述


http://www.ppmy.cn/devtools/97833.html

相关文章

Redis7基础篇(七)

redis哨兵&#xff08;sentinel&#xff09; 目录 redis哨兵&#xff08;sentinel&#xff09; 是什么 能干吗 案例演示 架构 案例步骤 出现的问题 哨兵的运行流程和选举原理 哨兵的使用建议​编辑 是什么 在之前的复制中我们了解到 主机shutdown之后 从机就会一直等…

ubuntu 22.04下载安装及相关配置

一、ubuntu 22.04下载 1.1、官网下载 Get Ubuntu | Download | Ubuntu 官网下载速度比较慢&#xff0c;不是很推荐。 1.2、清华镜像网站下载 清华大学开源软件镜像站 | Tsinghua Open Source Mirror 该方式下载很快&#xff0c;推荐使用。下载方式如下&#xff1a; 1.2.…

基于嵌入式C++、SQLite、MQTT、Modbus和Web技术的工业物联网网关:从边缘计算到云端集成的全栈解决方案设计与实现

一、项目概述 1.1 项目目标与用途 随着工业4.0时代的到来&#xff0c;传统工业设备与现代信息技术的结合越来越紧密。物联网工业网关作为连接工业设备与云端平台的桥梁&#xff0c;在工业自动化、设备监控、远程运维等方面发挥着至关重要的作用。本项目旨在设计并实现一个能够…

机器人走路的问题

public class Test52 {//假设有N个位置&#xff0c;记为1-N&#xff0c;N大于或等于2//开始机器人在M位置上&#xff08;M为1-N中的一个&#xff09;//如果机器人来到1位置&#xff0c;那么下一步只能向右来到2位置//如果机器人来到N位置&#xff0c;那么下一步只能向左来到N-1…

Hadoop入门基础(三):Hadoop启动踩坑记录

一、机器ssh连接方式非默认22端口 报错&#xff1a; sbin/start-dfs.sh Starting namenodes on [doop253] doop253: ssh: connect to host doop253 port 22: Connection refused 解决方法&#xff1a; sudo vim /etc/ssh/ssh_config 添加如下内容&#xff08;注意替换自己服…

从零开始手写STL库:unordered_set

从零开始手写STL库–unordered_set的实现 Gihub链接&#xff1a;miniSTL 文章目录 从零开始手写STL库–unordered_set的实现一、unordered_set是什么二、unordered_set要包含什么函数总结 一、unordered_set是什么 在STL中&#xff0c;std::unordered_set 是一个无序关联容器…

【数据同步】SeaTunnel初体验,5000字深入浅出带你用上Oracle-CDC

Apache SeaTunnel 是啥&#xff1f;下一代高性能、分布式、海量数据集成框架。支持上百个数据源、传输速度快、准确率高&#xff0c;丰富易扩展的连接器和插件化的连接器设计&#xff0c;能够更轻松的运行复杂的集成。是一个分布式、高性能的数据集成平台&#xff0c;用于数据迁…

软件工程造价师习题练习 16

1.功能点分析方法是一种从&#xff08; &#xff09;视角来度量软件向用户提供的功能规模的方法 A. 需求分析 B. 用户 C. 开发 D. 测试 功能点分析方法是一种软件规模估算方法&#xff0c;其主要目的是从用户的角度来测量软件提供的功能规模。这意味着它关注的是用户所需的…