设计模式(持续更新)

单例模式:

懒汉:

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

工厂模式:

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

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

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

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

后面的慢慢补了。。。

相关推荐
Code Warrior4 分钟前
【每日算法】专题五_位运算
开发语言·c++
Small black human4 小时前
设计模式-应用分层
设计模式
OneQ6665 小时前
C++讲解---创建日期类
开发语言·c++·算法
Coding小公仔7 小时前
C++ bitset 模板类
开发语言·c++
菜鸟看点7 小时前
自定义Cereal XML输出容器节点
c++·qt
悲伤小伞8 小时前
linux_git的使用
linux·c语言·c++·git
ysa0510309 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
小小小小王王王12 小时前
求猪肉价格最大值
数据结构·c++·算法
岁忧12 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
码农秋12 小时前
设计模式系列(10):结构型模式 - 桥接模式(Bridge)
设计模式·桥接模式