欢迎来到天天文库
浏览记录
ID:40586209
大小:291.00 KB
页数:15页
时间:2019-08-04
《云大设计模式实验3装饰着模式》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、云南大学软件学院实验报告课程:设计模式实验学期:2010-2011学年第一学期任课教师:专业:学号:姓名:成绩:实验3装饰者模式1.题目:使用装饰着模式实现一个小吃店供应食物,然后计算费用并输出所点的食物和消费金额的程序,其中的被装饰者是粗米线、细米线、面条,装饰者是炸酱、焖肉、番茄鸡蛋以及酱油、醋、辣椒和葱花。2.模式设计的UML类图:3.程序源代码:(1)食品抽象类FoodStuff.java:publicabstractclassFoodStuff{Stringdescription="UnknownFoodStuff";publicStringg
2、etDescription(){returndescription;}publicabstractdoublecost();}(2)被装饰者的各个子类:publicclassThickRicenoodlesextendsFoodStuff{publicThickRicenoodles(){description="粗米线";}publicdoublecost(){return4.0;}}publicclassThinRicenoodlesextendsFoodStuff{publicThinRicenoodles(){description="细米线";
3、}publicdoublecost(){return3.0;}}publicclassNoodlesextendsFoodStuff{publicNoodles(){description="面条";}publicdoublecost(){return2.0;}}(3)调味品抽象类CondimentDecorator.java:publicabstractclassCondimentDecoratorextendsFoodStuff{publicabstractStringgetDescription();}(4)调料装饰者的各个子类:publiccla
4、ssFiredbeansauceextendsCondimentDecorator{FoodStufffoodstuff;publicFiredbeansauce(FoodStufffoodstuff){this.foodstuff=foodstuff;}publicStringgetDescription(){returnfoodstuff.getDescription()+",炸酱";}publicdoublecost(){return2.0+foodstuff.cost();}}publicclassDaubeextendsCondimentDec
5、orator{FoodStufffoodstuff;publicDaube(FoodStufffoodstuff){this.foodstuff=foodstuff;}publicStringgetDescription(){returnfoodstuff.getDescription()+",焖肉";}publicdoublecost(){return2.0+foodstuff.cost();}}publicclassTomatoAndEggextendsCondimentDecorator{FoodStufffoodstuff;publicTomat
6、oAndEgg(FoodStufffoodstuff){this.foodstuff=foodstuff;}publicStringgetDescription(){returnfoodstuff.getDescription()+",番茄鸡蛋";}publicdoublecost(){return2.0+foodstuff.cost();}}(5)佐料装饰者的各个子类:publicclassSoyextendsCondimentDecorator{FoodStufffoodstuff;publicSoy(FoodStufffoodstuff){this
7、.foodstuff=foodstuff;}publicStringgetDescription(){returnfoodstuff.getDescription()+",酱油";}publicdoublecost(){return0.0+foodstuff.cost();}}publicclassVinegarextendsCondimentDecorator{FoodStufffoodstuff;publicVinegar(FoodStufffoodstuff){this.foodstuff=foodstuff;}publicStringgetDes
8、cription(){returnfoodstuff.getDescriptio
此文档下载收益归作者所有