设计模式(持续更新)

单例模式:

懒汉:

cpp 复制代码
public:
    static Instance& getInstance()
    {
        static instance=new Instance();
        return instance;
    }
前提:Instance(const Instance&)=delete;
Instance(Instance&&)=delete;
Instance& operator=(const Instance&) = delete;

线程安全的懒汉模式

cpp 复制代码
线程安全的:
class Instance{
private:
static Instance* instance;
mutex mut;
public:
static Instance* getInstance();
}
Instance *Instance::instance=nullptr;
Instance::mut;
Instance* Instance::getInstance()
{
    if(instance==nullptr)
    {
        lock_guard<mutex>lock(mut);
        if(instance==nullptr)
        {
            instance=new Instance();
        }
    
    }
    return instance;
}

饿汉模式:

cpp 复制代码
class Instance{
private:
static Instance instance=new Instance();
mutex mut;
public:
static Instance& getInstance();
}
Instance& Instance::getInstance()
{
    return instance;
}

工厂模式:

比较简单但是代码很多,直接放图:

简单工厂:就是创业公司,一个人干多个活

工厂方法:外包,丢给你一个专门的业务

抽象工厂:大厂的核心部门,一个具体工厂干很多核心业务

后面的慢慢补了。。。

相关推荐
郝学胜-神的一滴18 小时前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光19 小时前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
青少儿编程课堂1 天前
蓝桥杯真题解析:手链配色计数与波利亚定理(全国青少年信息素养大赛与电子学会软件编程考级)
c++·python·算法·bfs·信息学竞赛
艾莉丝努力练剑1 天前
【AI大模型接入SDK】项目总结
java·开发语言·前端·c++·人工智能·学习·架构
旖旎夜光1 天前
面试题 01.02: 判定是否互为字符重排 (哈希表) —— 题解
c++·笔记·学习·算法·哈希表
白小飞不会飞1 天前
【AUTOSAR】CP SOME/IP-TP
c++·嵌入式硬件·网络协议·车载系统·autosar
ShineWinsu2 天前
对于Qt:信号量(QSemaphore)的解析
c++·qt·面试·笔试·客户端·线程安全·信号量
星恒随风2 天前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
白小飞不会飞2 天前
【AUTOSAR】CP System Template(9):一份完整配置与排错清单
c++·嵌入式硬件·车载系统·autosar
白小飞不会飞2 天前
【AUTOSAR】CP System Template(8):抽取与扁平化
c++·嵌入式硬件·车载系统·autosar