设计模式-装饰者模式

装饰者模式:装饰者模式可以在运行事动态的为对象添加功能而不必改变原类文件和使用继承。
具体做法为使用一个基类Base,实现继承自基类的类class1,再实现继承自基类的class2,class2的构造方法中传入Base类型的指针。若将Base*类型指向class1的指针传入class2的构造函数,即可返回一个在class2中添加了某些功能的Base类指针对象。实现层层包围叠加的类(如图)。设计模式-装饰者模式_第1张图片
讨论模型:
实现两个继承自Base类的煎饼类(chineseHamburger)和鸡蛋饼(EggYolk)类,在运行时添加香菜(Caraway),大葱(Shallot),火腿(Ham),添加后改变煎饼和鸡蛋饼类中的cost和getDescripition方法。
Base类:

#ifndef ______Base_h
#define ______Base_h
#include "string"
class Base {
public:
    std::string descripition;//文字描述
    int price;//价格
public:
    virtual std::string getDescripition()=0;//获取文字描述
    virtual int cost()=0;//获取总价
};

#endif

EggYolk类

#ifndef ______EggYolk_h
#define ______EggYolk_h
#include "Base.h"


class EggYolk : public Base{
public:
    EggYolk(){
        descripition = "Egg Yolk";//设置文字描述
        price = 10;//设置价格
    }


    std::string getDescripition(){
        return descripition;
    }
    int cost(){
        return price;
    }
};

#endif

ChineseHamburger类

#ifndef ______ChineseHamburger_h
#define ______ChineseHamburger_h
//煎饼

#include "Base.h"
class ChineseHamburger : public Base{
public:
    ChineseHamburger(){
        descripition = "Chinese Humburger";
        price = 7;
    }


    std::string getDescripition(){
        return descripition;
    }
    int cost(){
        return price;
    }
};
#endif

继承自Base类的调料基类Seasoning类

//调料基类
#ifndef ______Seasoning_h
#define ______Seasoning_h
#include "Base.h"
class Seasoning : public Base{
public:
    Base* _base;//添加Base*指针
public:
    virtual std::string getDescripition()=0;
};

#endif

继承自Seasoning类的Ham类

#ifndef ______Ham_h
#define ______Ham_h
//火腿
#include "Seasoning.h"
#include "Base.h"
class Ham : public Seasoning{
public:
    Ham(Base* base){
        descripition = "Ham";
        price = 3;
        _base = base;//将构造方法中的Base类指针保存为成员变量
    }


    std::string getDescripition(){
        return _base->getDescripition()+", Ham";//调用_base指向的对象的getDescripition方法,然后添加上Ham类所作的改变后返回。
    }
    int cost(){
        return _base->cost()+price;//同上
    }

};

#endif

继承自Seasoning类的Shallot类

#ifndef ______Shallot_h
#define ______Shallot_h
//葱
#include "Seasoning.h"
#include "Base.h"
class Shallot : public Seasoning{
public:
    Shallot(Base* base){
        descripition = "Ham";
        price = 2;
        _base = base;
    }


    std::string getDescripition(){
        return _base->getDescripition()+", Shallot";
    }
    int cost(){
        return _base->cost()+price;
    }

};
#endif

继承自Seasoning类的Carryway类

#ifndef ______Caraway_h
#define ______Caraway_h

#include "Seasoning.h"
#include "Base.h"
class Caraway : public Seasoning{
public:
    Caraway(Base* base){
        descripition = "Ham";
        price = 1;
        _base = base;
    }


    std::string getDescripition(){
        return _base->getDescripition()+", Carway";
    }
    int cost(){
        return _base->cost()+price;
    }

};
#endif
#include <iostream>
#include "Base.h"
#include "EggYolk.h"
#include "ChineseHamburger.h"
#include "Seasoning.h"
#include "Ham.h"
#include "Shallot.h"
#include "Caraway.h"
int main(int argc, const char * argv[]) {

    Base* eggYolk = new EggYolk();//创造一个鸡蛋饼
    eggYolk = new Ham(eggYolk);//添加火腿
    eggYolk = new Caraway(eggYolk);//添加香菜
    printf("%s,cost:%d\n",eggYolk->getDescripition().c_str(),eggYolk->cost());

    Base* chineseHum = new ChineseHamburger();
    chineseHum = new Shallot(chineseHum);
    printf("%s,cost:%d\n",chineseHum->getDescripition().c_str(),chineseHum->cost());

    return 0;
}

输出:Egg Yolk, Ham, Carway,cost:14
Chinese Humburger, Shallot,cost:9

你可能感兴趣的:(设计模式)