10 装饰者模式
10.1 概述
在不改变现有对象结构的前提下,动态的给对象增加一些额外功能的模式。
10.2 功能
抽象构件角色:定义一个抽象接口以规范准备接收附加责任的对象。
具体构件角色:实现抽象构件,通过装饰角色为其添加一些职责。
抽象装饰角色:继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
具体装饰角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
10.3 实现
10.3.1 UML图
10.3.2 代码
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class FastFood {
private:
string desc;
public:
void setDesc(string desc) {
this->desc = desc;
}
virtual string getDesc() {
return this->desc;
}
virtual float cost() = 0;
};
class FriedRice : public FastFood {
public:
FriedRice() {
setDesc("炒饭");
}
float cost () {
return 10;
}
};
class FriedNoodle : public FastFood {
public:
FriedNoodle() {
setDesc("炒面");
}
float cost() {
return 15;
}
};
class Garnish : public FastFood {
protected:
FastFood* fastFood;
public:
virtual float cost() = 0;
virtual string getDesc() {
return fastFood->getDesc();
}
};
class Egg : public Garnish {
public:
Egg(FastFood* fastFood) {
this->fastFood = fastFood;
}
string getDesc() {
return "鸡蛋" + fastFood->getDesc();
}
float cost() {
return this->fastFood->cost() + 2;
}
};
class Sausage : public Garnish {
public:
Sausage(FastFood* fastFood) {
this->fastFood = fastFood;
}
string getDesc() {
return "香肠" + fastFood->getDesc();
}
float cost() {
return this->fastFood->cost() + 3;
}
};
int main() {
FastFood* fastFood = new FriedRice();
fastFood = new Egg(fastFood);
cout << fastFood->getDesc() <<' ' << fastFood->cost() << endl;
fastFood = new Sausage(fastFood);
cout << fastFood->getDesc() << ' ' << fastFood->cost() << endl;
return 0;
}
10.4 优点
装饰者模式可以带来比继承更加灵活性的扩展功能,使用更加方便,可以通过组合不同的装饰者对象来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美遵循开闭原则,继承是静态的附加责任,装饰者则是动态的附加责任。
装饰者和被装饰者可以独立发展,不会相互耦合,装饰者是继承者的一个替代模式装饰者可以动态扩展一个实现类的功能。
10.5 使用场景
1.当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。
不能采用继承的情况:
系统中存在大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸式增长。
类定义不能继承时(final类)。
2.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
3.当对象功能要求动态添加,也可以再动态撤销时。