var code = "282dc6f5-e025-4185-9118-e8fdbb208860"
装饰模式
装饰( Decorator )模式又叫做包装模式。通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案。 装饰模式就是把要添加的附加功能分别放在单独的类中,并让这个类包含它要装饰的对象,当需要执行时,客户端就可以有选择地、按顺序地使用装饰功能包装对象。
装饰模式可以动态的给一个对象添加一些额外的职责。就增加功能来说,此模式比生成子类更为灵活,通过子类继承的方式,但是如果后续继续增加功能的话,便要继续继承现有的类,如此就会使继承的层次越来越深,不利与代码的维护和可读性。故最好的方式便是通过装饰者模式来完成。
装饰模式实现步骤
1.提供一个抽象组件类:抽象被装饰者的行为
2.提供一个或多个具体组件类:被装饰者的行为具体实现
3.提供一个抽象装饰器类:抽象组件指针与抽象组件一致接口
4.提供一个具体的装饰器类:为具体组件附加责任
//1. 抽象组件类,修饰被装饰者的行为
class AbstractSubject
{
public:
virtual void show() = 0;
string str;
};
//2. 具体组件类,实现被装饰的行为
class User :public AbstractSubject
{
public:
User()
{
this->str = "英雄初始化 ";
}
void show()override
{
cout << this->str << endl;
}
};
//3. 抽象装饰器
class AbstractDecrote :public AbstractSubject
{
public:
virtual void show() = 0;
protected:
AbstractSubject* pBase;//具有一个抽象组件指针
};
//4. 具体的装饰器类:为具体的组件添加责任
//装饰器1
class DecroteUser1 :public AbstractDecrote
{
public:
DecroteUser1(AbstractSubject* user)
{
this->pBase = user;//为抽象组件添加指定的具体组件
}
void show()override
{
this->str = this->pBase->str + "英雄之铠甲";
cout << this->str << endl;
}
};
//装饰器2
class DecroteUser2 :public AbstractDecrote
{
public:
DecroteUser2(AbstractSubject* user)
{
this->pBase = user;//为抽象组件添加指定的具体组件
}
void show()override
{
this->str = this->pBase->str + "英雄之护膝";
cout << this->str << endl;
}
};
int main()
{
User* me = new User;
me->show();
//穿铠甲
DecroteUser1* pme1 = new DecroteUser1(me);
pme1->show();
//穿护膝
DecroteUser2* pme2 = new DecroteUser2(me);
pme2->show();
//传铠甲和护膝
DecroteUser2* pme3 = new DecroteUser2(pme1);
pme3->show();
delete me;
delete pme1;
delete pme2;
delete pme3;
return 0;
}
..............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
优点
- 易于扩展对象功能
- 通过装饰类的排列组合,可创造出很多不同行为的组合
缺点
- 会出现更多的代码,更多的类,增加程序复杂性