行为型模式-职责链(chain_of_responsibility)
2017-03-22 22:55
162 查看
职责链
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.实例
main.cc:#include "captain_handler.h" #include "colonel_handler.h" #include "chief_handler.h" #include <windows.h> /* design_pattern:"chain_of_responsibility" Running club, there is a girl to apply to join, the basic information sends to the captain, the captain agreed ,then submit to the colonel, the colonel agreed ,then submit to the chief, if chief agrees,join the girl; */ int main(){ Girl *xiaoli = new Girl("xiaoli",'A'); Girl *xiaomei = new Girl("xiaomei",'B'); Girl *xiaoya = new Girl("xiaoya",'C'); Girl *xiaohong = new Girl("xiaohong",'D'); Handler *captain_handler = new CaptainHandler("captain_tom"); Handler *colonel_handler = new ColonelHandler("colone_jack"); Handler *chief_handler = new ChiefHandler("chief_jone"); colonel_handler->SetHandler(chief_handler); captain_handler->SetHandler(colonel_handler); captain_handler->Accept(xiaoli); captain_handler->Accept(xiaomei); captain_handler->Accept(xiaoya); captain_handler->Accept(xiaohong); //clear delete xiaoli; delete xiaomei; delete xiaoya; delete xiaohong; delete captain_handler; delete colonel_handler; delete chief_handler; system("Pause"); return 0; }
Handler:
//handler.h #ifndef HELENDP_SOURCE_HANDLER_H_ #define HELENDP_SOURCE_HANDLER_H_ #include <string> #include "girl.h" using namespace std; class Handler{ public: Handler(); virtual ~Handler(); void SetHandler(Handler *handler); virtual void Accept(Girl *girl) = 0; protected: string name_; Handler *handler_; }; #endif //handler.cc #include "handler.h" Handler::Handler(){ } Handler::~Handler(){ } void Handler::SetHandler(Handler * handler){ handler_ = handler; }
CaptainHandler:
//captain_handler.h #ifndef HELENDP_SOURCE_CAPTAIN_HANDLER_H_ #define HELENDP_SOURCE_CAPTAIN_HANDLER_H_ #include "handler.h" class CaptainHandler : public Handler{ public: CaptainHandler(string name); ~CaptainHandler(); virtual void Accept(Girl *girl); }; #endif //captain_handler.cc #include "captain_handler.h" #include <iostream> using namespace std; CaptainHandler::CaptainHandler(string name){ name_ = name; handler_ = NULL; } CaptainHandler::~CaptainHandler(){ } void CaptainHandler::Accept(Girl *girl){ cout << endl << "audit(" << girl->GetName() << ")" << endl; if(girl->GetSize() < 'B'){ cout << "captain" << "(" << name_ << "):too small,not pass!" << endl; } else{ if(handler_) { cout << "captain" << "(" << name_ << "):Good,I'll submit to the colonel" << endl; handler_->Accept(girl); } } }
ColoneHandler:
//colone_handler.h #ifndef HELENDP_SOURCE_COLONEL_HANDLER_H_ #define HELENDP_SOURCE_COLONEL_HANDLER_H_ #include "handler.h" class ColonelHandler : public Handler{ public: ColonelHandler(string name); ~ColonelHandler(); virtual void Accept(Girl *girl); }; #endif //colone_handler.cc #include "colonel_handler.h" #include <iostream> using namespace std; ColonelHandler::ColonelHandler(string name){ name_ = name; handler_ = NULL; } ColonelHandler::~ColonelHandler(){ } void ColonelHandler::Accept(Girl *girl){ if(girl->GetSize() < 'C'){ cout << "colone" << "(" << name_ << "):too small,not pass!" << endl; } else{ if(handler_) { cout << "colone" << "(" << name_ << "):Good,I'll submit to the chief" << endl; handler_->Accept(girl); } } }
ChiefHandler:
//chief_handler.h #ifndef HELENDP_SOURCE_CHIEF_HANDLER_H_ #define HELENDP_SOURCE_CHIEF_HANDLER_H_ #include "handler.h" class ChiefHandler : public Handler{ public: ChiefHandler(string name); ~ChiefHandler(); virtual void Accept(Girl *girl); }; #endif //chief_handler.cc #include "chief_handler.h" #include <iostream> using namespace std; ChiefHandler::ChiefHandler(string name){ name_ = name; handler_ = NULL; } ChiefHandler::~ChiefHandler(){ } void ChiefHandler::Accept(Girl *girl){ if(girl->GetSize() < 'D'){ cout << "chief" << "(" << name_ << "):too small,not pass!" << endl; } else{ cout << "chief(" << name_ << "):the gril("<< girl->GetName() <<") big enough,pass!" << endl; } }
Girl:
//girl.h #ifndef HELENDP_SOURCE_GIRL_H_ #define HELENDP_SOURCE_GIRL_H_ #include <string> using namespace std; class Girl{ public: Girl(string name,int size); ~Girl(); string GetName(); int GetSize(); private: string name_; int size_; } bce1 ; #endif //girl.cc #include "girl.h" Girl::Girl(string name,int size){ name_ = name; size_ = size; } Girl::~Girl(){ } string Girl::GetName(){ return name_; } int Girl::GetSize(){ return size_; }
代码和UML图(EA)工程文件,最后会整理打包上传.
UML类图
结构
Handler(Handler):定义处理请求的接口类.ConcreteHandler(CaptainHandler,ChiefHandler,ColoneHandler):可以处理请求,并将请求递交给后续的对象.
优点
可以让多个对象以链条的方式处理请求.避免请求的发送者和接收者之间的耦合关系.
可以随时增加或修改一个请求的结构,增强灵活性.
缺点
请求有可能到链尾都没人处理的情况.相关文章推荐
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
- 3.1 Chain of responsibility(职责链) -- 对象行为型模式
- C#设计模式之二十职责链模式(Chain of Responsibility Pattern)【行为型】
- 设计模式(行为型)之职责链模式(Chain of Responsibility Pattern)
- "围观"设计模式(22)--行为型之职责链模式(Chain Of Responsibility Pattern)
- 十一个行为型模式1:职责链模式-Chain of Responsibility Pattern【学习难度:★★★☆☆,使用频率:★★☆☆☆】
- 职责链模式(Chain of Responsibility Pattern)
- 设计模式笔记18:职责链模式(Chain of Responsibility Pattern)
- 职责链模式(Chain of Responsibility Pattern) 转
- php设计模式 Chain Of Responsibility (职责链模式)
- 设计模式----Chain of Responsibility(职责链)模式
- 白话设计模式--实践应用--Chain of Responsibility(职责链)和factory模式
- 设计模式笔记18:职责链模式(Chain of Responsibility Pattern)
- 步步为营 .NET 设计模式学习笔记 十九、Chain of Responsibility(职责链模式)
- 职责链模式(Chain of Responsibility Pattern)
- 15.Javascript设计模式之职责链模式----Chain of Responsibility
- 设计模式袖珍版 连续转载之 - Chain of Responsibility(职责链)
- 职责链模式ChainOfResponsibility
- php设计模式 Chain Of Responsibility (职责链模式)
- 职责链模式(Chain Of Responsibility Pattern)