实验15:职责链模式
[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
直接放源码:
#include <iostream> #include <string> using namespace std; class ShoppingRequest { public: ShoppingRequest(string shoppingReaso,double billMone){ ShoppingReason=shoppingReaso; billMoney=billMone; } string getShoppingReason() { return ShoppingReason; } void setShoppingReason(string shoppingReaso) { ShoppingReason = shoppingReaso; } double getBillMoney() { return billMoney; } void setBillMoney(double billMone) { billMoney = billMone; } private: string ShoppingReason; double billMoney; }; class Leader { public: Leader(string nam){ name=nam; } void setSuccessor(Leader* successo){ successor=successo; } virtual void handleRequest(ShoppingRequest* request) = 0; protected: string name; Leader* successor; }; class Director : public Leader //PM:Project manager项目经理 { public: Director(string name):Leader(name){}; void handleRequest(ShoppingRequest* request){ if(request->getBillMoney()<10000.00){ cout <<"主任"<<name<<"处理物资采集:"<< request->getShoppingReason()<< ",批准采购资金为:"<<request->getBillMoney()<<"RMB"<< endl; }else{ successor->handleRequest(request); } } }; class DepartmentManager : public Leader //PM:Project manager项目经理 { public: DepartmentManager(string name):Leader(name){}; void handleRequest(ShoppingRequest* request){ if(request->getBillMoney()<50000.00){ cout <<"部门经理"<<name<<"处理物资采集:"<< request->getShoppingReason()<< ",批准采购资金为:"<<request->getBillMoney()<<"RMB"<< endl; }else{ successor->handleRequest(request); } } }; class ViceManager : public Leader //PM:Project manager项目经理 { public: ViceManager(string name):Leader(name){}; void handleRequest(ShoppingRequest* request){ if(request->getBillMoney()<100000.00){ cout <<"副经理"<<name<<"处理物资采集:"<< request->getShoppingReason()<< ",批准采购资金为:"<<request->getBillMoney()<<"RMB"<< endl; }else{ successor->handleRequest(request); } } }; class GeneralManager : public Leader //PM:Project manager项目经理 { public: GeneralManager(string name):Leader(name){}; void handleRequest(ShoppingRequest* request){ if(request->getBillMoney()<200000.00){ cout <<"总经理"<<name<<"处理物资采集:"<< request->getShoppingReason()<< ",批准采购资金为:"<<request->getBillMoney()<<"RMB"<< endl; }else{ cout <<"董事会处理物资采集:"<< request->getShoppingReason()<< ",批准采购资金为:"<<request->getBillMoney()<<"RMB"<< endl; } } }; int main() { Leader* l1,* l2,* l3,* l4; l1=new Director("张三"); l2=new DepartmentManager("李四"); l3=new ViceManager("王五"); l4=new GeneralManager("赵六"); l1->setSuccessor(l2); l2->setSuccessor(l3); l3->setSuccessor(l4); ShoppingRequest* s1=new ShoppingRequest("买a4纸",1000.00); ShoppingRequest* s2=new ShoppingRequest("买桌子",30000.00); ShoppingRequest* s3=new ShoppingRequest("买电脑",90000.00); ShoppingRequest* s4=new ShoppingRequest("买机器",150000.00); ShoppingRequest* s5=new ShoppingRequest("收购公司",1000000.00); l1->handleRequest(s1); cout<<"--------------------------------------------"<<endl; l1->handleRequest(s2); cout<<"--------------------------------------------"<<endl; l1->handleRequest(s3); cout<<"--------------------------------------------"<<endl; l1->handleRequest(s4); cout<<"--------------------------------------------"<<endl; l1->handleRequest(s5); cout<<"--------------------------------------------"<<endl; }
标签:15,name,request,public,ShoppingRequest,实验设计,软件,Leader,string From: https://www.cnblogs.com/lyf3701/p/16814459.html