装饰器模式

2017-12-24  本文已影响0人  AlexSun1995

介绍

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

为什么引入装饰器模式?

抽象图

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


装饰器模式的通用UML视图

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

观点与例子

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

总结

上一篇 下一篇

猜你喜欢

热点阅读