Decorator(装饰模式)

#ifndef COMPONENT_H
#define COMPONENT_H


class Component
{
    public:
        /** Default constructor */
        Component();
        /** Default destructor */
        virtual ~Component();
        virtual void Operation()=0;
    protected:
    private:
};

#endif // COMPONENT_H

#include "Component.h"

Component::Component()
{
    //ctor
}

Component::~Component()
{
    //dtor
}

#ifndef CONCRETECOMPONENT_H
#define CONCRETECOMPONENT_H

#include "Component.h"


class ConcreteComponent : public Component
{
    public:
        /** Default constructor */
        ConcreteComponent();
        /** Default destructor */
        virtual ~ConcreteComponent();
        void Operation();
    protected:
    private:
};

#endif // CONCRETECOMPONENT_H

#include "ConcreteComponent.h"
#include <iostream>
using namespace std;

ConcreteComponent::ConcreteComponent()
{
    cout<<"生产了一个IPhone手机"<<endl;
}

ConcreteComponent::~ConcreteComponent()
{
    cout<<"回收了一个IPhone手机"<<endl;
}

void ConcreteComponent::Operation()
{
    cout<<"IPhone"<<endl;
}

#ifndef CONCRETEDECORATORA_H
#define CONCRETEDECORATORA_H

#include "Decorator.h"


class ConcreteDecoratorA : public Decorator
{
    public:
        /** Default constructor */
        ConcreteDecoratorA(Component* pCom);
        /** Default destructor */
        virtual ~ConcreteDecoratorA();
        void Operation();
        void AddBeHavior();
    protected:
    private:
};

#endif // CONCRETEDECORATORA_H

#include "ConcreteDecoratorA.h"
#include <iostream>
using namespace std;

ConcreteDecoratorA::ConcreteDecoratorA(Component* pCom):Decorator(pCom)
{
    cout<<"创建了一个装饰类A"<<endl;
}

ConcreteDecoratorA::~ConcreteDecoratorA()
{
    cout<<"销毁了一个装饰类A"<<endl;
}

void ConcreteDecoratorA::Operation()
{
    Decorator::Operation();
    AddBeHavior();
}

void ConcreteDecoratorA::AddBeHavior()
{
    cout<<"给IPhone添加行为"<<endl;
}

#ifndef CONCRETEDECORATORB_H
#define CONCRETEDECORATORB_H

#include "Decorator.h"
#include <iostream>
#include <string>
using namespace std;

class ConcreteDecoratorB : public Decorator
{
    public:
        /** Default constructor */
        ConcreteDecoratorB(Component* pCom);
        /** Default destructor */
        virtual ~ConcreteDecoratorB();
        void Operation();
    protected:
    private:
    string m_AddedState;
};

#endif // CONCRETEDECORATORB_H

#include "ConcreteDecoratorB.h"
#include <iostream>
using namespace std;

ConcreteDecoratorB::ConcreteDecoratorB(Component* pCom):Decorator(pCom)
{
    cout<<"创建了一个装饰类B"<<endl;
    m_AddedState="给IPhone添加了一个状态";
}

ConcreteDecoratorB::~ConcreteDecoratorB()
{
    cout<<"销毁了一个装饰类B"<<endl;
}

void ConcreteDecoratorB::Operation()
{
    Decorator::Operation();
    cout<<m_AddedState<<endl;
}

#include <iostream>

using namespace std;
#include "Component.h"
#include "ConcreteDecoratorA.h"
#include "ConcreteDecoratorB.h"
#include "ConcreteComponent.h"

int main()
{
    Component* m_pIPhone=new ConcreteComponent();
    Component* m_pDa=new ConcreteDecoratorA(m_pIPhone);
    Component* m_pDb=new ConcreteDecoratorB(m_pDa);
    m_pDb->Operation();
    delete m_pDb;
    delete m_pDa;
    delete m_pIPhone;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值