2021.12.1 职责链模式c++
一、今日学习内容
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
#include<iostream> using namespace std; #include<string> //抽象管理者 class Manager { protected: Manager *m_manager; string m_name; public: Manager(Manager *manager, string name) :m_manager(manager), m_name(name){} virtual void DealWithRequest(string name, int num) {} }; //主任 class Director : public Manager { public: Director(Manager *manager, string name) :Manager(manager, name) {} void DealWithRequest(string name, int num) { if (num < 10000) //主任职权之内 { cout << "主任" << m_name << "审批采购单:" << name << ",采购金额:" << num << "元" << endl << endl; } else { cout << "主任" << m_name << "无法处理,交由部门经理处理" << endl; m_manager->DealWithRequest(name, num); } } }; //部门经理 class CommonManager : public Manager { public: CommonManager(Manager *manager, string name) :Manager(manager, name) {} void DealWithRequest(string name, int num) { if (num < 50000) //部门经理职权之内 { cout << "部门经理" << m_name << "审批采购单:" << name << ",采购金额:" << num << "元" << endl << endl; } else { cout << "部门经理" << m_name << "无法处理,交由副总经理处理" << endl; m_manager->DealWithRequest(name, num); } } }; //副总经理 class VicePresident : public Manager { public: VicePresident(Manager *manager, string name) :Manager(manager, name) {} void DealWithRequest(string name, int num) { if (num < 100000) //副总经理职权之内 { cout << "副总经理" << m_name << "审批采购单:" << name << ",采购金额:" << num << "元" << endl << endl; } else { cout << "副总经理" << m_name << "无法处理,交由总经理处理" << endl; m_manager->DealWithRequest(name, num); } } }; //总经理 class President : public Manager { public: President(Manager *manager, string name) :Manager(manager, name) {} void DealWithRequest(string name, int num) { if (num < 200000) //副总经理可以处理20万元 { cout << "总经理" << m_name << "审批采购单:" << name << ",采购金额:" << num << "元" << endl << endl; } else { cout << "总经理" << m_name << "无法处理,召开职工大会处理" << endl; m_manager->DealWithRequest(name, num); } } }; class Congress : public Manager { public: Congress(Manager *manager, string name) :Manager(manager, name) {} void DealWithRequest(string name, int num) //职工可以处理大于20万元的采购单 { cout << "职工大会" << m_name << "审批采购单:" << name << ",采购金额:" << num << "元" << endl << endl; } }; //主函数 int main() { Manager *congress = new Congress(NULL, ""); //设置上级,职工大会没有上级 Manager *president = new President(congress, "zongjingli"); //设置上级 Manager *vicepresident = new VicePresident(president, "fuzongjingli"); //设置上级 Manager *commonmanager = new CommonManager(vicepresident, "bumenjingli"); //设置上级 Manager *director = new Director(commonmanager, "zhuren"); //设置上级 director->DealWithRequest("01", 5000); //主任张三审批采购单,如若不能处理,逐级往上10001为采购单编号,5000为采购金额 director->DealWithRequest("02", 40000); director->DealWithRequest("03", 80000); director->DealWithRequest("04", 190000); director->DealWithRequest("05", 800000); delete director; delete commonmanager; delete vicepresident; delete president; delete congress; return 0; }