设计模式学习

单例模式

单例模式是指在内存中只会创建且仅创建一次对象的设计模式。在程序中多次使用同一个对象且作用相同时,为了防止频繁地创建对象使得内存飙升,单例模式可以让程序仅在内存中创建一个对象,让所有需要调用的地方都共享这一单例对象。

饿汉模式

#include <iostream>
#include <vector>

class Singleton {
private:
    Singleton(){}
    Singleton(const Singleton& temp) = delete;
    Singleton& operator=(const Singleton & temp) = delete;
    static Singleton* instance;
public:
    static Singleton* getInstance()
    {
        return instance;
    }

    void mPrintf()
    {
        std::cout << "my name is Singleton";
    }
};

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


int main()
{
    Singleton *instance1 = Singleton::getInstance();
    
    instance1->mPrintf();
}

懒汉模式

#include <iostream>
#include <mutex>

class Singleton {
private:
    Singleton(){}
    Singleton(const Singleton& temp) = delete;
    Singleton& operator=(const Singleton& temp) = delete;
    static std::shared_ptr<Singleton> instance;
    static std::mutex mu;
public:
    static std::shared_ptr<Singleton> getInstance()
    {
        if (instance == nullptr) {
            std::unique_lock<std::mutex> ul(mu);
            if (instance == nullptr) {
                std::shared_ptr<Singleton> temp;
                instance = temp;
            }
            ul.unlock();
        }
        return instance;
    }

    void mPrintf()
    {
        std::cout << "my name is Singleton";
    }
};

std::shared_ptr<Singleton> Singleton::instance = nullptr;
std::mutex Singleton::mu;

int main()
{
    std::shared_ptr<Singleton> instance = Singleton::getInstance();
    instance->mPrintf();
}

工厂模式

简单工厂

1.简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类可以被外界直接调用,创建所需的产品对象。

2.提供一个抽象产品类

简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。

3.提供一个具体产品类

简单工厂模式所创建的具体实例对象

#include <iostream>
#include <vector>

class Base{
protected:
    int val1;
    int val2;
public:
    Base(int _val1, int _val2):val1(_val1),val2(_val2){}
    virtual int selfMeans() = 0;
};

class addMeans: public Base{
public:
    addMeans(int _val1,int _val2):Base(_val1, _val2){}

    virtual int selfMeans()
    {
        return val1 + val2;
    }
};

class Factory {
public:
    Base* createMeans(char ch, int val1, int val2)
    {
        switch (ch) {

            case '+':
                return new addMeans(val1, val2);
        }
        std::cout << "no value means";
        return nullptr;
    }
};
//add other Means
int main()
{
    Factory factory;
    Base* means = factory.createMeans ('+', 1, 2);
    std::cout << means->selfMeans() << std::endl;
    return 0;
}

//补充工厂模式

观察者模式

抓老鼠的小游戏

#include <iostream>
#include <list>

class Subject;

class Observer {
private:
    std::string name;
    Subject* sub;
public:
    Observer(std::string str, Subject *_sub):name(str),sub(_sub){}
    virtual void update() = 0;
};

class Cat : public Observer {
public:
    Cat(std::string str, Subject* _sub):Observer(str, _sub){}
    virtual void update()
    {
        std::cout << "猫咪:收到命令,我去抓老鼠" << std::endl;
    }
};

class Dog : public Observer {
public:
    Dog(std::string str, Subject* _sub) :Observer(str, _sub) {}
    virtual void update()
    {
        std::cout << "大黄:收到命令,我去抓老鼠" << std::endl;
    }
};

class Subject {
protected:
    std::list<Observer*> res;
public:
    std::string action;
    virtual void addObserver(Observer* temp) = 0;
    virtual void removeObserver(Observer* temp) = 0;
    virtual void notify() = 0;
};

class Master: public Subject {
public:
    virtual void addObserver(Observer* temp)
    {
        res.push_back(temp);
    }
    virtual void removeObserver(Observer* temp)
    {
        std::list<Observer*>::iterator it = res.begin();
        while (it != res.end()) {
            if ((*it) == temp) {
                res.erase(it);
                return;
            }
            it++;
        }
    }
    virtual void notify()
    {
        std::list<Observer*>::iterator it = res.begin();
        while (it != res.end()) {
            (*it)->update();
            it++;
        }
    }
};

int main()
{
    Master *master = new Master;
    Cat* cat = new Cat("mimi", master);
    Dog* dog = new Dog("大黄", master);
    master->addObserver(cat);
    master->addObserver(dog);

    master->action = "123";
    master->notify();
    master->action = "456";
    master->notify();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值