DesignPattern/14.ChainOfResponsibility/2.Code/ChainOfResponsibility.h

136 lines
2.4 KiB
C
Raw Normal View History

2019-10-29 14:14:19 +00:00
#ifndef __CHAIN_OF_RESPONSIBILITY_PATTERN_H__
#define __CHAIN_OF_RESPONSIBILITY_PATTERN_H__
#include <mutex>
#include <time.h>
using namespace std;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD><C6B1>
class Bill
{
public:
Bill(){}
Bill(int iId, string iName, double iAccount){
id = iId;
name = iName;
account = iAccount;
}
double getAccount(){
return this->account;
}
void print(){
printf("\nID:\t%d\n", id);
printf("Name:\t%s\n", name.c_str());
printf("Account:\t%f\n", account);
}
private:
int id;
string name;
double account;
};
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
class Approver
{
public:
Approver(){}
Approver(string iName){
setName(iName);
}
virtual ~Approver(){}
2019-10-29 14:14:19 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD>
void setSuperior(Approver *iSuperior){
this->superior = iSuperior;
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
virtual void handleRequest(Bill*) = 0;
string getName(){
return name;
}
void setName(string iName){
name = iName;
}
protected:
Approver *superior;
private:
string name;
};
// <20><><EFBFBD><EFBFBD><E5B4A6><EFBFBD>ߣ<EFBFBD><DFA3>
class GroupLeader :public Approver
{
public:
GroupLeader(){}
GroupLeader(string iName){
setName(iName);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void handleRequest(Bill *bill){
if (bill->getAccount() < 10){
printf("<EFBFBD>鳤 %s <20><><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD>Ʊ<EFBFBD>ݣ<EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>",this->getName().c_str());
bill->print();
}
else{
printf("<EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
this->superior->handleRequest(bill);
}
}
};
// <20><><EFBFBD><EFBFBD><E5B4A6><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD>
class Head :public Approver
{
public:
Head(){}
Head(string iName){
setName(iName);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void handleRequest(Bill *bill){
if (bill->getAccount() >= 10 && bill->getAccount()<30){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD> %s <20><><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD>Ʊ<EFBFBD>ݣ<EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>", this->getName().c_str());
bill->print();
}
else{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
this->superior->handleRequest(bill);
}
}
};
// <20><><EFBFBD><EFBFBD><E5B4A6><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD>
class Manager :public Approver
{
public:
Manager(){}
Manager(string iName){
setName(iName);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void handleRequest(Bill *bill){
if (bill->getAccount() >= 30 && bill->getAccount()<60){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD> %s <20><><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD>Ʊ<EFBFBD>ݣ<EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>", this->getName().c_str());
bill->print();
}
else{
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD>ϼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
this->superior->handleRequest(bill);
}
}
};
// <20><><EFBFBD><EFBFBD><E5B4A6><EFBFBD>ߣ<EFBFBD><DFA3>ϰ<EFBFBD>
class Boss :public Approver
{
public:
Boss(){}
Boss(string iName){
setName(iName);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void handleRequest(Bill *bill){
printf("<EFBFBD>ϰ<EFBFBD> %s <20><><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD>Ʊ<EFBFBD>ݣ<EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>", this->getName().c_str());
bill->print();
}
};
#endif //__CHAIN_OF_RESPONSIBILITY_PATTERN_H__