模式介绍

装饰者模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法完整性的前提下,提供了额外的功能。

使用场景

1、扩展一个类的功能。
2、动态增加功能,动态撤销。

系统实现

/**
 * 饮料抽象类
 */
public abstract class Drink {
    public String name;
    private float price = 0.0f;

    public abstract float cost();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }
}
/**
 * 咖啡大类
 */
public class Coffee extends Drink {
    @Override
    public float cost() {
        return super.getPrice();
    }
}
/**
 * 意大利咖啡
 */
public class Espresso extends Coffee{
    public Espresso(){
        setName("意大利咖啡");
        setPrice(12.0f);
    }
}
/**
 * 美式咖啡
 */
public class LongBlack extends Coffee{
    public LongBlack(){
        setName("美式咖啡");
        setPrice(15.0f);
    }
}
/**
 * 调味大类
 */
public class Decorator extends Drink {
    private Drink drink;

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

    @Override
    public float cost() {
        return super.getPrice()+drink.cost();
    }
}
/**
 * 牛奶调味品
 */
public class Milk extends Decorator{
    public Milk(Drink drink) {
        super(drink);
        setName("牛奶调味品");
        setPrice(3.0f);
    }
}
/**
 * 巧克力调味品
 */
public class Chocolate extends Decorator {
    public Chocolate(Drink drink) {
        super(drink);
        setName("巧克力调味品");
        setPrice(3.0f);
    }
}
/**
 * 客户端
 */
public class Client {
    public static void main(String args[]){
        // 意大利咖啡
        Drink espressoCoffee = new Espresso();
        // 加一份牛奶
        espressoCoffee = new Milk(espressoCoffee);
        // 加一份巧克力
        espressoCoffee = new Chocolate(espressoCoffee);
        System.out.println("订单需要:"+espressoCoffee.cost()+"块钱!");
    }
}
结果:
订单需要:18.0块钱!

相关文章:

  • 2021-12-28
  • 2021-05-28
  • 2021-06-12
  • 2021-12-04
  • 2021-05-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-12-23
  • 2021-09-09
  • 2021-12-11
相关资源
相似解决方案