#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;
}