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

137 lines
2.6 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;
2021-10-28 15:15:51 +00:00
// 请求:票据
2019-10-29 14:14:19 +00:00
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;
};
2021-10-28 15:15:51 +00:00
// 抽象处理者
2019-10-29 14:14:19 +00:00
class Approver
{
public:
Approver(){}
Approver(string iName){
setName(iName);
}
virtual ~Approver(){}
2021-10-28 15:15:51 +00:00
// 添加上级
2019-10-29 14:14:19 +00:00
void setSuperior(Approver *iSuperior){
this->superior = iSuperior;
}
2021-10-28 15:15:51 +00:00
// 处理请求
2019-10-29 14:14:19 +00:00
virtual void handleRequest(Bill*) = 0;
string getName(){
return name;
}
void setName(string iName){
name = iName;
}
protected:
Approver *superior;
private:
string name;
};
2021-10-28 15:15:51 +00:00
// 具体处理者:组长
2019-10-29 14:14:19 +00:00
class GroupLeader :public Approver
{
public:
GroupLeader(){}
GroupLeader(string iName){
setName(iName);
}
2021-10-28 15:15:51 +00:00
// 处理请求
2019-10-29 14:14:19 +00:00
void handleRequest(Bill *bill){
if (bill->getAccount() < 10){
2021-10-28 15:15:51 +00:00
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
2019-10-29 14:14:19 +00:00
bill->print();
}
else{
2021-10-28 15:15:51 +00:00
printf("组长无权处理,转交上级……\n");
2019-10-29 14:14:19 +00:00
this->superior->handleRequest(bill);
}
}
};
2021-10-28 15:15:51 +00:00
// 具体处理者:主管
2019-10-29 14:14:19 +00:00
class Head :public Approver
{
public:
Head(){}
Head(string iName){
setName(iName);
}
2021-10-28 15:15:51 +00:00
// 处理请求
2019-10-29 14:14:19 +00:00
void handleRequest(Bill *bill){
if (bill->getAccount() >= 10 && bill->getAccount()<30){
2021-10-28 15:15:51 +00:00
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
2019-10-29 14:14:19 +00:00
bill->print();
}
else{
2021-10-28 15:15:51 +00:00
printf("主管无权处理,转交上级……\n");
2019-10-29 14:14:19 +00:00
this->superior->handleRequest(bill);
}
}
};
2021-10-28 15:15:51 +00:00
// 具体处理者:经理
2019-10-29 14:14:19 +00:00
class Manager :public Approver
{
public:
Manager(){}
Manager(string iName){
setName(iName);
}
2021-10-28 15:15:51 +00:00
// 处理请求
2019-10-29 14:14:19 +00:00
void handleRequest(Bill *bill){
if (bill->getAccount() >= 30 && bill->getAccount()<60){
2021-10-28 15:15:51 +00:00
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
2019-10-29 14:14:19 +00:00
bill->print();
}
else{
2021-10-28 15:15:51 +00:00
printf("经理无权处理,转交上级……\n");
2019-10-29 14:14:19 +00:00
this->superior->handleRequest(bill);
}
}
};
2021-10-28 15:15:51 +00:00
// 具体处理者:老板
2019-10-29 14:14:19 +00:00
class Boss :public Approver
{
public:
Boss(){}
Boss(string iName){
setName(iName);
}
2021-10-28 15:15:51 +00:00
// 处理请求
2019-10-29 14:14:19 +00:00
void handleRequest(Bill *bill){
2021-10-28 15:15:51 +00:00
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
2019-10-29 14:14:19 +00:00
bill->print();
}
};
#endif //__CHAIN_OF_RESPONSIBILITY_PATTERN_H__