海门做网站,wordpress cms主题教程,二级分销佣金分配表,团购网站模板装饰器模式是一种结构型设计模式#xff0c;它允许动态地将责任附加到对象上。装饰器模式是通过创建一个包装对象#xff0c;也就是装饰器#xff0c;来包裹真实对象#xff0c;从而实现对真实对象的功能增强。装饰器模式可以在不修改原有对象的情况下#xff0c;动态地添…装饰器模式是一种结构型设计模式它允许动态地将责任附加到对象上。装饰器模式是通过创建一个包装对象也就是装饰器来包裹真实对象从而实现对真实对象的功能增强。装饰器模式可以在不修改原有对象的情况下动态地添加功能。 装饰器模式通常由以下几个组件组成 1.Component组件接口定义了一个对象接口可以给这些对象动态地添加职责。 2.ConcreteComponent具体组件实现了组件接口是被装饰的对象。 3.Decorator装饰器抽象类持有一个指向组件对象的引用并实现了组件接口。通常是一个抽象类用于定义装饰器的基本行为。 4.ConcreteDecorator具体装饰器扩展了装饰器抽象类实现了具体的装饰功能。 开始上代码 首先我们定义一个咖啡
// 组件接口
interface Coffee {double cost();String getDescription();
}咖啡分很多种类我们先来一个浓缩咖啡
// 具体组件 - 浓缩咖啡
class Espresso implements Coffee {Overridepublic double cost() {return 1.5;}Overridepublic String getDescription() {return Espresso;}
}然后我觉得浓缩咖啡有点苦所以我想加点糖和牛奶 所以这里我们要开始上装饰器了
// 装饰器抽象类
abstract class CoffeeDecorator implements Coffee {protected Coffee coffee;public CoffeeDecorator(Coffee coffee) {this.coffee coffee;}Overridepublic double cost() {return coffee.cost();}Overridepublic String getDescription() {return coffee.getDescription();}
}加奶的装饰器
// 具体装饰器 - 加牛奶
class Milk extends CoffeeDecorator {public Milk(Coffee coffee) {super(coffee);}Overridepublic double cost() {return super.cost() 0.5;}Overridepublic String getDescription() {return super.getDescription() , Milk;}
}加糖的装饰器
// 具体装饰器 - 加糖
class Sugar extends CoffeeDecorator {public Sugar(Coffee coffee) {super(coffee);}Overridepublic double cost() {return super.cost() 0.2;}Overridepublic String getDescription() {return super.getDescription() , Sugar;}
}然后测试一下
public class Test {public static void main(String[] args) {// 点一杯浓缩咖啡Coffee espresso new Espresso();System.out.println(Espresso: espresso.getDescription() - Cost: $ espresso.cost());// 加牛奶Coffee espressoWithMilk new Milk(espresso);System.out.println(Espresso with Milk: espressoWithMilk.getDescription() - Cost: $ espressoWithMilk.cost());// 加糖Coffee espressoWithSugar new Sugar(espresso);System.out.println(Espresso with Sugar: espressoWithSugar.getDescription() - Cost: $ espressoWithSugar.cost());// 加牛奶和糖Coffee espressoWithMilkAndSugar new Sugar(new Milk(espresso));System.out.println(Espresso with Milk and Sugar: espressoWithMilkAndSugar.getDescription() - Cost: $ espressoWithMilkAndSugar.cost());}
}运行结果 可以发现这个模式很好理解看起来也比较实用的样子