java设计模式(二)---装饰者模式

调用抽象类公共抽象类CondimentDecorator延伸饮料{公共抽象字符串getDescription(); }} //饮料的价钱//浓缩咖啡浓缩咖啡公共类咖啡饮料延伸(){公共浓咖啡(){描述=“浓咖啡”; }公共双成本(){返回1.99; }} //代调料公共类摩卡延伸CondimentDecorator {饮料饮料; 公共摩卡(饮料饮料){this.beverage =饮料; } public String getDescription(){return beverage.getDescription +“,Mocha”; } public double cost(){return .20 + beverage.cost(); }}

//代码测试公共类StarbuzzCoffee {公共静态无效主要(字符串ARGS []){//饮料饮料=新咖啡(); Beverage2 =新的DarkRoast(); drink2 =新摩卡(drink2); drink2 =新摩卡(drink2); 饮料2 =新鞭(饮料2); 的system.out.println(beverage2.getDescription()+“ - ”+ beverage2.cost()); }}“

猜你喜欢

转载自blog.csdn.net/qq_33628614/article/details/79474844