DesignPattern/10.DecoratorPattern/2.Code/DecoratorPattern.h

94 lines
1.5 KiB
C
Raw Normal View History

2019-10-24 16:02:47 +00:00
#ifndef __DECORATOR_PATTERN_H__
#define __DECORATOR_PATTERN_H__
//<2F><><EFBFBD>󹹼<EFBFBD>
class Component
{
public:
Component(){}
virtual ~Component(){}
2019-10-24 16:02:47 +00:00
virtual void operation() = 0;
};
//<2F><><EFBFBD><EFBFBD><E5B9B9><EFBFBD><EFBFBD>
class Phone :public Component
{
public:
Phone(){}
void operation(){
printf("<EFBFBD>ֻ<EFBFBD>\n");
}
};
//<2F><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>
class Decorator :public Component
{
public:
Decorator(){}
Decorator(Component *c){
this->component = c;
}
void operation(){
this->component->operation();
}
Component *getComponent(){
return this->component;
}
void setComponent(Component *c){
this->component = c;
}
private:
Component *component;
};
//<2F><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>
class DecoratorShell:public Decorator
{
public:
DecoratorShell(){}
DecoratorShell(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("װ<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>\n");
}
};
//<2F><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD>ֽ
class DecoratorSticker :public Decorator
{
public:
DecoratorSticker(){}
DecoratorSticker(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ֽ\n");
}
};
//<2F><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD>
class DecoratorRope :public Decorator
{
public:
DecoratorRope(){}
DecoratorRope(Component *c){
this->setComponent(c);
}
void operation(){
this->getComponent()->operation();
this->newBehavior();
}
void newBehavior(){
printf("ϵ<EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
}
};
#endif //__DECORATOR_PATTERN_H__