美文网首页
装饰器模式

装饰器模式

作者: AlexSun1995 | 来源:发表于2017-12-24 18:49 被阅读0次

介绍

python装饰器学习 这篇文章中,介绍了python 中的装饰器,python内置了对装饰器的支持。面向对象的装饰器模式和python内置装饰器的性质相同,都是在不改变原有代码的情况下实现功能的扩展。
本文以java代码为例,学习面向对象设计模式中的装饰器模式

为什么引入装饰器模式?

抽象图

简单的说装饰器是对原对象的一层包装,实现的方法是组合。我们向组合后的decorator发送消息时,传递给的也是外层的包装器,经过包装器内部的处理以后,最后也由包装器传递出来。


装饰器模式的通用UML视图

在这张图中,将组建和包装器也进行了分层,分为:

  • 抽象组建
  • 具体组建
  • 抽象包装器
  • 具体包装器
    这样做的好处是,让包装器和组建都同时具备了更强的灵活性。抽象包装器让创造一个新的具体包装器更加容易,抽象组建让创建一个新的具体组建更加灵活。


    pizza 例子中的装饰器模式

观点与例子

  • 不使用继承来共享方法,使用组合来共享方法
    实现这个UML图
    Drink interface
package design.decorator_patten;

public interface Drink {
    String getDescription();
    float cost();
}

Coffee

package design.decorator_patten;

public class Coffee implements Drink {
    @Override
    public String getDescription() {
        return "a cup of coffee";
    }

    @Override
    public float cost() {
        float cost = (float) 2.11;
        return cost;
    }
}
package design.decorator_patten;

/**
 * 包含调料信息的Drink装饰器
 * 对Drink 增加了一层包装,加上了调料的信息
 */
public class WithCondimentDecorator implements Drink {
    private Drink drink;

    public WithCondimentDecorator(Drink drink) {
        this.drink = drink;
    }

    @Override
    public String getDescription() {
        return drink.getDescription();
    }

    @Override
    public float cost() {
        return drink.cost();
    }
}

withMilkDecorator 实例化的装饰器,调料为milk

package design.decorator_patten;

public class withMilkDecorator extends WithCondimentDecorator {
    @Override
    public String getDescription() {
        return super.getDescription() + " with milk added!";
    }

    @Override
    public float cost() {
        return (float) (super.cost() + 4.0);
    }

    public withMilkDecorator(Drink drink) {
        super(drink);
    }
}

withSugarDecorator 实例化的装饰器,调料为milk

package design.decorator_patten;

public class withSugarDecorator extends WithCondimentDecorator {

    public withSugarDecorator(Drink drink) {
        super(drink);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + " with sugar added! ";
    }

    @Override
    public float cost() {
        return (float) (super.cost() + 3.0);
    }
}

测试类:

package design.decorator_patten;

public class Test {
    public static void main(String args[]){
        Drink drink = new Coffee();
        System.out.println(drink.getDescription() + " cost: " + drink.cost());
        withMilkDecorator milkDecorator = new withMilkDecorator(drink);
        System.out.println(milkDecorator.getDescription() + " cost: " + milkDecorator.cost());
        withSugarDecorator sugarDecorator = new withSugarDecorator(drink);
        System.out.println(sugarDecorator.getDescription() + "cost: " + sugarDecorator.cost());

        // 装饰器本身也是Drink类型的, 可以使用同一个Drink,同时add milk 和 sugar
        drink = new withMilkDecorator(drink);
        drink = new withSugarDecorator(drink);
        System.out.println(drink.getDescription() + " cost: " + drink.cost());
    }
}
结果:
a cup of coffee cost: 2.11
a cup of coffee with milk added! cost: 6.1099997
a cup of coffee with sugar added! cost: 5.1099997
a cup of coffee with milk added! with sugar added!  cost: 9.11

总结

  • 在我们的例子中,所有的装饰器实体都是继承自抽象装饰器类,抽象装饰器类又是实现了Drink的接口,所以从本质上说他们都是Drink类型的,这样做究竟是否必要?
  • 装饰器模式和组合模式有什么共同点?装饰器模式是组合模式的应用
  • 一句话总结装饰器模式:不同于python中的方法装饰器,java中的装饰器一般都是类装饰器。包装器通过组合类型为组建的对象,就可以在包装器中操作组建对象。但是包装器可以同时实现抽象组建的接口,这样的包装器类事实就和组建是同一个类型的了,这就为多层包装提供了很多便利。

相关文章

网友评论

      本文标题:装饰器模式

      本文链接:https://www.haomeiwen.com/subject/wothgxtx.html