黄药师及其五大弟子功夫继承关系篇----命令模式C++实现

295 阅读2分钟

学了C++基本的语法都知道继承可以让子类拥有更多的功能,除了继承还有组合,委托,也能让一个类的功能增加。设计模式,这个设计是设计继承,组合,委托,之间相互叠加的方式,让其符合业务需求。

命令模式是委托和继承的运用实例,它把一个类的函数转换成一个对象。通过多个类把一个类A作为它的委托,在各自类中调用这个类A的不同函数来实现。这样做看似有些多次一举,但如果碰到这样的需求,那这是很好的专家级的解决方案。如果我们的要调用的函数以千计,此设计模式可把这些函数转换为一个个对象,然后把这些对象放进容器里,这样我们可以像处理数据一样维护这些数据,把他们进行排序等操作。

设计模式对于刚刚学完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;
}