public abstract class Beverage { protected String Description; public String getDescription(){ return Description; } public abstract int cost(); } public class Espresso extends Beverage{ public int cost(){ return 2; } public Espresso(){ Description = "Espresso"; } } abstract class CondimentDecorator extends Beverage{ public abstract String getDescription(); } public class Mocha extends CondimentDecorator{ Beverage beverage; public Mocha(Beverage beverage){ this.beverage=beverage; } @Override public String getDescription() { return beverage.getDescription()+", Mocha "; } @Override public int cost() { return beverage.cost()+0.5; } public Beverage remove(Beverage b) { return b; } } ...
有更多的像牛奶一样..大豆..等,并像HouseBlend咖啡..等装饰..如何从对象中移除装饰器?
,如果我有对象的摩卡牛奶装饰,我想只删除“摩卡”装饰。
Beverage beverage = new Mocha(new Espresso());
beverage = new Milk(beverage);
编辑: 的情况是
客户已将快报与摩卡咖啡和牛奶。
现在Expresso装饰摩卡和牛奶。
突然客户想用鞭子取代摩卡。
我理解它可能只是一个例子,有想法,但在特定的设置,我可能不会使用一个装饰,而是整合(例如,使用enum类),因为无论装饰器的顺序如何(例如,Milk&Mocha与Mocha&Milk相同),您可能都希望保持Description的一致性。 – didierc 2014-11-03 12:44:56