#include<cstdio>
using namespace std;
namespace pattern{
/// this pattern links all handler as a line, if previous handler can process a request, it wil not be
/// send to next handler. For example, if someone requested changing programming style,
/// this decision will be made by a programmer.
/// He will change his programming style without noticing his lead.
/// But if someone request changing the requirements, the programmer can not make this decision.
/// He will noticing his lead, and make his lead decided.
/// this is request class.
#define STYLE 0
#define REQIREMENT 1
class Request{
public:
int type; // changing programming style or changing requirements?
};
/// this class for processing request.
class Handler{
private:
Handler* parent;
public:
Handler(Handler* p):parent(p){}
virtual bool DoHandle(Request& r)=0;
void Process(Request& r);
virtual ~Handler(){}
};
void Handler::Process(Request& r){
if(!this->DoHandle(r) && NULL != parent){
parent->Process(r);
}
}
class Programmer: public Handler{
public:
Programmer(Handler* p):Handler(p){}
virtual bool DoHandle(Request& r);
};
bool Programmer::DoHandle(Request& r){
if(r.type == STYLE){
printf("programmer: I can process changing programming style\n");
return true;
}else{
printf("programmer: I can not process this request. I will report this to my lead!\n");
return false;
}
}
class Lead:public Handler{
public:
Lead(Handler* p):Handler(p){}
virtual bool DoHandle(Request& r);
};
bool Lead::DoHandle(Request& r){
if(r.type == REQIREMENT){
printf("lead: I will process changing reqirements\n");
return true;
}
return false;
}
}
int main(){
pattern::Lead lead((pattern::Handler*)NULL);
pattern::Programmer programmer(&lead);
pattern::Request r;
r.type = STYLE;
programmer.Process(r);
r.type = REQIREMENT;
programmer.Process(r);
return 0;
}