学了C++基本的语法都知道继承可以让子类拥有更多的功能,除了继承还有组合,委托,也能让一个类的功能增加。设计模式,这个设计是设计继承,组合,委托,之间相互叠加的方式,让其符合业务需求。
命令模式是委托和继承的运用实例,它把一个类的函数转换成一个对象。通过多个类把一个类A作为它的委托,在各自类中调用这个类A的不同函数来实现。这样做看似有些多次一举,但如果碰到这样的需求,那这是很好的专家级的解决方案。如果我们的要调用的函数以千计,此设计模式可把这些函数转换为一个个对象,然后把这些对象放进容器里,这样我们可以像处理数据一样维护这些数据,把他们进行排序等操作。
设计模式对于刚刚学完C++语法的同学来说,还是比较难啃的一块,本系列通过<<神雕侠侣>>, <<射雕英雄传>>等武侠小说;<<龙珠>>,<<火影忍者>>动漫等方式,通过师徒,门派关系,功夫继承来阐明设计模式类之间关系,继承方式等。
上一篇:裘千尺裘千仞裘千丈与杨过篇–责任链模式C++实现
下一篇:孙猴子装饰七十二变篇–装饰器模式C++实现
这些代码都是在学习这些的过程中码的。。。。。
上代码,亲测有效!
Exe : Command.o
g++ -o Exe Command.o
main.o : Command.cpp
g++ -c -g Command.cpp
rm :
Command
#include <iostream>
#include <string>
#include <queue>
using namespace std;
//命令模式
//黄药师
class Doctor_H
{
public:
//半步<<九阴真经>> 含摧心掌,九阴白骨爪 梅超风,陈玄风
void up_book();
//劈空掌 曲灵风
void break_from_air();
//奇门遁甲 陆乘风
void everything_from_nothing();
//锻造 冯默风
void smelt_from_iron();
private:
//碧海潮生曲
void voice_from_sea();
//弹指神通
void power_from_finger();
};
void Doctor_H::up_book()
{
cout << "摧心掌_九阴白骨爪" << endl;
}
void Doctor_H::break_from_air()
{
cout << "劈空掌" << endl;
}
void Doctor_H::everything_from_nothing()
{
cout << "奇门遁甲" << endl;
}
void Doctor_H::smelt_from_iron()
{
cout << "锻造" << endl;
}
class Apprentice
{
public:
virtual void show() = 0;
Doctor_H* p_Doctor_H = NULL;
};
class Fe_Cu : public Apprentice
{
public:
Fe_Cu(Doctor_H* p_Doctor_H);
void show();
};
Fe_Cu::Fe_Cu(Doctor_H* p_Doctor_H)
{
this->p_Doctor_H = p_Doctor_H;
}
void Fe_Cu::show()
{
p_Doctor_H->up_book();
}
class QLF : public Apprentice
{
public:
QLF(Doctor_H* p_Doctor_H);
void show();
};
QLF::QLF(Doctor_H* p_Doctor_H)
{
this->p_Doctor_H = p_Doctor_H;
}
void QLF::show()
{
p_Doctor_H->break_from_air();
}
class LCF : public Apprentice
{
public:
LCF(Doctor_H* p_Doctor_H);
void show();
};
LCF::LCF(Doctor_H* p_Doctor_H)
{
this->p_Doctor_H = p_Doctor_H;
}
void LCF::show()
{
p_Doctor_H->everything_from_nothing();
}
class FMF : public Apprentice
{
public:
FMF(Doctor_H* p_Doctor_H);
void show();
};
FMF::FMF(Doctor_H* p_Doctor_H)
{
this->p_Doctor_H = p_Doctor_H;
}
void FMF::show()
{
p_Doctor_H->smelt_from_iron();
}
class World
{
public:
queue<Apprentice*> que_p_App;
void add_Apprentice(Apprentice* p_Apprentice);
void show();
};
void World::add_Apprentice(Apprentice* p_Apprentice)
{
que_p_App.push(p_Apprentice);
}
void World::show()
{
while(que_p_App.empty() != true)
{
que_p_App.front()->show(); //queue的front指最先插入的一个
que_p_App.pop();
}
}
int main(void)
{
Doctor_H* p_Doctor_H = new Doctor_H;
Fe_Cu* p_Fe_Cu = new Fe_Cu(p_Doctor_H);
QLF* p_QLF = new QLF(p_Doctor_H);
LCF* p_LCF = new LCF(p_Doctor_H);
FMF* p_FMF = new FMF(p_Doctor_H);
World* p_World = new World;
p_World->add_Apprentice(p_Fe_Cu);
p_World->add_Apprentice(p_QLF);
p_World->add_Apprentice(p_LCF);
p_World->add_Apprentice(p_FMF);
p_World->show();
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)