设计模式小结

1 前言

设计模式是指在软件开发中,经过验证的,用于解决在特定环境下,重复出现的,特定问题的解决
方案;

我们从模式设计出发,迭代出设计模式

怎么学习设计模式?

  1. 找稳定点和变化点,把变化点隔离出来
  2. 先满足设计原则,慢慢迭代出设计模式

2. 设计原则

2.1 依赖倒置

  • 高层模块不应该依赖低层模块,两者都应该依赖抽象;
  • 抽象不应该依赖具体实现,具体实现应该依赖于抽象;

2.2 开放封闭

  • 一个类应该对扩展开放,对修改关闭;

2.3 面向接口

  • 不将变量类型声明为某个特定的具体类,而是声明为某个接口;
  • 客户程序无需获知对象的具体类型,只需要知道对象所具有的接口;
  • 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案;

2.4 封装变化点

  • 将稳定点和变化点分离,扩展修改变化点;让稳定点和变化点的实现层次分离;

2.5 单一职责

  • 一个类应该仅有一个引起它变化的原因;

2.6 里氏替换

  • 子类型必须能够替换掉它的父类型;主要出现在子类覆盖父类实现,原来使用父类型的程序可能出现错误;覆盖了父类方法却没有实现父类方法的职责;

2.7 接口隔离

  • 不应该强迫客户依赖于它们不用的方法;
  • 一般用于处理一个类拥有比较多的接口,而这些接口涉及到很多职责;

2.8 组合优于继承

  • 继承耦合度高,组合耦合度低;

3. 创建型设计模式

3.1 模板方法

定义

定义一个操作中的算法的骨架 ,而将一些步骤延迟到子类中。 Template Method使得子类可以不
改变一个算法的结构即可重定义该算法的某些特定步骤。 ——《 设计模式》 GoF

背景

某个品牌动物园,有一套固定的表演流程,但是其中有若干个表演子流程可创新替换,以尝试迭代
更新表演流程;

要点 

  • 最常用的设计模式,子类可以复写父类子流程,使父类的骨架流程丰富;
  • 控制流程的典型应用;
  • 父类 protected 保护子类需要复写的子流程;这样子类的子流程只能父类来调用;

本质

 通过固定算法骨架来约束子类的行为;

 代码样例

#include <iostream>
using namespace std;

class ZooShow {
public:
    // 固定流程封装到这里
    void Show() {
        Show0();
        Show1();
        Show2();
        Show3();
    }
protected:
    // 子流程 使用protected保护起来 不被客户调用 但允许子类扩展
    virtual void Show0(){
        cout << "show0" << endl;
    }
    virtual void Show2(){
        cout << "show2" << endl;
    }
    virtual void Show1() {

    }
    virtual void Show3() {

    }
};
class ZooShowEx : public ZooShow {
protected:
    virtual void Show1(){
        cout << "Ex show1" << endl;
    }
    virtual void Show3(){
        cout << "Ex show3" << endl;
    }
    virtual void Show4() {
        //
    }
};

class ZooShowEx1 : public ZooShow {
protected:
    virtual void Show0(){
        cout << "Ex1 show1" << endl;
    }
    virtual void Show2(){
        cout << "Ex1 show3" << endl;
    }
};

class ZooShowEx2 : public ZooShow {
protected:
    virtual void Show1(){
        cout << "Ex2 show1" << endl;
    }
    virtual void Show2(){
        cout << "Ex2 show3" << endl;
    }
};
/*
*/
int main () {
    ZooShow *zs = new ZooShowEx;
    // ZooShow *zs1 = new ZooShowEx1;
    // ZooShow *zs2 = new ZooShowEx2;
    zs->Show();
    return 0;
}

3.2 观察者模式

定义

定义对象间的一种一对多(变化)的依赖关系,以便当一个对象(Subject)的状态发生改变时,所
有依赖于它的对象都得到通知并自动更新。 ——《 设计模式》 GoF

背景

气象站发布气象资料给数据中心,数据中心经过处理,将气象信息更新到两个不同的显示终端(A
和B;

要点

  • 观察者模式使得我们可以独立地改变目标与观察者,从而使二者之间的关系松耦合;
  • 观察者自己决定是否订阅通知,目标对象并不关注谁订阅了;
  • 观察者不要依赖通知顺序,目标对象也不知道通知顺序;
  • 常用在基于事件的ui框架中,也是 MVC 的组成部分;
  • 常用在分布式系统中、actor框架中;

本质

触发联动

结构图

 代码样例

#include <vector>

class IDisplay { //观察者的基类,展示
public:
    virtual void Show(float temperature) = 0; // update
    virtual ~IDisplay() {}
};

class DisplayA : public IDisplay {
public:
    virtual void Show(float temperature);
};

class DisplayB : public IDisplay{
public:
    virtual void Show(float temperature);
};

class Subject
{
public:
	virtual ~Subject() {};
	virtual void Attach(Observer*) = 0;//注册观察者
	virtual void Detach(Observer*) = 0;//移除观察者
	virtual void Notify() = 0; //发送通知
};

class DataCenter: public Subject{
public:
    void Attach(IDisplay * ob){ //...};  
    void Detach(IDisplay * ob){//...}; 
    void Notify() { 
        float temper = CalcTemperature(); //计算数据
        for (auto iter = obs.begin(); iter != obs.end(); iter++) { //一一通知给观察者
            (*iter)->Show(temper);
        }
    }
    void changeState(){
        CalcTemperature();
    }

private:
    virtual float CalcTemperature() {
        float temper = GetWeatherData();//从数据中心获取数据,并计算
        return temper;
    }
    std::vector<IDisplay*> obs;
};

class ConcDataCenter ::public DataCenter {
    public set
}

int main() {
    DataCenter *center = new DataCenter;
    IDisplay *da = new DisplayA();
    IDisplay *db = new DisplayB();
    center->Attach(da);
    center->Attach(db);

    center->changeState();
    center->Notify();
    
    //-----
    center->Detach(db);
    center->changeState();
    center->Notify();
    return 0;
}

3.3 策略模式

定义

定义一系列算法,把它们一个个封装起来,并且使它们可互相替换。该模式使得算法可独立于使用
它的客户程序而变化。 ——《设计模式》 GoF

背景

某商场节假日有固定促销活动,为了加大促销力度,现提升国庆节促销活动规格;

要点

  • 策略模式提供了一系列可重用的算法,从而可以使得类型在运⾏时方便地根据需要在各个算法之间进行切换;

  • 策略模式消除了条件判断语句;也就是在解耦合;

本质

分离算法,选择实现;

结构图

代码样例

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){}
};
// 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_Shengdan : public ProStategy {
public:
    virtual double CalcPro(const Context &ctx){}
};

// 稳定的  变化的
class Promotion {
public:
    Promotion(ProStategy *sss) : s(sss){}
    ~Promotion(){}
    double CalcPromotion(const Context &ctx){
        return s->CalcPro(ctx);
    }
private:
    ProStategy *s;
};

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

3.4 单例模式

定义

保证一个类仅有一个实例,并提供一个该实例的全局访问点。 ——《设计模式》GoF

要点

  • 全局只有一个实例:static 特性,同时禁止用户自己声明并定义实例(把构造函数设为 private)
  • 线程安全
  • 禁止赋值和拷贝
  • 用户通过接口获取实例:使用 static 类成员函数

结构图

代码示例一

懒汉式,直到使用时才会实例化对象,如果不被调用就不会占用内存。

懒汉式有两个问题:

1. 线程不安全。解决方式:加锁

2. 内存泄漏,只有new,没有delete。解决方式:1)使用智能指针;2)调用atexit来注册delete函数,当程序结束时,delete实例化的对象

class Singleton {
public:
  static Singleton * GetInstance() {
    if (_instance == nullptr) {
      _instance = new Singleton();
   }
    return _instance;
 }
private:
  Singleton() {} //构造
  ~Singleton() {}
  Singleton(const Singleton&)=delete;
  Singleton& operator=(const Singleton&)=delete;
  static Singleton * _instance;
};
Singleton* Singleton::_instance = nullptr;//静态成员需要初始化

代码示例二

此时,还存在了一个问题,多线程环境下 cpu reorder操作

#include <mutex>
class Singleton { // 懒汉模式 lazy load
public:
  static Singleton * GetInstance() {
    // std::lock_guard<std::mutex> lock(_mutex); // 如果在这行代码加锁,所有获取实例的代码都要互斥,影响性能
    if (_instance == nullptr) {
      std::lock_guard<std::mutex> lock(_mutex); // 在这行代码加锁,只有_instance 未实例化的时候影响性能
      if (_instance == nullptr) {
        _instance = new Singleton();
        // 1. 分配内存
        // 2. 调用构造函数
        // 3. 返回指针
        // 多线程环境下 cpu reorder操作
        atexit(Destructor);  // 注册了析构函数
     }
   }
    return _instance;
 }
private:
  static void Destructor() {
    if (nullptr != _instance) {
      delete _instance;
      _instance = nullptr;
   }
 }
  Singleton(){} //构造
  Singleton(const Singleton&)=delete;
  Singleton& operator=(const Singleton&)=delete;
  static Singleton * _instance;
  static std::mutex _mutex;
};
Singleton* Singleton::_instance = nullptr;//静态成员需要初始化
std::mutex Singleton::_mutex; //互斥锁初始

代码示例三

基于Magic Static方法解决了线程安全、内存泄漏、以及cpu reorder的问题。

  • 利⽤静态局部变量特性,延迟加载; 
  • 利⽤静态局部变量特性,系统⾃动回收内存,⾃动调⽤析构函数;
  • 静态局部变量初始化时,没有 new 操作带来的cpu指令reorder操作;
  • c++11 静态局部变量初始化时,具备线程安全;
// c++11 magic static 特性:如果当变量在初始化的时候,并发同时进⼊声明语句,并发线程将会阻塞等待初始化结束。
// c++ effective
class Singleton
{
public:
    static Singleton& GetInstance() {
    static Singleton instance;
    return instance;
 }
private:
  Singleton(){}
  ~Singleton() {}
  Singleton(const Singleton&)=delete;
  Singleton& operator=(const Singleton&)=delete;
};

int main(int argc, char *argv[])
{
    Singleton& instance_1 = Singleton::GetInstance();
    Singleton& instance_2 = Singleton::GetInstance();
    return 0;
}

代码示例四

模板方法

template<typename T>
class Singleton {
public:
  static T& GetInstance() {
    static T instance; // 这⾥要初始化DesignPattern,需要调⽤DesignPattern
构造函数,同时会调⽤⽗类的构造函数。
    return instance;
 }
protected:
  virtual ~Singleton() {}
  Singleton() {} // protected修饰构造函数,才能让别⼈继承
  Singleton(const Singleton&)=delete;
  Singleton& operator =(const Singleton&)=delete;
};


class DesignPattern : public Singleton<DesignPattern> {
  friend class Singleton<DesignPattern>; // friend 能让Singleton<T> 访问到
DesignPattern构造函数
public:
  ~DesignPattern() {}
private:
  DesignPattern() {}
  DesignPattern(const DesignPattern&)=delete;
  DesignPattern& operator=(const DesignPattern&)=delete;
};

3.5 工厂方法

定义

定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例
化延迟到子类。 ——《设计模式》GoF

背景 

实现一个导出数据的接口,让客户选择数据的导出方式;

要点

解决创建过程比较复杂,希望对外隐藏这些细节的场景;

  • 比如连接池、线程池
  • 隐藏对象真实类型;
  • 对象创建会有很多参数来决定如何创建;
  • 创建对象有复杂的依赖关系;

本质

延迟到子类来选择实现;

结构图

 代码示例

#include <string>
// 实现导出数据的接口, 导出数据的格式包含 xml,json,文本格式txt 后面可能扩展excel格式csv
class IExport {
public:
    virtual bool Export(const std::string &data) = 0;
    virtual ~IExport(){}
};

class ExportXml : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportJson : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportTxt : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportCSV : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class IExportFactory {
public:
    IExportFactory() {
        _export = nullptr;
    }
    virtual ~IExportFactory() {
        if (_export) {
            delete _export;
            _export = nullptr;
        }
    }
    bool Export(const std::string &data) {
        if (_export == nullptr) {
            _export = NewExport();
        }
        return _export->Export(data);
    }
protected:
    virtual IExport * NewExport(/* ... */) = 0;
private:
    IExport* _export;
};

class ExportXmlFactory : public IExportFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportXml();
        // 可能之后有什么操作
        return temp;
    }
};
class ExportJsonFactory : public IExportFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportJson;
        // 可能之后有什么操作
        return temp;
    }
};
class ExportTxtFactory : public IExportFactory {
protected:
    IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportTxt;
        // 可能之后有什么操作
        return temp;
    }
};

class ExportCSVFactory : public IExportFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportCSV;
        // 可能之后有什么操作
        return temp;
    }
};

int main () {
    IExportFactory *factory = new ExportTxtFactory();
    factory->Export("hello world");
    return 0;
}

3.6 抽象工厂

定义

提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。
——《设计模式》GoF

背景

实现一个拥有导出导入数据的接口,让客户选择数据的导出导入方式;

结构图

 代码示例

#include <string>
// 实现导出数据的接口, 导出数据的格式包含 xml,json,文本格式txt 后面可能扩展excel格式csv
class IExport {
public:
    virtual bool Export(const std::string &data) = 0;
    virtual ~IExport(){}
};

class ExportXml : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportJson : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportTxt : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class ExportCSV : public IExport {
public:
    virtual bool Export(const std::string &data) {
        return true;
    }
};

class IImport {
public:
    virtual bool Import(const std::string &data) = 0;
    virtual ~IImport(){}
};

class ImportXml : public IImport {
public:
    virtual bool Import(const std::string &data) {
        return true;
    }
};

class ImportJson : public IImport {
public:
    virtual bool Import(const std::string &data) {
        return true;
    }
};

class ImportTxt : public IImport {
public:
    virtual bool Import(const std::string &data) {
        return true;
    }
};

class ImportCSV : public IImport {
public:
    virtual bool Import(const std::string &data) {
        // ....
        return true;
    }
};

class IDataApiFactory {
public:
    IDataApiFactory() {
        _export = nullptr;
        _import = nullptr;
    }
    virtual ~IDataApiFactory() {
        if (_export) {
            delete _export;
            _export = nullptr;
        }
        if (_import) {
            delete _import;
            _import = nullptr;
        }
    }
    bool Export(const std::string &data) {
        if (_export == nullptr) {
            _export = NewExport();
        }
        return _export->Export(data);
    }
    bool Import(const std::string &data) {
        if (_import == nullptr) {
            _import = NewImport();
        }
        return _import->Import(data);
    }
protected:
    virtual IExport * NewExport(/* ... */) = 0;
    virtual IImport * NewImport(/* ... */) = 0;
private:
    IExport *_export;
    IImport *_import;
};

class XmlApiFactory : public IDataApiFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportXml;
        // 可能之后有什么操作
        return temp;
    }
    virtual IImport * NewImport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IImport * temp = new ImportXml;
        // 可能之后有什么操作
        return temp;
    }
};

class JsonApiFactory : public IDataApiFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportJson;
        // 可能之后有什么操作
        return temp;
    }
    virtual IImport * NewImport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IImport * temp = new ImportJson;
        // 可能之后有什么操作
        return temp;
    }
};
class TxtApiFactory : public IDataApiFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportTxt;
        // 可能之后有什么操作
        return temp;
    }
    virtual IImport * NewImport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IImport * temp = new ImportTxt;
        // 可能之后有什么操作
        return temp;
    }
};

class CSVApiFactory : public IDataApiFactory {
protected:
    virtual IExport * NewExport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IExport * temp = new ExportCSV;
        // 可能之后有什么操作
        return temp;
    }
    virtual IImport * NewImport(/* ... */) {
        // 可能有其它操作,或者许多参数
        IImport * temp = new ImportCSV;
        // 可能之后有什么操作
        return temp;
    }
};

int main () {
    IDataApiFactory *factory = new CSVApiFactory();
    factory->Import("hello world");
    factory->Export("hello world");
    return 0;
}

3.7 责任链

定义

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成
一条链,并沿着这条链传递请求,直到有一个对象处理它为止。 ——《设计模式》GoF

背景

请求流程,1 天内需要主程序批准,3 天内需要项目经理批准,3 天以上需要老板批准;

要点

  • 解耦请求方和处理方,请求方不知道请求是如何被处理,处理方的组成是由相互独立的子处理构成,子处理流程通过链表的方式连接,子处理请求可以按任意顺序组合;
  • 责任链请求强调请求最终由一个子处理流程处理;通过了各个子处理条件判断;
  • 责任链扩展就是功能链,功能链强调的是,一个请求依次经由功能链中的子处理流程处理;
  • 将职责以及职责顺序运行进行抽象,那么职责变化可以任意扩展,同时职责顺序也可以任意扩展; 

本质

分离职责,动态组合

代码样例

#include <string>

class Context {
public:
    std::string name;
    int day;
};


class IHandler {
public:
    virtual ~IHandler() {}
    void SetNextHandler(IHandler *next) {
        next = next;
    }
    bool Handle(const Context &ctx) {
        if (CanHandle(ctx)) {
            return HandleRequest(ctx);
        } else if (GetNextHandler()) {
            return GetNextHandler()->HandleRequest(ctx);
        } else {
            // err
        }
        return false;
    }
protected:
    virtual bool HandleRequest(const Context &ctx) = 0;
    virtual bool CanHandle(const Context &ctx) =0;
    IHandler * GetNextHandler() {
        return next;
    }
private:
    IHandler *next;
};

class HandleByMainProgram : public IHandler {
protected:
    virtual bool HandleRequest(const Context &ctx){
        //
        return true;
    }
    virtual bool CanHandle(const Context &ctx) {
        //
        return true;
    }
};

class HandleByProjMgr : public IHandler {
protected:
    virtual bool HandleRequest(const Context &ctx){
        //
        return true;
    }
    virtual bool CanHandle(const Context &ctx) {
        //
        return true;
    }
};
class HandleByBoss : public IHandler {
protected:
    virtual bool HandleRequest(const Context &ctx){
        //
        return true;
    }
    virtual bool CanHandle(const Context &ctx) {
        //
        return true;
    }
};

class HandleByBeauty : public IHandler {
protected:
    virtual bool HandleRequest(const Context &ctx){
        //
        return true;
    }
    virtual bool CanHandle(const Context &ctx) {
        //
        return true;
    }
};

int main() {

    IHandler * h0 = new HandleByBeauty();
    IHandler * h1 = new HandleByMainProgram();
    IHandler * h2 = new HandleByProjMgr();
    IHandler * h3 = new HandleByBoss();
    h0->SetNextHandler(h1);
    h1->SetNextHandler(h2);
    h2->SetNextHandler(h3);
    // 设置下一指针 
    Context ctx;
    h0->Handle(ctx);
    return 0;
}

3.8 装饰器

定义

动态地给一个对象增加一些额外的职责。就增加功能而言,装饰器模式比生产子类更为灵活。
—— 《设计模式》GoF

背景

普通员工有销售奖金,累计奖金,部门经理除此之外还有团队奖金;后面可能会添加环比增长奖金,同时可能针对不同的职位产生不同的奖金组合;

要点

  •  通过采用组合而非继承的手法, 装饰器模式实现了在运行时动态扩展对象功能的能力,而且可以根据需要扩展多个功能。 避免了使用继承带来的“灵活性差”和“多子类衍生问题”。
  • 不是解决“多子类衍生问题”问题,而是解决“父类在多个方向上的扩展功能”问题;
  • 装饰器模式把一系列复杂的功能分散到每个装饰器当中,一般一个装饰器只实现一个功能,实现复用装饰器的功能;

本质

动态组合

结构图 

 代码样例

#include <iostream>
// 普通员工有销售奖金,累计奖金,部门经理除此之外还有团队奖金;后面可能会添加环比增长奖金,同时可能产生不同的奖金组合;
// 销售奖金 = 当月销售额 * 4%
// 累计奖金 = 总的回款额 * 0.2%
// 部门奖金 = 团队销售额 * 1%
// 环比奖金 = (当月销售额-上月销售额) * 1%
// 销售后面的参数可能会调整
using namespace std;
class Context {
public:
    bool isMgr;
    // User user;
    // double groupsale;
};

// 试着从职责出发,将职责抽象出来
class CalcBonus {    
public:
    CalcBonus(CalcBonus * c = nullptr) : cc(c) {}
    virtual double Calc(Context &ctx) {
        return 0.0; // 基本工资
    }
    virtual ~CalcBonus() {}

protected:
    CalcBonus* cc;
};

class CalcMonthBonus : public CalcBonus {
public:
    CalcMonthBonus(CalcBonus * c) : CalcBonus(c) {}
    virtual double Calc(Context &ctx) {
        double mbonus /*= 计算流程忽略*/; 
        return mbonus + cc->Calc(ctx);
    }
};

class CalcSumBonus : public CalcBonus {
public:
    CalcSumBonus(CalcBonus * c) : CalcBonus(c) {}
    virtual double Calc(Context &ctx) {
        double sbonus /*= 计算流程忽略*/; 
        return sbonus + cc->Calc(ctx);
    }
};

class CalcGroupBonus : public CalcBonus {
public:
    CalcGroupBonus(CalcBonus * c) : CalcBonus(c) {}
    virtual double Calc(Context &ctx) {
        double gbnonus /*= 计算流程忽略*/; 
        return gbnonus + cc->Calc(ctx);
    }
};

class CalcCycleBonus : public CalcBonus {
public:
    CalcCycleBonus(CalcBonus * c) : CalcBonus(c) {}
    virtual double Calc(Context &ctx) {
        double gbnonus /*= 计算流程忽略*/; 
        return gbnonus + cc->Calc(ctx);
    }
};

int main() {
    // 1. 普通员工
    Context ctx1;
    CalcBonus *base = new CalcBonus();
    CalcBonus *cb1 = new CalcSumBonus(base);
    CalcBonus *cb2 = new CalcMonthBonus(cb1);
    cb2->Calc(ctx1);
    // 2. 部门经理
    Context ctx2;
    CalcBonus *cb3 = new CalcGroupBonus(cb2);
    cb3->Calc(ctx2);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值