1、定义
装饰者模式动态地将责任附加到对象上。如果要扩展功能,装饰者提供了比继承更有弹性的替代方案。
2、UML类图
装饰者模式.png
角色说明
- Component:抽象组件,接口或抽象类,被装饰者。
- ConcreteComponent:具体组件
- Decorator:装饰者抽象类,继承被装饰者的同时关联被装饰者(持有引用),同时添加自己的行为
- ConcreteDecorator:具体装饰类。
3、模式演进
以星巴克为例,有多种类型咖啡,特浓咖啡、混合咖啡、烘焙咖啡等。这里创建一个咖啡的基类,包含description与cost属性。每种类型的咖啡可能会添加多种不同的调料,比如牛奶、摩卡、泡沫....
//基类
abstract class Beverage {
private String description;
public String getDescription() {
return description;
}
public Beverage(String description) {
this.description = description;
}
abstract public double cost();
}
class Espresso extends Beverage{
public Espresso() {
super("特浓咖啡");
}
@Override
public double cost() {
return 25;
}
}
class HouseBlend extends Beverage{
public HouseBlend() {
super("混合咖啡");
}
@Override
public double cost() {
return 35;
}
}
class DarkRoast extends Beverage{
public DarkRoast() {
super("烘焙咖啡");
}
@Override
public double cost() {
return 28;
}
}
演进一
如果为第种类型的不同口味的咖啡都创建一个子类,可能会引起类爆炸(加1份奶,1份摩卡,2份奶......),对于维护来说是一种恶梦。
演进二
使用实例变量和继承来追踪这些调料,cost不再是抽象方法
abstract class Beverage {
private boolean milk,mocha,whip;//牛奶,摩卡,泡沫
private String description;
public Beverage(String description) {
this.description = description;
}
public boolean isMilk() {
return milk;
}
public void setMilk(boolean milk) {
this.milk = milk;
}
public boolean isMocha() {
return mocha;
}
public void setMocha(boolean mocha) {
this.mocha = mocha;
}
public boolean isWhip() {
return whip;
}
public void setWhip(boolean whip) {
this.whip = whip;
}
public String getDescription() {
if(isMilk()) {
description += " 牛奶";
}
if(isMocha()) {
description += " 摩卡";
}
if(isWhip()) {
description += " 泡沫";
}
return description;
}
public double cost() {
double total = 0;
if(isMilk()) {
total += 2;
}
if(isMocha()) {
total += 4;
}
if(isWhip()) {
total += 3.5;
}
return total;
};
}
class Espresso extends Beverage {
public Espresso() {
super("特浓咖啡");
}
@Override
public double cost() {
return 25 + super.cost();
}
}
class HouseBlend extends Beverage {
public HouseBlend() {
super("混合咖啡");
}
@Override
public double cost() {
return 35 + super.cost();
}
}
class DarkRoast extends Beverage {
public DarkRoast() {
super("烘焙咖啡");
}
@Override
public double cost() {
return 28 + super.cost();
}
}
客户端测试
Beverage b = new HouseBlend();
b.setMilk(true);
System.out.println(b.getDescription() + ":" + b.cost());
Beverage b2 = new Espresso();
b2.setMocha(true);
System.out.println(b2.getDescription() + ":"+ b2.cost());
Beverage b3 = new DarkRoast();
b3.setWhip(true);
System.out.println(b3.getDescription() + ":"+ b3.cost());
输出结果
混合咖啡 牛奶:37.0
特浓咖啡 摩卡:29.0
烘焙咖啡 泡沫:31.5
- 优点:
- 类不会爆炸
- 增加一种类型的咖啡时,不影响以前的代码,符合开闭原则
class Decaf extends Beverage{
public Decaf() {
super("低咖啡因咖啡");
}
@Override
public double cost() {
return 29 + super.cost();
}
}
- 缺点:
1、增加一种新的调料时,需要修改Beverage的cost方法,不符合开闭原则
2、如果想要加2份牛奶,貌似比较麻烦
演进三 装饰者模式
以咖啡为主体,在运行时以调料来“装饰”咖啡。比方说,顾客想要摩卡和牛奶烘焙咖啡。那么要做的是
- 拿一个烘焙咖啡对象(DarkRoast);
- 以摩卡对象(Mocha)装饰它,变成摩卡烘焙咖啡,加上摩卡的价格;
- 以牛奶对象(Milk)装饰摩卡烘焙咖啡,变成摩卡和牛奶烘焙咖啡,加上牛奶的价格;
如果要双份牛奶,就进行下一步
- 以牛奶对象(Milk)装饰牛奶和摩卡烘焙咖啡,变成摩卡和牛奶和牛奶烘焙咖啡,再加上一份牛奶的价格;
通过以上步骤分析:
- 摩卡和牛奶是“装饰者”,烘焙咖啡就是“被装饰者”;
- 装饰完的对象可以继续装饰,说明装饰者与被装饰者的类型是一致的;
- 装饰者可以在被装饰者的行为基础上添加自己的行为(例如在烘焙咖啡基础加上摩卡的价格),这就需要装饰者持有被装饰者的引用
- 可以运行时动态地装饰对象
现在,让我们来设计星巴克咖啡
- 设计装饰者调料抽象类Condiment
- 继承被装饰者Beverage
- 关联Beverage
- 实现具体装饰者类(牛奶、摩卡.....),并添加自己的行为
abstract class Condiment extends Beverage{
protected Beverage beverage;
public Condiment(Beverage beverage) {
super("调料");
this.beverage = beverage;
}
}
class Milk extends Condiment {
public Milk(Beverage beverage) {
super(beverage);
}
@Override
public double cost() {
return beverage.cost() + 2;
}
@Override
public String getDescription() {
return beverage.getDescription() + "牛奶";
}
}
class Mocha extends Condiment {
public Mocha(Beverage beverage) {
super(beverage);
}
@Override
public double cost() {
return beverage.cost() + 4;
}
@Override
public String getDescription() {
return beverage.getDescription() + "摩卡";
}
}
最终UML类图为
星巴克咖啡.png
客户端测试
public static void main(String[] args) {
Beverage b = new HouseBlend();
Beverage b2 = new Milk(b);//加牛奶
Beverage b3 = new Mocha(b2);//加摩卡
Beverage b4 = new Mocha(b3);//加摩卡
System.out.println(b4.getDescription() + ":" + b4.cost());
}
输出结果
混合咖啡牛奶摩卡摩卡:45.0
4、装饰者模式说明
优点:
- 用组合可以实现运行时动态扩展,比继承的编译时静态决定要灵活
- 当增加新的装饰时,可以不改动现有的代码,符合开闭原则
缺点:
- 引入装饰者会增加大量的小类,导致设计不容易被理解
其他:
- 装饰者Codiment与被装饰者Beverage并不是is-a的关系,这里用继承的目的是为了达到类型匹配,而不是利用继承获得行为,行为是通过组合进行动态添加的。
5、Android中的装饰者模式
5.1 Context
Context在Android开发中表示“上下文”。Context类是一个抽象类,具体实现在ContexImpl中。继承关系如下
Context继承结构.png
- 其中ContextWarpper继承Context并持有Context的引用,这里对应模式中的装饰者,Context为被装饰者。
public class ContextWrapper extends Context {
Context mBase;
public ContextWrapper(Context base) {
mBase = base;
}
//........
}
- Application、Service、Activity为具体装饰类。
5.2 Drawable
Drawable也是Android开发中经常用到的一个概念。是一个”可绘制东西“的抽象。用来做绘制相关的操作。跟View不同样,Drawable 不能接受任何事件以及用户交互。
Drawable继承关系.png
- DrawableWrapper为抽象装饰者,继承Drawable的同时持有Drawable类型的引用。
- ShapeDrawable、BitmapDrawable、LayerDrawable都是我们开发中用过具体实现类,只不过我们一般通过XML方式来定义。
- ClipDrawable、InsertDrawable、RotateDrawable、ScaleDrawable为具体装饰者。可实现一些额外的装饰效果。比如ClipDrwable可实现自身裁剪复制显示功能。