1、类图ide
2、示例代码测试
类图:this
代码:spa
一、饮料类code
/** * 饮料类 */ public abstract class Beverage { public String description = "Unknown Beverage"; public String getDescription(){ return description; } public abstract double cost(); }
二、具体饮料类对象
public class Espresso extends Beverage { public Espresso() { description = "Espresso"; } @Override public double cost() { return 1.99; } } public class HouseBlend extends Beverage { public HouseBlend() { description = "House Blend Coffee"; } @Override public double cost() { return 0.89; } }
三、调料 类,必须让CondimentDecorator可以取代Beverage,因此将CondimentDecorator扩展自Beverageblog
/** * 调料类 */ public abstract class CondimentDecorator extends Beverage{ public abstract String getDescription(); }
四、具体调料类ip
public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage) { this.beverage = beverage; } @Override public String getDescription() { return beverage.getDescription() + ", Mocha"; } @Override public double cost() { return 0.2 + beverage.cost(); } } public class Soy extends CondimentDecorator { Beverage beverage; public Soy(Beverage beverage) { this.beverage = beverage; } @Override public String getDescription() { return beverage.getDescription() + ", Soy"; } @Override public double cost() { return 0.3 + beverage.cost(); } } public class Whip extends CondimentDecorator { Beverage beverage; public Whip(Beverage beverage) { this.beverage = beverage; } @Override public String getDescription() { return beverage.getDescription() + ", Whip"; } @Override public double cost() { return 0.4 + beverage.cost(); } }
五、订单测试类get
/** * 咖啡供应类 */ public class StarbuzzCoffee { public static void main(String[] args) { //订一杯Espresso,不须要调料 Beverage beverage = new Espresso(); System.out.println(beverage.getDescription() + " $" + beverage.cost()); //来一杯调料为豆浆、摩卡、奶泡的HouseBlend咖啡 Beverage beverage1 = new HouseBlend(); beverage1 = new Soy(beverage1); beverage1 = new Mocha(beverage1); beverage1 = new Whip(beverage1); System.out.println(beverage1.getDescription() + " $" + beverage1.cost()); } }
3、使用场景io
须要扩展一个类的功能,或给一个类增长附加功能。
须要动态地给一个对象增长功能,这些功能能够再动态地撤销。
须要为一批的兄弟类进行改装或加装功能,固然是首选装饰模式。