学了C++基本的语法都知道继承可以让子类拥有更多的功能,除了继承还有组合,委托,也能让一个类的功能增加。设计模式,这个设计是设计继承,组合,委托,之间相互叠加的方式,让其符合业务需求。
责任链模式是继承 +
委托的运用。在以下代码中,定义一个抽象管理者类,派生经理类,总监类,总经理类。有些决定经理可以做,如果经理不能做,那么调用其委托的总监来做,若总监也不能做,则调用其委托的总经理来做。这些代码都是在学习这些的过程中码的。。。。。
上一篇:武侠小说之令狐冲篇–适配器模式C++
下一篇:黄药师及其五大弟子功夫继承关系篇----命令模式C++实现
上代码,亲测有效!
Exe : Chain.o
g++ -o Exe Chain.o
main.o : Chain.cpp
g++ -c -g Chain.cpp
clean :
rm Chain
#include <iostream>
#include <string>
using namespace std;
//责任链模式
class Abstract_Manage
{
public:
virtual void deal_with(string worker_name, int money) = 0;
Abstract_Manage* p_Abstract_Manage = NULL;
string Manage_Name;
};
class Km : public Abstract_Manage
{
public:
Km(Abstract_Manage* p_Abstract_Manage, string Manage_Name);
void deal_with(string worker_name, int money);
};
Km::Km(Abstract_Manage* p_Abstract_Manage, string this_Manage_Name)
{
this->p_Abstract_Manage = p_Abstract_Manage;
this->Manage_Name = this_Manage_Name;
}
void Km::deal_with(string worker_name, int money)
{
if(money < 10000)
{
cout << this->Manage_Name << "同意" << worker_name << "加薪" << money << "元" << endl;
}
else
{
cout << "Let me tell you a story !" << endl;
}
}
class M : public Abstract_Manage
{
public:
M(Abstract_Manage* p_Abstract_Manage, string Manage_Name);
void deal_with(string worker_name, int money);
};
M::M(Abstract_Manage* p_Abstract_Manage, string this_Manage_Name)
{
this->p_Abstract_Manage = p_Abstract_Manage;
this->Manage_Name = this_Manage_Name;
}
void M::deal_with(string worker_name, int money)
{
if(money < 5000)
{
cout << this->Manage_Name << "同意" << worker_name << "加薪" << money << "元" << endl;
}
else
{
p_Abstract_Manage->deal_with(worker_name, money);
}
}
class Dm : public Abstract_Manage
{
public:
Dm(Abstract_Manage* p_Abstract_Manage, string Manage_Name);
void deal_with(string worker_name, int money);
};
Dm::Dm(Abstract_Manage* p_Abstract_Manage, string this_Manage_Name)
{
this->p_Abstract_Manage = p_Abstract_Manage;
this->Manage_Name = this_Manage_Name;
}
void Dm::deal_with(string worker_name, int money)
{
if(money < 2000)
{
cout << this->Manage_Name << "同意" << worker_name << "加薪" << money << "元" << endl;
}
else
{
p_Abstract_Manage->deal_with(worker_name, money);
}
}
int main(void)
{
Km* p_Km = new Km( NULL, "裘千尺");
M* p_M = new M(p_Km, "裘千仞");
Dm* p_Dm = new Dm(p_M, "裘千丈");
p_Dm->deal_with("杨过", 60000);
delete p_Dm;
delete p_M;
delete p_Km;
return 0;
}
哎呦喂ヾ(✿゚▽゚)ノ~路长馆小,雪轻帘薄,酒热乎,这位爷~您ヾ(✿゚▽゚)ノ~ 里面坐~
本公众号专注分享C++,ffmpeg,opencv等相关音视频知识
webrtc,udp,tcp,rtsp,rtmp,srt/nginx+rtmp等流媒体协议和服务器
同时也会有大厂音视频技术专家不定期直播分享…
国人开发流媒体srs服务器,及yangrtc(国人版的webrtc)协议新动向
偶尔分享下程序员梦呓碎碎念(๑•॒̀ ູ॒•́๑)啦啦啦
目前刚刚开通,接受读者的优质投稿…
鉴于国内音视频圈子小,起步晚,以致分享少,门槛高,特开通分享,一起扇动这阵风吧!
微信扫描下方二维码,关注公众号,赶快进入音视频开发者社区吧!
![](https://img-blog.csdnimg.cn/32ebb60e13f9449496052c247d2ccf65.png)
![](https://img-blog.csdnimg.cn/bfd8a35f631f44059d8d1d228a22d54b.jpg)
![](https://img-blog.csdnimg.cn/1b3e3fc19f5f4837a6514769eabf4e10.png)