畅游设计模式之装饰者模式

索引链接:畅游设计模式

需求

  • 实现咖啡点餐

UML类图

请添加图片描述

代码实现

Drink
@Data
public abstract class Drink {

    public String des;//描述
    private float price = 0.0f;

    //计算费用的抽象方法:子类来实现
    public abstract float cost();
}
Chocolate/Milk/Soy
//具体的 Decorator, 这里就是调味品
public class Chocolate extends Decorator {

    public Chocolate(Drink obj) {
        super(obj);
        setDes(" Chocolate ");
        setPrice(3.0f); // 调味品 的价格
    }

}

public class Milk extends Decorator {
    public Milk(Drink obj) {
        super(obj);
        setDes(" Milk ");
        setPrice(2.0f);
    }

}

public class Soy extends Decorator {

    public Soy(Drink obj) {
        super(obj);
        setDes(" Soy ");
        setPrice(1.5f);
    }


}
Coffee
public class Coffee extends Drink {
    @Override
    public float cost() {
        return super.getPrice();
    }
}
Decorator
public class Decorator extends Drink {

    private Drink obj;

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

    @Override
    public float cost() {
        //getPrice是自己的价格
        return super.getPrice() + obj.cost();
    }

    @Override
    public String getDes() {
        return super.getDes() + " " + super.getPrice() + "&&" + obj.getDes();
    }
}
具体的咖啡实现
public class ShortBlack extends Coffee {
    public ShortBlack() {
        setDes(" ShortBlack ");
        setPrice(4.0f);
    }
}

public class LongBlack extends Coffee {

    public LongBlack() {
        setDes(" Longblack ");
        setPrice(5.0f);
    }
}

public class Espresso extends Coffee {
    public Espresso() {
        setDes(" Espresso ");
        setPrice(6.0f);
    }
}

public class DeCaf extends Coffee {


    public DeCaf() {
        setDes(" 无因咖啡 ");
        setPrice(1.0f);
    }
}

CoffeeBar
public class CoffeeBar {


    public static void main(String[] args) {
        // 装饰者模式下的订单:2 份巧克力+一份牛奶的 LongBlack
        // 1.  点一份 LongBlack
        Drink order = new LongBlack();
        System.out.println("费用 1=" + order.cost());
        System.out.println("描述=" + order.getDes());

        // 2. order 加入一份牛奶
        order = new Milk(order);

        System.out.println("order 加入一份牛奶 费用 =" + order.cost());
        System.out.println("order 加入一份牛奶 描述 = " + order.getDes());

        // 3. order 加入一份巧克力
        order = new Chocolate(order);
        System.out.println("order 加入一份牛奶  加入一份巧克力  费 用 =" + order.cost());
        System.out.println("order 加入一份牛奶 加入一份巧克力 描述 = " + order.getDes());
        // 3. order 加入一份巧克力
        order = new Chocolate(order);

        System.out.println("order 加入一份牛奶  加入 2 份巧克力    费 用 =" + order.cost());
        System.out.println("order 加入一份牛奶 加入 2 份巧克力 描述 = " + order.getDes());


        System.out.println("===========================");
        Drink order2 = new DeCaf();
        System.out.println("order2 无因咖啡    费 用 =" + order2.cost());
        System.out.println("order2 无因咖啡 描述 = " + order2.getDes());
        order2 = new Milk(order2);
        System.out.println("order2 无因咖啡  加入一份牛奶    费 用 =" + order2.cost());
        System.out.println("order2 无因咖啡 加入一份牛奶 描述 = " + order2.getDes());
    }


}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值