每日总结

软件设计模式  职责链模式:

 

#include <iostream>
#include <string>
using namespace std;
class CGD {
public:
    int money;
    CGD() {};
    CGD(int money) { this->money = money; };
    int getMoney() { return money; };
    void setMoney(int money) { this->money = money; };
};
class Leader {
public:
    string name;
    Leader* up;
    Leader() {};
    Leader(string name);
    void setUp(Leader* up) { this->up = up; };
    virtual void handleCGD(CGD* cgd) = 0;
};
class ZR :public Leader {
public:
    ZR(string name) { this->name = name; };
    void handleCGD(CGD* cgd) {
        if (cgd->getMoney() < 10000) {
            cout << "主任" << name << "审批了" << cgd->getMoney() << "元的采购单" << endl;
        }
        else {
            this->up->handleCGD(cgd);
        }
    };

};
class BMJL :public Leader {
public:
    BMJL(string name) { this->name = name; };
    void handleCGD(CGD* cgd) {
        if (cgd->getMoney() < 50000) {
            cout << "部门经理" << name << "审批了" << cgd->getMoney() << "元的采购单" << endl;
        }
        else {
            this->up->handleCGD(cgd);
        }
    };
};
class FZJL :public Leader {
public:
    FZJL(string name) { this->name = name; };
    void handleCGD(CGD* cgd) {
        if (cgd->getMoney() < 100000) {
            cout << "副总经理" << name << "审批了" << cgd->getMoney() << "元的采购单" << endl;
        }
        else {
            this->up->handleCGD(cgd);
        }
    };
};
class ZJL :public Leader {
public:
    ZJL(string name) { this->name = name; };
    void handleCGD(CGD* cgd) {
        if (cgd->getMoney() < 200000) {
            cout << "总经理" << name << "审批了" << cgd->getMoney() << "元的采购单" << endl;
        }
        else {
            cout << cgd->getMoney() << "元的采购单需要开职工大会来决定" << endl;
        }
    };
};
int main() {
    Leader* zr, * bmjl, * fzjl, * zjl;

    zr = new ZR("老王");
    bmjl = new BMJL("老李");
    fzjl = new FZJL("老张");
    zjl = new ZJL("老赵");

    zr->setUp(bmjl);
    bmjl->setUp(fzjl);
    fzjl->setUp(zjl);

    zr->handleCGD(new CGD(100));
    zr->handleCGD(new CGD(1000));
    zr->handleCGD(new CGD(11000));
    zr->handleCGD(new CGD(60000));
    zr->handleCGD(new CGD(110000));
    zr->handleCGD(new CGD(190000));
    zr->handleCGD(new CGD(250000));
    return 0;
}

 

posted @ 2021-10-12 10:24  chenghaixinag  阅读(19)  评论(0编辑  收藏  举报