装饰着模式(decorator)
程序员文章站
2023-12-23 21:19:33
...
一下代码就是运用了装饰者模式:
运行结果为:
运行方式为:装饰者与被装饰者都继承相同的基类,装饰者不知道其他的装饰者的存在
package com.base; public abstract class Beverage { public String description = "Unknown Beverage"; public String getDescription(){ return description; } public abstract double cost(); }
package com.base; public abstract class CondimentDecorator extends Beverage { public abstract String getDescription(); }
package com.decorated; import com.base.Beverage; public class DarkRoast extends Beverage { public DarkRoast() { description = "Dark Roast Coffee"; } public double cost() { return 0.99; } }
package com.decorated; import com.base.Beverage; public class Espresso extends Beverage { public Espresso(){ description = "Espresso"; } @Override public double cost() { return 1.99; } }
package com.decorated; import com.base.Beverage; public class HouseBlend extends Beverage { public HouseBlend(){ description = "House Blend Coddee"; } @Override public double cost() { return 0.89; } }
package com.decorator; import com.base.Beverage; import com.base.CondimentDecorator; 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.20+beverage.cost(); } }
package com.decorator; import com.base.Beverage; import com.base.CondimentDecorator; public class Soy extends CondimentDecorator { Beverage beverage; public Soy(Beverage beverage) { this.beverage = beverage; } public String getDescription() { return beverage.getDescription() + ", Soy"; } public double cost() { return 0.15 + beverage.cost(); } }
package com.decorator; import com.base.Beverage; import com.base.CondimentDecorator; public class Whip extends CondimentDecorator { Beverage beverage; public Whip(Beverage beverage) { this.beverage = beverage; } public String getDescription() { return beverage.getDescription() + ", Whip"; } public double cost() { return 0.10 + beverage.cost(); } }
package com.test; import com.base.Beverage; import com.decorated.DarkRoast; import com.decorated.Espresso; import com.decorated.HouseBlend; import com.decorator.Mocha; import com.decorator.Soy; import com.decorator.Whip; public class StarBuzzCoffee { public static void main(String args[]) { Beverage beverage = new Espresso(); System.out.println(beverage.getDescription() + " $" + beverage.cost()); Beverage beverage2 = new DarkRoast(); beverage2 = new Mocha(beverage2); beverage2 = new Mocha(beverage2); beverage2 = new Whip(beverage2); System.out.println(beverage2.getDescription() + " $" + beverage2.cost()); Beverage beverage3 = new HouseBlend(); beverage3 = new Soy(beverage3); beverage3 = new Mocha(beverage3); beverage3 = new Whip(beverage3); System.out.println(beverage3.getDescription() + " $" + beverage3.cost()); } }
运行结果为:
Espresso $1.99 Dark Roast Coffee, Mocha, Mocha, Whip $1.49 House Blend Coddee, Soy, Mocha, Whip $1.34
运行方式为:装饰者与被装饰者都继承相同的基类,装饰者不知道其他的装饰者的存在