定义:动态的将责任附加到对象上。若要扩展功能,装饰着提供了比继承更有弹性的替代方法
类图
事例:
去饮品店点饮料和调料,计算所需要的价格。
定义饮料类(Component)
public abstract class Beverage {
String description = "Unkown beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
定义调料类(Decorator)
public abstract class CondimentDecorator extends Beverage{
public abstract String getDescription();
}
定义具体饮料类(ConcreteComponent)
public class Espresso extends Beverage {
public Espresso(){
description = "Espresso";
}
@Override
public double cost() {
return 1.99;
}
}
public class HouseBlend extends Beverage{
public HouseBlend(){
description = "HouseBlend";
}
@Override
public double cost() {
return 0.89;
}
}
定义调料实现类(ConcreteDecorator)
public class Mocha extends CondimentDecorator{
Beverage beverage;
public Mocha(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + ",Mocha";
}
@Override
public double cost() {
return 0.20 + this.beverage.cost();
}
}
public class Soy extends CondimentDecorator {
Beverage beverage;
public Soy(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + ", Soy";
}
@Override
public double cost() {
return 0.15 + this.beverage.cost();
}
}
public class Whip extends CondimentDecorator {
Beverage beverage;
public Whip(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + ", Whip";
}
@Override
public double cost() {
return 0.30 + this.beverage.cost();
}
}
测试类
public class Test {
public static void main(String[] args){
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription()+"$"+beverage.cost());
Beverage beverage1 = new HouseBlend();
beverage1 = new Mocha(beverage1);
beverage1 = new Soy(beverage1);
beverage1 = new Whip(beverage1);
beverage1 = new Mocha(beverage1);
System.out.println(beverage1.getDescription()+"$"+beverage1.cost());
}
}
类似的java.io使用了装饰者模式。
例如:InputStream是抽象组件,FileInputStream是一个抽象装饰者,BufferedInputStream是具体的装饰者。
缺点:利用装饰模式,常常造成设计中出现大量小类。