add Chain-of-Responsibility Pattern

master
FengJungle 2019-10-29 22:14:19 +08:00
parent e2135a6368
commit bf547ae8be
10 changed files with 258 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -0,0 +1,64 @@
#ifndef __FLYPATTERN_PATTERN_H__
#define __FLYPATTERN_PATTERN_H__
#include <mutex>
#include <time.h>
using namespace std;
// 抽象主题角色
class Subject
{
public:
Subject(){}
virtual void method() = 0;
};
// 真实主题角色
class RealSubject :public Subject
{
public:
RealSubject(){}
void method(){
printf("调用业务方法\n");
}
};
// Log类
class Log
{
public:
Log(){}
string getTime(){
time_t t = time(NULL);
char ch[64] = { 0 };
//年-月-日 时:分:秒
strftime(ch, sizeof(ch)-1, "%Y-%m-%d %H:%M:%S", localtime(&t));
return ch;
}
};
// 代理类
class Proxy:public Subject
{
public:
Proxy(){
realSubject = new RealSubject();
log = new Log();
}
void preCallMethod(){
printf("方法method()被调用,调用时间为%s\n",log->getTime().c_str());
}
void method(){
preCallMethod();
realSubject->method();
postCallMethod();
}
void postCallMethod(){
printf("方法method()调用调用成功!\n");
}
private:
RealSubject *realSubject;
Log* log;
};
#endif //__FLYPATTERN_PATTERN_H__

View File

@ -0,0 +1,14 @@
#include <iostream>
#include "ProxyPattern.h"
int main()
{
Subject *subject;
subject = new Proxy();
subject->method();
printf("\n\n");
system("pause");
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 420 KiB

View File

@ -0,0 +1,135 @@
#ifndef __CHAIN_OF_RESPONSIBILITY_PATTERN_H__
#define __CHAIN_OF_RESPONSIBILITY_PATTERN_H__
#include <mutex>
#include <time.h>
using namespace std;
// 请求:票据
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;
};
// 抽象处理者
class Approver
{
public:
Approver(){}
Approver(string iName){
setName(iName);
}
// 添加上级
void setSuperior(Approver *iSuperior){
this->superior = iSuperior;
}
// 处理请求
virtual void handleRequest(Bill*) = 0;
string getName(){
return name;
}
void setName(string iName){
name = iName;
}
protected:
Approver *superior;
private:
string name;
};
// 具体处理者:组长
class GroupLeader :public Approver
{
public:
GroupLeader(){}
GroupLeader(string iName){
setName(iName);
}
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() < 10){
printf("组长 %s 处理了该票据,票据信息:",this->getName().c_str());
bill->print();
}
else{
printf("组长无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:主管
class Head :public Approver
{
public:
Head(){}
Head(string iName){
setName(iName);
}
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() >= 10 && bill->getAccount()<30){
printf("主管 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
else{
printf("主管无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:经理
class Manager :public Approver
{
public:
Manager(){}
Manager(string iName){
setName(iName);
}
// 处理请求
void handleRequest(Bill *bill){
if (bill->getAccount() >= 30 && bill->getAccount()<60){
printf("经理 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
else{
printf("经理无权处理,转交上级……\n");
this->superior->handleRequest(bill);
}
}
};
// 具体处理者:老板
class Boss :public Approver
{
public:
Boss(){}
Boss(string iName){
setName(iName);
}
// 处理请求
void handleRequest(Bill *bill){
printf("老板 %s 处理了该票据,票据信息:", this->getName().c_str());
bill->print();
}
};
#endif //__CHAIN_OF_RESPONSIBILITY_PATTERN_H__

View File

@ -0,0 +1,33 @@
#include <iostream>
#include "ChainOfResponsibility.h"
int main()
{
// 请求处理者:组长,兵哥,春总,老板
Approver *zuzhang, *bingge, *chunzong, *laoban;
zuzhang = new GroupLeader("孙大哥");
bingge = new Head("兵哥");
chunzong = new Manager("春总");
laoban = new Boss("张老板");
zuzhang->setSuperior(bingge);
bingge->setSuperior(chunzong);
chunzong->setSuperior(laoban);
// 创建报销单
Bill *bill1 = new Bill(1, "Jungle", 8);
Bill *bill2 = new Bill(2, "Lucy", 14.4);
Bill *bill3 = new Bill(3, "Jack", 32.9);
Bill *bill4 = new Bill(4, "Tom", 89);
// 全部先交给组长审批
zuzhang->handleRequest(bill1); printf("\n");
zuzhang->handleRequest(bill2); printf("\n");
zuzhang->handleRequest(bill3); printf("\n");
zuzhang->handleRequest(bill4);
printf("\n\n");
system("pause");
return 0;
}

View File

@ -1,5 +1,7 @@
# DesignPattern # DesignPattern
前言Jungle设计模式系列 Jungle设计模式系列
代码资源https://github.com/FengJungle/DesignPattern
01.设计模式——设计模式概述 01.设计模式——设计模式概述
@ -60,3 +62,11 @@
15.设计模式(十五)——享元模式 15.设计模式(十五)——享元模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102763849 博客地址https://blog.csdn.net/sinat_21107433/article/details/102763849
15.设计模式(十六)——代理模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102772697
16.设计模式(十七)——职责链模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102790445