装饰者模式:动态地将责任附加到对象上。想要扩展功能,装饰者提供有别于继承的另一种选择。——《HEAD FIRST 设计模式》
我的c++代码:
#ifndef DESIGN_DECORATOR_H_#define DESIGN_DECORATOR_H_#includeclass Beverage{public: virtual ~Beverage(){}public: virtual std::string getDescription(); virtual int cost();protected: std::string m_descri;};class CondimentDecorator:public Beverage{public: virtual ~CondimentDecorator(){}};////class HouseBlend:public Beverage{public: HouseBlend(); virtual ~HouseBlend(){}public: virtual std::string getDescription(); virtual int cost();};class DarkRoast:public Beverage{public: DarkRoast(); virtual ~DarkRoast(){}public: virtual std::string getDescription(); virtual int cost();};class Espresso:public Beverage{public: Espresso(); virtual ~Espresso(){}public: virtual std::string getDescription(); virtual int cost();};class Decaf:public Beverage{public: Decaf(); virtual ~Decaf(){}public: virtual std::string getDescription(); virtual int cost();};////class Milk:public CondimentDecorator{public: Milk(Beverage* b); virtual ~Milk(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};class Mocha:public CondimentDecorator{public: Mocha(Beverage* b); virtual ~Mocha(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};class Soy:public CondimentDecorator{ public: Soy(Beverage* b); virtual ~Soy(){} public: virtual std::string getDescription(); virtual int cost(); private: Beverage* m_b;};class Whip:public CondimentDecorator{public: Whip(Beverage* b); virtual ~Whip(){}public: virtual std::string getDescription(); virtual int cost();private: Beverage* m_b;};#endif // DESIGN_DECORATOR_H_
#include "decorator.h"std::string Beverage::getDescription(){ return this->m_descri;}int Beverage::cost(){ return 0;}HouseBlend::HouseBlend(){ this->m_descri = "HouseBlend";}std::string HouseBlend::getDescription(){ return Beverage::getDescription();}int HouseBlend::cost(){ return 1;}DarkRoast::DarkRoast(){ this->m_descri = "DarkRoast";}std::string DarkRoast::getDescription(){ return Beverage::getDescription();}int DarkRoast::cost(){ return 2;}Espresso::Espresso(){ this->m_descri = "Espresso";}std::string Espresso::getDescription(){ return Beverage::getDescription();}int Espresso::cost(){ return 3;}Decaf::Decaf(){ this->m_descri = "Decaf";}std::string Decaf::getDescription(){ return Beverage::getDescription();}int Decaf::cost(){ return 4;}Milk::Milk(Beverage* b){ this->m_b = b;}std::string Milk::getDescription(){ return this->m_b->getDescription() + ",Milk";}int Milk::cost(){ return this->m_b->cost() + 1;}Mocha::Mocha(Beverage* b){ this->m_b = b;}std::string Mocha::getDescription(){ return this->m_b->getDescription() + ",Mocha";}int Mocha::cost(){ return this->m_b->cost() + 2;}Soy::Soy(Beverage* b){ this->m_b = b;}std::string Soy::getDescription(){ return this->m_b->getDescription() + ",Soy";}int Soy::cost(){ return this->m_b->cost() + 3;}Whip::Whip(Beverage* b){ this->m_b = b;}std::string Whip::getDescription(){ return this->m_b->getDescription() + ",Whip";}int Whip::cost(){ return this->m_b->cost() + 4;}