设计模式之责任链Chain of Responsibility
来源:互联网 发布:知乎 上海 外国人 医院 编辑:程序博客网 时间:2024/06/08 05:10
将一种响应(请求)通过一条彼此关联的处理类处理。一般遵照由小到大的粒度。
典型的例子:界面帮助请求
代码示例:
class PurchasePower{protected:static final double _BASE = 500;PurchasePower* _successor;protected:virtual double getAllowable() = 0;virtual string getRole() = 0;void setSuccessor(PurchasePower* successor){_successor = successor;}void processRequest(PurchaseRequest* request){if(reuqest->getAmount() < getAllowable() ){cout << getRole() << "will approve $" + request->getAmount() << endl;}else if(_successor != nullptr){_successor->processRequest(request);}}};class ManagerPPower : public PurchasePower{protected:virtual double getAllowable() override{return _BASE * 10;}virtual string getRole() override{return "Manager";}};class DirectorPPower : public PurchasePower{protected:virtual double getAllowable() override{return _BASE * 20;}virtual string getRole() override{return "Director";}};class VicePresidentPPower : public PurchasePower{protected:virtual double getAllowable() override{return _BASE * 40;}virtual string getRole() override{return "Vice President";}};class PresidentPPower : public PurchasePower{protected:virtual double getAllowable() override{return _BASE * 60;}virtual string getRole() override{return "President";}};class PurchaseRequest{private:double _amount;string _purpose;public:PurchaseRequest(double amount, string purpose){_amount = amount;_purpose = purpose;}double getAmount(){return _amount;}void setAmount(double amount){_amount = amount;}string getPurpose(){return _purpose;}void setPurpose(string purpose){_purpose = purpose;}};void main(){ManagerPPower* manager = new ManagerPPower();DirectorPPower* director = new DirectorPPower();VicePresidentPPower* vp = new VicePresidentPPower();PresidentPPower* president = new PresidentPPower();manager->setSuccessor(director);director->setSuccessor(vp);vp->setSuccessor(president);try{while(true){cout << "Enter the amount to check who should approve your expenditure." << endl;cout << ">" << endl;double d = 0;cin >> d;manager->processRequest(new PurchaseRequest(d, "General"));}}catch(...){exit(1);}}
阅读全文
0 0
- 设计模式之责任链Chain of Responsibility模式
- 设计模式之责任链模式(Chain of Responsibility)
- 设计模式之---责任链模式(Chain of Responsibility)
- 设计模式之责任链模式--- Pattern chain-of-responsibility
- 设计模式之13 - 责任链模式Chain Of Responsibility
- 设计模式拾荒之责任链模式 ( Chain of Responsibility)
- 设计模式之责任链模式(Chain of Responsibility)
- 设计模式之二十一------ chain of responsibility(责任链)
- 设计模式之责任链(Chain of Responsibility)
- 设计模式之-责任链(Chain of responsibility )
- 设计模式之责任链Chain of Responsibility
- 设计模式 - Chain of Responsibility Pattern(责任链模式)
- 设计模式 - Chain of Responsibility Pattern(责任链模式)
- 设计模式:责任链模式(Chain of Responsibility, CoR)
- 设计模式——责任链模式(Chain Of Responsibility)
- JAVA设计模式—责任链模式(Chain Of Responsibility)
- 设计模式初探-责任链模式(CHAIN OF RESPONSIBILITY)
- [设计模式-行为型]责任链模式(Chain of Responsibility)
- 爬虫实战-获取wegene网站上SNP位点信息
- CHtmlView或IE控件调用js的方法
- leetcode之Merge Two Binary Trees
- sql server 2012找不到 sql server configuration manager
- Removing obsolete files from server...电脑异常关机,导致tomcat出问题
- 设计模式之责任链Chain of Responsibility
- jQuery:基础学习(3)事件,动画效果
- Max Consecutive Ones
- mybatis中${} #{}使用
- fixed定位问题
- 阿里的开源框架_V-layout的使用
- Lock的使用、ReentrantReadWriteLock实现读写分离
- 用单个for循环同时循环多个变量解多元方程
- Android零基础入门第68节:完善RecyclerView,添加首尾视图