【design pattern】结构型模式之—装饰者模式(Decorator)

来源:互联网 发布:男穿紧身牛仔裤知乎 编辑:程序博客网 时间:2024/06/16 10:19

Motivation

你可以通过继承,静态的扩展对象的功能(编译期);然而有时候你需要在对象被使用的时候,动态的扩展对象的功能(运行期);这个时候你就需要装饰者模式。

Intent

装饰者模式的目的是 动态地为 一个对象 增加额外的职责;

Implement

UML 图

decorator UML
解释:

Component: 被装饰对象的父类接口
ConcreteComponent:被装饰的具体实现类
Decorator:装饰者接口,包含了一个对 Component 的引用
ConcreteDecorators:具体的装饰者实现类

Examples

咖啡 和 调料 的装饰关系
Beverage.java

package com.ycit.head.first.decorator;/** * 星巴兹咖啡抽象类,添加不同的调料价格不同 * Created by xlch on 2017/5/10. */public abstract class Beverage {    public String description = "unknown Beverage";    public String getDescription() {        return description;    }    public abstract double cost();}

Espresso.java

package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;/** * 咖啡实现类:浓缩咖啡 * Created by xlch on 2017/5/10. */public class Espresso extends Beverage {    public Espresso() {        description = "Espresso";    }    @Override    public double cost() {        return 1.99;    }}

CondimentDecorator.java

package com.ycit.head.first.decorator;/** * 调料装饰器,继承 Beverage * Created by xlch on 2017/5/10. */public abstract class CondimentDecorator extends Beverage {    public abstract String getDescription();}

Mocha.java

package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;import com.ycit.head.first.decorator.CondimentDecorator;/** * mocha 调料 * Created by xlch on 2017/5/10. */public class Mocha extends CondimentDecorator {    Beverage beverage;    public Mocha(Beverage beverage) {        this.beverage = beverage;    }    @Override    public String getDescription() {        return beverage.getDescription() + ",Mocha";    }    @Override    public double cost() {        return beverage.cost() + .20;    }}

Soy.java

package com.ycit.head.first.decorator.impl;import com.ycit.head.first.decorator.Beverage;import com.ycit.head.first.decorator.CondimentDecorator;/** * soy 调料 * Created by xlch on 2017/5/10. */public class Soy extends CondimentDecorator {    Beverage beverage;    public Soy (Beverage beverage) {        this.beverage = beverage;    }    @Override    public String getDescription() {        return beverage.getDescription() + ", Soy";    }    @Override    public double cost() {        return beverage.cost() + .20;    }}

还可以有很多其他调料;

测试类:

package com.ycit.head.first.decorator.impl;/** * Created by xlch on 2017/5/10. */public class Main {    public static void main(String [] args) {        /**我需要一杯mocha 浓缩咖啡**/        Espresso espresso = new Espresso();        Mocha mocha = new Mocha(espresso);        System.out.println(mocha.getDescription());        System.out.println(mocha.cost());        /**我需要一杯mocha + soy 浓缩咖啡**/        Soy soy = new Soy(mocha);        System.out.println(soy.getDescription());        System.out.println(soy.cost());    }}

Case

Jdk 中的 输入输出流的设计利用了 装饰器模式;
其中的 FilterInputStream 相当于该处的 Decorator
具体的实现类包括:

  • BufferedInputStream
  • DataInputStream
  • PushbackInputStream
0 0