咖啡订单项目:
问题解析:
这里的milk、soy、chocolate都是Boolean类型
问题解析:
装饰(Decorator)模式中的角色:
类图展示:
抽象类Drink:
public abstract class Drink { private String description; private double price; public abstract double cost(); public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
装饰者:
public class Decorator extends Drink { private Drink drink; public Decorator(Drink drink) { this.drink = drink; } @Override public double cost() { return super.getPrice() + drink.cost(); } @Override public String getDescription() { return super.getDescription() + " " + getPrice() + " && " + drink.getDescription(); } }
Coffee:
public class Coffee extends Drink { @Override public double cost() { return super.getPrice(); } }
Coffee子类:
public class Decaf extends Coffee { public Decaf() { setDescription("无因咖啡"); setPrice(1); } } public class Espresso extends Coffee { public Espresso() { setDescription("意大利咖啡"); setPrice(6); } } public class LongBlack extends Coffee { public LongBlack() { setDescription("longBlack"); setPrice(5); } } public class ShortBlack extends Coffee { public ShortBlack() { setDescription("shortBlack"); setPrice(4); } }
装饰器子类:
public class Soy extends Decorator { public Soy(Drink drink) { super(drink); setDescription("豆浆"); setPrice(1.5); } } public class Milk extends Decorator{ public Milk(Drink drink) { super(drink); setDescription("牛奶"); setPrice(2); } } public class Chocolate extends Decorator { public Chocolate(Drink drink) { super(drink); setDescription("巧克力"); setPrice(3); } }
Client调用:
public class Client { public static void main(String[] args) { // 两份巧克力+一份牛奶Longblack Drink order1 = new LongBlack(); System.out.println("order1费用:" + order1.cost()); System.out.println("order1描述:" + order1.getDescription()); order1 = new Milk(order1); System.out.println("order1加一份牛奶费用:" + order1.cost()); System.out.println("order1加一份牛奶描述:" + order1.getDescription()); order1 = new Chocolate(order1); System.out.println("order1加一份牛奶,加一份巧克力费用:" + order1.cost()); System.out.println("order1加一份牛奶,加一份巧克力描述:" + order1.getDescription()); order1 = new Chocolate(order1); System.out.println("order1加一份牛奶,加两份巧克力费用:" + order1.cost()); System.out.println("order1加一份牛奶,加两份巧克力描述:" + order1.getDescription()); System.out.println("=============================="); Drink order2 = new Decaf(); System.out.println("无因咖啡费用:" + order2.cost()); System.out.println("无因咖啡描述:" + order2.getDescription()); order2 = new Milk(order2); System.out.println("无因咖啡,加一份牛奶费用:" + order2.cost()); System.out.println("无因咖啡,加一份牛奶描述:" + order2.getDescription()); } }
结果打印:
order1费用:5.0 order1描述:longBlack order1加一份牛奶费用:7.0 order1加一份牛奶描述:牛奶 2.0 && longBlack order1加一份牛奶,加一份巧克力费用:10.0 order1加一份牛奶,加一份巧克力描述:巧克力 3.0 && 牛奶 2.0 && longBlack order1加一份牛奶,加两份巧克力费用:13.0 order1加一份牛奶,加两份巧克力描述:巧克力 3.0 && 巧克力 3.0 && 牛奶 2.0 && longBlack ============================== 无因咖啡费用:1.0 无因咖啡描述:无因咖啡 无因咖啡,加一份牛奶费用:3.0 无因咖啡,加一份牛奶描述:牛奶 2.0 && 无因咖啡