设计模式简述(一)

定义:设计模式指的是在软件开发过程中,经过验证的,用于解决在特定环境下,重复出现的,特定问题的解决方案。创建型设计模式关注对象的创建过程,提供了更灵活、可扩展的对象创建机制。结构型设计模式用于解决对象之间组合成更大结构的问题,以便更好地管理它们之间的关系

前提:具体需求有稳定点也有变化点,希望将来能够修改少量代码就能适应需求的变化

基础:继承、封装、多态

模版方法:

定义:定义了一个算法的骨架,将算法中的某些步骤延迟到子类中实现

人话:算法子流程是固定的,但是子流程的执行顺序可能会发生变化,因此可以使用继承,在继承基类方法的同时实现一些函数,这些函数的子流程执行顺序是不同的。在程序执行的时候通过晚绑定实例化子类。子类要满足里氏替换原则,就是子类可以完美替代父类。demo program:

cpp 复制代码
#include <iostream>
using namespace std;

// 开闭
class ZooShow {
public:
    void Show() {
        // 如果子表演流程没有超时的话,进行一个中场游戏环节;如果超时,直接进入下一个子表演流程
        if (Show0())
            PlayGame();
        Show1();
        Show2();
        Show3();
    }
    
private:
    void PlayGame() {
        cout << "after Show0, then play game" << endl;
    }
    bool expired;
    // 对其他用户关闭,但是子类开放的
protected:
    virtual bool Show0() {
        cout << "show0" << endl;
        if (! expired) {
            return true;
        }
        return false;
    }
    virtual void Show2() {
        cout << "show2" << endl;
    }
    virtual void Show1() {

    }
    virtual void Show3() {

    }
};

// 框架
// 模板方法模式
class ZooShowEx10 : public ZooShow {
protected:
    virtual void Show0() {
        if (! expired) {
            return true;
        }
        return false;
    }
}

class ZooShowEx1 : public ZooShow {
protected:
    virtual bool Show0() {
        cout << "ZooShowEx1 show0" << endl;
        if (! expired) { // 里氏替换
            return true;
        }
        return false;
    }
    virtual void Show2(){
        cout << "show3" << endl;
    }
};

class ZooShowEx2 : public ZooShow {
protected:
    virtual void Show1(){
        cout << "show1" << endl;
    }
    virtual void Show2(){
        cout << "show3" << endl;
    }
};

class ZooShowEx3 : public ZooShow {
protected:
    virtual void Show1(){
        cout << "show1" << endl;
    }
    virtual void Show3(){
        cout << "show3" << endl;
    }
    virtual void Show4() {
        //
    }
};
/*
*/
int main () {
    ZooShow *zs = new ZooShowEx10; // 晚绑定还是早绑定
    // ZooShow *zs1 = new ZooShowEx1;
    // ZooShow *zs2 = new ZooShowEx2;
    zs->Show();
    return 0;
}

观察者模式:

定义:用于定义对象之间的一对多依赖关系,当一个对象的状态发生改变时,其所有依赖者(观察者)都会收到通知并自动更新。

人话:定义一个控制类,把所有实例化的对象全部存储到这个类的容器(也可以使用其他类型的数据结构)中,需要更新的时候遍历所有对象修改数据。优点是无需修改依赖和被依赖的对象,缺点是观察者过多影响性能。

代码实例:

cpp 复制代码
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
//
class IDisplay {
public:
    virtual void Show(float temperature) = 0;
    virtual ~IDisplay() {}
};

class DisplayA : public IDisplay {
public:
    virtual void Show(float temperature) {
        cout << "DisplayA Show" << endl;
    }
private:
    void jianyi();
};

class DisplayB : public IDisplay{
public:
    virtual void Show(float temperature) {
        cout << "DisplayB Show" << endl;
    }
};

class DisplayC : public IDisplay{
public:
    virtual void Show(float temperature) {
        cout << "DisplayC Show" << endl;
    }
};

class DisplayD : public IDisplay{
public:
    virtual void Show(float temperature) {
        cout << "DisplayC Show" << endl;
    }
};

class WeatherData {
};

// 应对稳定点,抽象
// 应对变化点,扩展(继承和组合)
class DataCenter {
public:
    void Attach(IDisplay * ob) {
        //
    }
    void Detach(IDisplay * ob) {
        //
    }
    void Notify() {
        float temper = CalcTemperature();
        for (auto &ob : obs) {
            ob->Show(temper);
        }
    }

// 接口隔离
private:
    WeatherData * GetWeatherData();

    float CalcTemperature() {
        WeatherData * data = GetWeatherData();
        // ...
        float temper/* = */;
        return temper;
    }
    std::list<IDisplay*> obs;
};

int main() {
    // 单例模式
    DataCenter *center = new DataCenter;
    // ... 某个模块
    IDisplay *da = new DisplayA();
    center->Attach(da);

    // ...
    IDisplay *db = new DisplayB();
    center->Attach(db);
    
    IDisplay *dc = new DisplayC();
    center->Attach(dc);

    center->Notify();
    
    //-----
    center->Detach(db);
    center->Notify();

    center->Notify();
    return 0;
}

策略模式:

定义:提供一系列可以重用的算法,使得程序在运行时可以方便的切换。

人话:固定的部分抽象成基类,子类继承基类并且重写基类的虚函数,程序调用的时候根据传入的实例化的类来执行函数。是一种比较好的去处if-else的方法。

例子:

cpp 复制代码
class Context {

};

// 稳定点:抽象去解决它
// 变化点:扩展(继承和组合)去解决它
class ProStategy {
public:
    virtual double CalcPro(const Context &ctx) = 0;
    virtual ~ProStategy(); 
};
// cpp
class VAC_Spring : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){
    }
};

class VAC_Spring_v2 : public VAC_Spring {
public:
    virtual double CalcPro(const Context &ctx){
        //....
    }
};

class VAC_worker : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};

// cpp
class VAC_QiXi : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};
class VAC_QiXi1  : public VAC_QiXi {
public:
    virtual double CalcPro(const Context &ctx){}
};
// cpp
class VAC_Wuyi : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};
// cpp
class VAC_GuoQing : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};
class VAC_GuoQing2 : public VAC_GuoQing {
public:
    virtual double CalcPro(const Context &ctx){}
};

class VAC_Shengdan : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};

// 设计原则:接口隔离原则
// 组合、继承
// 组合基类指针
// 两种方法:1. 采用具体接口选择算法  2. 依赖注入
class Promotion {
public:
    Promotion(ProStategy *sss = nullptr) : s(sss){}
    ~Promotion(){}
    void Choose(ProStategy *sss) {
        // 条件选择
        if (sss != nullptr) {
            s = sss;
        }
    }
    double CalcPromotion(const Context &ctx){
        if (s != nullptr) {
            return s->CalcPro(ctx);
        }
        return 0.0L;
    }
private:
    ProStategy *s;
};

int main () {
    Context ctx;
    ProStategy *s = new VAC_QiXi1();
    Promotion *p = new Promotion(s);
    p->Choose(new VAC_GuoQing2());
    p->CalcPromotion(ctx);
    return 0;
}

这是一条吃饭博客,由挨踢零声赞助。学C/C++就找挨踢零声,加入挨踢零声,面试不挨踢!

相关推荐
charlie1145141912 小时前
深入讨论C语言的可能抽象:部分对设计模式的思考
c语言·学习·设计模式·软件工程
茂茂在长安4 小时前
JAVA面试_进阶部分_23种设计模式总结
java·设计模式·面试
小王子10247 小时前
设计模式Python版 观察者模式(上)
python·观察者模式·设计模式
Duramentee7 小时前
C++ 设计模式 十九:观察者模式 (读书 现代c++设计模式)
c++·观察者模式·设计模式
工一木子9 小时前
【HeadFirst系列之HeadFirst设计模式】第14天之复合模式:设计模式的终极组合拳!
设计模式·复合模式
千里码!12 小时前
java23种设计模式-模板方法模式
java·设计模式·模板方法模式
攻城狮7号12 小时前
【第八节】C++设计模式(结构型模式)-Decorator(装饰器)模式
c++·设计模式·装饰器模式
北城望戈14 小时前
工作中遇到的设计模式整理
java·设计模式
LuckyLay14 小时前
Golang学习笔记_40——模版方法模式
笔记·学习·设计模式·golang·模板方法模式
Forget the Dream17 小时前
设计模式之责任链模式
java·c++·设计模式·责任链模式