C++学习Day04之单例模式

目录

  • 一、程序及输出
    • [1.1 饿汉式实例](#1.1 饿汉式实例)
    • [1.2 饿汉式单例](#1.2 饿汉式单例)
    • [1.3 懒汉式单例](#1.3 懒汉式单例)
    • [1.4 线程安全的懒汉式单例](#1.4 线程安全的懒汉式单例)
  • 二、分析与总结

一、程序及输出

1.1 饿汉式实例

c 复制代码
#include<iostream>
using namespace std;
#include <string>
class Printer
{
public:
	static Printer * getInstance()//获取实例方法
	{
		return printer;
	}

	void printText(string text)
	{
		m_Count++;
		cout << text << endl;
	}

	int m_Count;

private:
	Printer() //单例构造函数
	{ 
		m_Count = 0;
		cout << "打印机构造调用" << endl; 
	};

	Printer(const Printer & p){};//拷贝构造函数

	static Printer * printer;//单例实体
	
};

Printer * Printer::printer = new Printer();//类外初始化

void test01()
{
	Printer * p1 = Printer::getInstance();
	p1->printText("入职证明");
	p1->printText("离职证明");
	p1->printText("加薪申请");
	p1->printText("旅游申请");

	cout << "打印机使用次数: " << p1->m_Count << endl;

	Printer * p2 = Printer::getInstance();
	p2->printText("调休申请");

	cout << "打印机使用次数: " << p1->m_Count << endl;

}

int main(){
  cout << "mian函数调用" << endl;    //单例模式编译阶段就分配内存,所以单例模式的构造函数会先打印。
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

输出:

可以看到,单例的创建是在全局作用域中进行初始化,编译阶段就分配内存,所以单例模式的构造函数会先打印

1.2 饿汉式单例

饿汉式单例模式

在程序启动时即创建单例实例。这样可以确保在多线程环境下也不会出现多个实例被创建的情况。

实现方式是将构造函数私有化,然后在类的内部定义一个静态成员变量来保存单例实例,并提供一个静态成员函数用于获取该实例。

c 复制代码
class Singleton {
public:
    static Singleton * getInstance() {
        return instance;
    }

private:
    Singleton() {}

    static Singleton * instance;
};

Singleton * Singleton::instance = new Singleton();

跟我们最开始的实例框架结构一致

1.3 懒汉式单例

懒汉式单例模式

在需要时才创建单例实例。这种方式可能需要考虑多线程环境下的线程安全性。

实现方式是将构造函数私有化,然后在类的内部定义一个静态成员变量指针,并在静态成员函数中进行实例化。

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

private:
    Singleton() {}

    static Singleton * instance;
};

Singleton * Singleton::instance = nullptr;

1.4 线程安全的懒汉式单例

线程安全的懒汉式单例模式

在懒汉式单例模式的基础上,通过加锁等手段来确保在多线程环境下也只有一个实例被创建。

可以使用互斥锁(mutex)或双重检查锁定(double-checked locking)等方式来实现线程安全的单例模式。

c 复制代码
class Singleton {
public:
    static Singleton * getInstance() {
        if (instance == nullptr) {
            std::lock_guard<std::mutex> lock(mutex);
            if (instance == nullptr) {
                instance = new Singleton();
            }
        }
        return instance;
    }

private:
    Singleton() {}

    static Singleton * instance;
    static std::mutex mutex;
};

Singleton * Singleton::instance = nullptr;
std::mutex Singleton::mutex;

二、分析与总结

单例设计模式是一种创建型设计模式,用于确保类只有一个实例,并提供全局访问点以访问该实例

分为饿汉式懒汉式

在具体使用过程中注意线程安全、实例化过程等细节

相关推荐
端平入洛1 天前
delete又未完全delete
c++
端平入洛2 天前
auto有时不auto
c++
西岸行者3 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马3 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
悠哉悠哉愿意3 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
超级大福宝3 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
别催小唐敲代码3 天前
嵌入式学习路线
学习
weiabc3 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法