Decorate 模式

作用:

装饰器模式有助于将其他行为或责任动态附加到对象,装饰器为扩展功能提供了子类化的灵活替代方案。这也被称为包装。如果您的应用程序进行某种过滤,那么装饰器可能是考虑这项工作的良好模式。

#include <iostream>
#include <string>
using namespace std;

class Car
{
protected:
    string _str;
public:
    Car()
    {
    _str = "Unknown Car";
    }
    virtual string getDescription()
    {
        return _str;
    }
    virtual double getCost() = 0;
    virtual ~Car()
    {
        cout<<"~Car \n";
    }
};

class OptionsDecorator:public Car
{
public:
    virtual string getDescription() = 0;
    virtual ~OptionsDecorator()
    {
        cout<<"~OptionsDecorator"<<endl;
    }
};

class CarModel1:public Car
{
public:
    CarModel1()
    {
        _str = "CarModel1";
    }

    virtual double getCost()
    {
        return 31000.23;
    }
    ~CarModel1()
    {
    cout<<"CarModel1\n";
    }
};

class Navigation:public OptionsDecorator
{
   Car *_b;
public:
    Navigation(Car *b)
    {
        _b = b;
    }

    string getDescription()
    {
        return _b->getDescription() + ",Navigation";
    }

    double getCost()
    {
        return _b->getCost() + 300;
    }

    ~Navigation()
    {
        cout<<"~Navigation"<<endl;
        delete _b;
    }
};

class PremiumSoundSystem: public OptionsDecorator
{
                Car *_b;
        public:
                PremiumSoundSystem(Car *b)
                {
                        _b = b;
                }
                string getDescription()
                {
                        return _b->getDescription() + ", PremiumSoundSystem";
                }

                double getCost()
                {
                        return 0.30 + _b->getCost();
                }
                ~PremiumSoundSystem()
                {
                        cout << "~PremiumSoundSystem()\n";
                        delete _b;
                }
};

class ManualTransmission: public OptionsDecorator
{
                Car *_b;
        public:
                ManualTransmission(Car *b)
                {
                        _b = b;
                }
                string getDescription()
                {
		        return _b->getDescription()+ ", ManualTransmission";
                }

                double getCost()
                {
                        return 0.30 + _b->getCost();
                }
                ~ManualTransmission()
                {
                        cout << "~ManualTransmission()\n";
                        delete _b;
                }
};

int main()
{
    //Create our Car that we want to buy
        Car *b = new CarModel1();

        cout << "Base model of " << b->getDescription() << " costs $" << b->getCost() << "\n";

        //Who wants base model let's add some more features

        b = new Navigation(b);
        cout << b->getDescription() << " will cost you $" << b->getCost() << "\n";
        b = new PremiumSoundSystem(b);
        b = new ManualTransmission(b);
        cout << b->getDescription() << " will cost you $" << b->getCost() << "\n";

        // WARNING! Here we leak the CarModel1, Navigation and PremiumSoundSystem objects!
        // Either we delete them explicitly or rewrite the Decorators to take
        // ownership and delete their Cars when destroyed.
        delete b;

        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值