Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现

来源:互联网 发布:stm8l15x中文数据手册 编辑:程序博客网 时间:2024/05/16 15:39

装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方式

设计原则:类应该对扩展开放,对修改关闭


类图:


 C++ 代码如下:

源码下载:http://download.csdn.net/detail/lingyunjinzhu/5184135

#ifndef BEVERAGE_H#define BEVERAGE_H#include<string>class Beverage{    public:        Beverage();        virtual ~Beverage();        virtual std::string getDescription();        virtual double cost(){ return 0;};        std::string description;    protected:    private:      // std::string description;};#endif // BEVERAGE_H#include "Beverage.h"#include <string>Beverage::Beverage():description("Unknow name"){}Beverage::~Beverage(){}std::string Beverage::getDescription(){    return description;}

#ifndef HOUSEBLEND_H#define HOUSEBLEND_H#include "Beverage.h"class HouseBlend : public Beverage{    public:        HouseBlend();        ~HouseBlend();        double cost();    protected:    private:};#endif // HOUSEBLEND_H#include "HouseBlend.h"HouseBlend::HouseBlend(){    description="House Blend Coffee";}HouseBlend::~HouseBlend(){    //dtor}double HouseBlend::cost(){    return 0.89;}

#ifndef DARKROAST_H#define DARKROAST_H#include "Beverage.h"class DarkRoast : public Beverage{    public:        DarkRoast();        ~DarkRoast();        double cost();    protected:    private:};#endif // DARKROAST_H#include "DarkRoast.h"#include<string>using namespace std;DarkRoast::DarkRoast(){    description= "DarkRoast";}DarkRoast::~DarkRoast(){    //dtor}double DarkRoast::cost(){    return 0.99;}

#ifndef ESPRESSO_H#define ESPRESSO_H#include "Beverage.h"class Espresso : public Beverage{    public:        Espresso();        ~Espresso();        double cost();    protected:    private:};#endif // ESPRESSO_H#include "Espresso.h"Espresso::Espresso(){    description="Espresso";}Espresso::~Espresso(){    //dtor}double Espresso::cost(){    return 1.99;}

#ifndef DECAT_H#define DECAT_H#include "Beverage.h"class Decat : public Beverage{    public:        Decat();        ~Decat();        double cost();    protected:    private:};#endif // DECAT_H#include "Decat.h"Decat::Decat(){    description="Decat";}Decat::~Decat(){    //dtor}double Decat::cost(){    return 1.05;}

#ifndef CONDIMENTDECORATOR_H#define CONDIMENTDECORATOR_H#include"Beverage.h"class CondimentDecorator: public Beverage{    public:        CondimentDecorator();        virtual ~CondimentDecorator();        virtual std::string getDescription(){return "";};    protected:    private:};#endif // CONDIMENTDECORATOR_H#include "CondimentDecorator.h"CondimentDecorator::CondimentDecorator(){    //ctor}CondimentDecorator::~CondimentDecorator(){    //dtor}

#ifndef MILK_H#define MILK_H#include "CondimentDecorator.h"class Milk : public CondimentDecorator{    public:        Milk(Beverage *beverage);        ~Milk();        std::string getDescription();        double cost();    protected:    private:        Beverage *beverage;};#endif // MILE_H#include "Milk.h"Milk::Milk(Beverage *beverage){    this->beverage=beverage;}Milk::~Milk(){    //dtor}std::string Milk::getDescription(){    return beverage->getDescription()+", Milk";}double Milk::cost(){    return beverage->cost()+0.10;}

#ifndef MOCHA_H#define MOCHA_H#include "CondimentDecorator.h"class Mocha : public CondimentDecorator{    public:        Mocha(Beverage *beverage);        ~Mocha();        std::string getDescription();        double cost();    protected:    private:        Beverage *beverage;};#endif // MOCHA_H#include "Mocha.h"Mocha::Mocha(Beverage *beverage){    this->beverage=beverage;}Mocha::~Mocha(){    //dtor}double Mocha::cost(){    return 0.20+beverage->cost();}std::string Mocha::getDescription(){    return beverage->getDescription() +", Mocha";}

#ifndef SOY_H#define SOY_H#include "CondimentDecorator.h"class Soy : public CondimentDecorator{    public:        Soy(Beverage *beverage);        ~Soy();        std::string getDescription();        double cost();    protected:    private:        Beverage *beverage;};#endif // SOY_H#include "Soy.h"Soy::Soy(Beverage *beverage){    this->beverage=beverage;}Soy::~Soy(){    //dtor}std::string Soy::getDescription(){    return beverage->getDescription()+", Spy";}double Soy::cost(){    return 0.15+beverage->cost();}

#ifndef WHIP_H#define WHIP_H#include "CondimentDecorator.h"class Whip : public CondimentDecorator{    public:        Whip(Beverage *beverage);        ~Whip();        std::string getDescription();        double cost();    protected:    private:        Beverage *beverage;};#endif // WHIP_H#include "Whip.h"Whip::Whip(Beverage *beverage){    this->beverage=beverage;}Whip::~Whip(){    //dtor}std::string Whip::getDescription(){    return beverage->getDescription()+", Whip";}double Whip::cost(){    return 0.10+beverage->cost();}

测试的主函数:


#include "Beverage.h"#include "CondimentDecorator.h"#include "DarkRoast.h"#include "Decat.h"#include "Espresso.h"#include "HouseBlend.h"#include "Milk.h"#include "Mocha.h"#include "Soy.h"#include "Whip.h"#include <iostream>#include <string>using namespace std;int main(){    Beverage *beverage = new Espresso();    cout<<beverage->getDescription()<<" $"<<beverage->cost()<<endl;    Beverage *beverage2 =new DarkRoast();    beverage2 = new Mocha(beverage2);    beverage2 = new Mocha(beverage2);    beverage2 = new Whip(beverage2);    cout<<beverage2->getDescription()<<" $"<<beverage2->cost()<<endl;    Beverage *beverage3 = new HouseBlend();    beverage3 = new Soy(beverage3);    beverage3 = new Mocha(beverage3);    beverage3 = new Whip(beverage3);    cout<<beverage3->getDescription()<<" $"<<beverage3->cost()<<endl;return 0;} 

测试结果:


作者原创,转载请注明出处http://blog.csdn.net/lingyunjinzhu/article/details/8722884


原创粉丝点击