设计模式-装饰模式

来源:互联网 发布:车载数据采集系统 编辑:程序博客网 时间:2024/05/29 09:27

网上的装饰模式学习资料有很多,下面是我的想法。

===============================================

装饰模式:

主要是一级一级向上的叠加,

一个关键的点是:第二级向下扩展第三级时,第三级返回的数要向上传递,

sum=0;sum+=2;sum+=3;有这样子的一种感觉。


//定义原蛋糕20元,奶油5元,加图案3元,加水果8元,加蜡烛4元,精美包装2元,卡片1元

//1蛋糕类class Cake{public:    virtual double addMoney() = 0;};//2_1原包装类class OriginalCake : public Cake{private:double OriginalPrice;//蛋糕原价格Cake *m_OriginalCake;public:OriginalCake(double price){OriginalPrice = price;}double addMoney(){return OriginalPrice;}//返回原价};//2_2包装class ExtraCake : public Cake{private:     Cake *m_ExtraCake;//蛋糕public:    ExtraCake(Cake *pCake) : m_ExtraCake(pCake){}    double addMoney(){        if (m_ExtraCake != NULL){            return  m_ExtraCake->addMoney();        }//如果没有额外添加,添加为0return 0;    }};//3_1加奶油class AddButter : public ExtraCake{private:double ButterPrice;//奶油价格public:    AddButter(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){this->ButterPrice=price;}    double addMoney(){return ButterPrice+ExtraCake::addMoney();    }//        奶油钱+原价void SetButterPrice(double price){this->ButterPrice=price;}//设置价格};//3_2加图案class AddPattern : public ExtraCake{private:double PatternPrice;//图案价格public:    AddPattern(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){this->PatternPrice=price;}    double addMoney(){return PatternPrice+ExtraCake::addMoney();    }//        图案钱+原价void SetPatternPrice(double price){this->PatternPrice=price;}//设置价格};//3_3加水果class AddFruit : public ExtraCake{private:double FruitPrice;//水果价格public:    AddFruit(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){this->FruitPrice=price;}    double addMoney(){        return FruitPrice+ExtraCake::addMoney();    }////        水果钱+原价void SetFruitPrice(double price){this->FruitPrice=price;}//设置价格};//3_4加蜡烛class AddCandle : public ExtraCake{private:double CandlePrice;//蜡烛价格public:    AddCandle(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){this->CandlePrice=price;}    double addMoney(){        return CandlePrice+ExtraCake::addMoney();    }////        蜡烛钱+原价void SetCandlePrice(double price){this->CandlePrice=price;}//设置价格};//3_5精美包装class AddCasing : public ExtraCake{private:double CasingPrice;//精美包装价格public:    AddCasing(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){CasingPrice=price;}    double addMoney(){        return CasingPrice+ExtraCake::addMoney();    }////        蜡烛钱+原价void SetCasingPrice(double price){this->CasingPrice=price;}//设置价格};//3_6增加卡片class AddCard : public ExtraCake{private: double CardPrice;//卡片价格public:AddCard(Cake *pExtraCake,double price) : ExtraCake(pExtraCake){CardPrice=price;}double addMoney(){return CardPrice+ExtraCake::addMoney();}////        卡片钱+原价void SetCardPrice(double price){this->CardPrice=price;}//设置价格};int main(){//定义原蛋糕20元,奶油5元,加图案3元,加水果8元,加蜡烛4元,精美包装2元,卡片1元OriginalCake* _OCake = new OriginalCake(20);cout<<"原味:"<<_OCake->addMoney()<<"元"<<endl;Cake *pCake1 = new AddButter(_OCake,5);cout<<"原味 + 奶油:"<<pCake1->addMoney()<<"元"<<endl;Cake *pCake2 = new AddButter( new AddFruit(_OCake,8),5);cout<<"原味 + 奶油 + 水果:"<<pCake2->addMoney()<<"元"<<endl;Cake *pCake3 = new AddButter( new AddFruit( new AddCandle(new AddCard(_OCake,2),4),8),5);cout<<"原味 + 奶油 + 水果 + 蜡烛 + 卡片:"<<pCake3->addMoney()<<"元"<<endl;Cake *pCake4 = new AddPattern( new AddCasing(_OCake,2),3);cout<<"原味 + 图案 + 精美包装:"<<pCake4->addMoney()<<"元"<<endl;delete _OCake; _OCake = NULL;delete pCake1; pCake1 = NULL;delete pCake2; pCake2 = NULL;delete pCake3; pCake3 = NULL;delete pCake4; pCake4 = NULL; return 0;}

//定义原蛋糕20元,奶油5元,加图案3元,加水果8元,加蜡烛4元,精美包装2元,卡片1元

1.原味,20:输出20元

2.原味 + 奶油,20+5: 输出25元

3.原味 + 奶油+ 水果,20+8+5: 输出33元

4.原味 + 奶油+ 水果 + 蜡烛 + 卡片: 输出39元

5.原味 + 图案+ 精美包装: 输出25元


0 0
原创粉丝点击