十一、职责链模式
【摘要】 职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
#include<iostream>
#include<string>
using namespace std;
//请求
class request
{
public:
strin...
职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
#include<iostream>
#include<string>
using namespace std;
//请求
class request
{
public:
string type;//类型
int count;//数量
string content;//内容
public:
request(string m_type, int len ,string m_content):type(m_type),count(len),content(m_content){}
};
//抽象管理类
class Manger
{
protected:
string m_name;
Manger *super;
public:
Manger(string name):m_name(name){}
virtual void setsuper(Manger *m_super)
{
super = m_super;
}
virtual void requestapplication(request m_request){}
};
//经理
class jingli:public Manger
{
public:
jingli(string name):Manger(name){}
void requestapplication(request m_request)
{
if(m_request.type=="请假" && m_request.count<3)
{ cout<<m_name<<": "<<m_request.content<<"数量:"<<m_request.count<<" 批准"<<endl;
}
else
{ super->requestapplication(m_request);
}
}
};
//主管
class zhuguan:public Manger
{
public:
zhuguan(string name):Manger(name){}
void requestapplication(request m_request)
{
if(m_request.type=="请假" && m_request.count<5)
{ cout<<m_name<<": "<<m_request.content<<"数量:"<<m_request.count<<" 批准"<<endl;
}
else
{ super->requestapplication(m_request);
}
}
};
//总经理
class zongjingli:public Manger
{
public:
zongjingli(string name):Manger(name){}
void requestapplication(request m_request)
{
if(m_request.type=="请假" && m_request.count<10)
{ cout<<m_name<<": "<<m_request.content<<"数量:"<<m_request.count<<" 批准"<<endl;
}
else
{ cout<<m_name<<": "<<m_request.content<<"数量:"<<m_request.count<<" 不批准"<<endl;
}
}
};
int main()
{
request *myrequest = new request("请假" ,6,"小明请假");
jingli *m_jingli = new jingli("经理");
zhuguan*m_zhuguan = new zhuguan("主管");
zongjingli *m_zongjingli = new zongjingli("总经理");
m_jingli->setsuper(m_zhuguan);
m_zhuguan->setsuper(m_zongjingli);
m_jingli->requestapplication(*myrequest);
myrequest->count=100;
cout<<endl;
m_jingli->requestapplication(*myrequest);
myrequest->count=1;
cout<<endl;
m_jingli->requestapplication(*myrequest);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
文章来源: blog.csdn.net,作者:IM-STONE,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/doubleintfloat/article/details/86563165
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)