职责链模式(chain of responsibility)c++版本
来源:互联网 发布:淘宝店铺会员管理系统 编辑:程序博客网 时间:2024/06/16 17:51
这是大话设计模式中的职责链模式的c++版本
/* * chain_responsibility.cpp * * Created on: Oct 17, 2017 * Author: clh01s@163.com * 职责链模式:使多个对象都有机会处理请求, * 从而避免请求的发送者和接收 * 者之间的耦合关系。将这个对 * 象连成一条链,并沿着这条链 * 传递该请求,直到有一个对象 * 处理它为止。 * 职责链模式与状态模式非常的相似。但是它们还是有所区别。 * 职责链模式:消息的处理对象是用户调用时指定,用户需要知 * 道消息处理的上下家,但是也比较灵活可以随时增加和修改 * 一个请求的结构。 * 状态模式:消息的处理在类定义时就已经确定,用户不需要知道 * 消息的详细处理流程,用户不需要知道太多的细节,但是不够灵 * 活一个新的处理就需要添加或者修改一个或多个类。 */#include <iostream>#include <string>using namespace std;class Request{public: void SetRequesType(string RequestType){_RequestType = RequestType; /*cout<<_RequestType<<endl;*/} string GetRequesType(){return _RequestType;} void SetRequestContent(string RequestContent){_RequestContent = RequestContent; /*cout<<_RequestContent<<endl;*/} string GetRequestContent(){return _RequestContent;} void SetNum(int num){_num = num;/*cout<<_num<<endl;*/} int GetNum(){return _num;}private: //申请类别 string _RequestType; //申请内容 string _RequestContent; //申请数量 int _num;};//管理者类class Manager{public: Manager(string name):name_(name),superior_(NULL){} virtual ~Manager(){} void SetSuperior(Manager* superior){superior_ = superior;} //申请请求 virtual void RequestApplications(Request* request)=0;protected: string name_; //管理者上级 Manager* superior_;};//经理class CommonManager:public Manager{public: CommonManager(string name):Manager(name){} void RequestApplications(Request* request) override { if(("请假" == request->GetRequesType() ) && (request->GetNum() <= 2 ) ) { cout<<name_<<"已批准:"<<request->GetRequestContent()<<","<<request->GetNum()<<"天。"<<endl; }else { if(NULL != request) superior_->RequestApplications(request); } }};//总监class Majordomo:public Manager{public: Majordomo(string name):Manager(name){} void RequestApplications(Request* request) override { if(("请假" == request->GetRequesType() ) && (request->GetNum() <= 5 ) ) { cout<<name_<<"已批准:"<<request->GetRequestContent()<<","<<request->GetNum()<<"天。"<<endl; }else { if(NULL != request) superior_->RequestApplications(request); } }}; class GeneralManager: public Manager { public: GeneralManager(string name):Manager(name){} void RequestApplications(Request* request) override { if(("请假" == request->GetRequesType() ) ) { cout<<name_<<"已批准:"<<request->GetRequestContent()<<","<<request->GetNum()<<"天。"<<endl; }else if (("加薪" == request->GetRequesType() ) && (request->GetNum() <= 500 ) ) { cout<<name_<<"已批准:"<<request->GetRequestContent()<<","<<request->GetNum()<<"元。"<<endl; } else if(("加薪" == request->GetRequesType() ) && (request->GetNum() > 500 ) ) { cout<<name_<<":"<<request->GetRequestContent()<<","<<request->GetNum()<<"。公司需要考虑一下...."<<endl; } } };int main(){ CommonManager* jingli = new CommonManager("经理"); Majordomo* zongjian = new Majordomo("总监"); GeneralManager* zongjingli = new GeneralManager("总经理"); //设置各个上级 jingli->SetSuperior(zongjian); zongjian->SetSuperior(zongjingli); //请求 Request* request = new Request(); request->SetRequesType("请假"); request->SetRequestContent("小王请假"); request->SetNum(2); jingli->RequestApplications(request); //请求 request->SetRequesType("请假"); request->SetRequestContent("小王请假"); request->SetNum(4); jingli->RequestApplications(request); //请求 request->SetRequesType("加薪"); request->SetRequestContent("小王申请加薪"); request->SetNum(500); jingli->RequestApplications(request); //请求 request->SetRequesType("加薪"); request->SetRequestContent("小王申请加薪"); request->SetNum(1200); jingli->RequestApplications(request); delete request,zongjian,zongjingli,request; return 0;}
执行结果:
clh@clh:~/testcode/设计模式$ g++ chain_responsibility.cpp -std=c++11 -gclh@clh:~/testcode/设计模式$ ./a.out 经理已批准:小王请假,2天。总监已批准:小王请假,4天。总经理已批准:小王申请加薪,500元。总经理:小王申请加薪,1200。公司需要考虑一下....
以下内容摘抄自《设计模式》
职责链模式适用于以下场景:
1.有多个对象可以处理一个请求,哪个对象处理该请求由运行时刻自动确定。
2.你想在不明确指定接受者的情况下,向多个对象中的一个提交一个请求。
3.可处理一个请求的对象集合应被动态指定。
职责链的优点:
1.降低耦合度 该模式使得一个对象无需知道是其哪一个对象处理其请求。对象仅需知道该请求会被“正确”地处理。
2.增强了给对象指派职责的灵活性 当在对象中分派职责时,职责链给你更多的灵活性。
职责链缺点:
1.不保证被接受 既然一个请求没有明确的接收者,那么就不能保证它一定会被处理该请求可能一直到链的末端都得不到处理。也可能因为链的配置不正确而得不到处理。
转载请注明源地址:http://blog.csdn.net/clh01s
阅读全文
0 0
- 职责链模式(chain of responsibility)c++版本
- 职责链模式(Chain of Responsibility)
- Chain of Responsibility(职责链模式)
- 职责链(Chain of Responsibility)模式
- 职责链模式(Chain Of Responsibility)
- 职责链模式(Chain of Responsibility)
- 职责链模式(Chain of Responsibility)
- Chain Of Responsibility(职责链)模式
- 职责链模式(Chain Of Responsibility)
- 职责链(Chain of Responsibility)模式
- 职责链模式(Chain of Responsibility)
- 职责链模式(Chain of Responsibility)
- 职责链模式(Chain of Responsibility)
- 职责链模式(Chain Of Responsibility)
- 职责链模式(Chain of Responsibility)
- 职责链(Chain of responsibility)模式
- Java模式 - Chain of Responsibility(职责链模式)
- 设计模式-职责链模式(chain of Responsibility)
- python3 爬虫https的坑 -- 已解决
- node-web服务器简单搭建
- Oracle数据库使用pfile启动还是spfile启动---oracle
- git在push的时候出现insufficient permission for adding an object错误
- 配置snooping和ipsg遇到的特定IP的情况处理
- 职责链模式(chain of responsibility)c++版本
- python 报错解决办法
- 实现日期任意跳转
- 【转】libscws(简体中文分词库)
- Set集合
- Android 里的数据储存
- 打开r40平台tinav2.1下的以太网卡(分色排版)
- spring session解决集群环境下session共享问题
- 直方图均衡化