2019-10-24 16:02:47 +00:00
|
|
|
|
#ifndef __DECORATOR_PATTERN_H__
|
|
|
|
|
#define __DECORATOR_PATTERN_H__
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 抽象构件
|
2019-10-24 16:02:47 +00:00
|
|
|
|
class Component
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Component(){}
|
2021-04-04 13:09:08 +00:00
|
|
|
|
virtual ~Component(){}
|
2019-10-24 16:02:47 +00:00
|
|
|
|
virtual void operation() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 具体构件
|
2019-10-24 16:02:47 +00:00
|
|
|
|
class Phone :public Component
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Phone(){}
|
|
|
|
|
void operation(){
|
2021-10-28 15:15:51 +00:00
|
|
|
|
printf("<EFBFBD>ֻ<EFBFBD>\n");
|
2019-10-24 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 抽象装饰类
|
2019-10-24 16:02:47 +00:00
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 具体装饰类:手机壳
|
2019-10-24 16:02:47 +00:00
|
|
|
|
class DecoratorShell:public Decorator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DecoratorShell(){}
|
|
|
|
|
DecoratorShell(Component *c){
|
|
|
|
|
this->setComponent(c);
|
|
|
|
|
}
|
|
|
|
|
void operation(){
|
|
|
|
|
this->getComponent()->operation();
|
|
|
|
|
this->newBehavior();
|
|
|
|
|
}
|
|
|
|
|
void newBehavior(){
|
2021-10-28 15:15:51 +00:00
|
|
|
|
printf("安装手机壳\n");
|
2019-10-24 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
|
|
|
|
|
// 具体装饰类:手机贴纸
|
2019-10-24 16:02:47 +00:00
|
|
|
|
class DecoratorSticker :public Decorator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DecoratorSticker(){}
|
|
|
|
|
DecoratorSticker(Component *c){
|
|
|
|
|
this->setComponent(c);
|
|
|
|
|
}
|
|
|
|
|
void operation(){
|
|
|
|
|
this->getComponent()->operation();
|
|
|
|
|
this->newBehavior();
|
|
|
|
|
}
|
|
|
|
|
void newBehavior(){
|
2021-10-28 15:15:51 +00:00
|
|
|
|
printf("贴卡通贴纸ֽ\n");
|
2019-10-24 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-28 15:15:51 +00:00
|
|
|
|
// 具体装饰类:挂绳
|
2019-10-24 16:02:47 +00:00
|
|
|
|
class DecoratorRope :public Decorator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
DecoratorRope(){}
|
|
|
|
|
DecoratorRope(Component *c){
|
|
|
|
|
this->setComponent(c);
|
|
|
|
|
}
|
|
|
|
|
void operation(){
|
|
|
|
|
this->getComponent()->operation();
|
|
|
|
|
this->newBehavior();
|
|
|
|
|
}
|
|
|
|
|
void newBehavior(){
|
2021-10-28 15:15:51 +00:00
|
|
|
|
printf("系手机挂绳\n");
|
2019-10-24 16:02:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
#endif //__DECORATOR_PATTERN_H__
|