資源描述:
《云大設(shè)計(jì)模式實(shí)驗(yàn)3裝飾著模式》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、云南大學(xué)軟件學(xué)院實(shí)驗(yàn)報(bào)告課程:設(shè)計(jì)模式實(shí)驗(yàn)學(xué)期:2010-2011學(xué)年第一學(xué)期任課教師:專業(yè):學(xué)號(hào):姓名:成績(jī):實(shí)驗(yàn)3裝飾者模式1.題目:使用裝飾著模式實(shí)現(xiàn)一個(gè)小吃店供應(yīng)食物,然后計(jì)算費(fèi)用并輸出所點(diǎn)的食物和消費(fèi)金額的程序,其中的被裝飾者是粗米線、細(xì)米線、面條,裝飾者是炸醬、燜肉、番茄雞蛋以及醬油、醋、辣椒和蔥花。2.模式設(shè)計(jì)的UML類圖:3.程序源代碼:(1)食品抽象類FoodStuff.java:publicabstractclassFoodStuff{Stringdescription="UnknownFoodStuff";publicStringg
2、etDescription(){returndescription;}publicabstractdoublecost();}(2)被裝飾者的各個(gè)子類:publicclassThickRicenoodlesextendsFoodStuff{publicThickRicenoodles(){description="粗米線";}publicdoublecost(){return4.0;}}publicclassThinRicenoodlesextendsFoodStuff{publicThinRicenoodles(){description="細(xì)米線";
3、}publicdoublecost(){return3.0;}}publicclassNoodlesextendsFoodStuff{publicNoodles(){description="面條";}publicdoublecost(){return2.0;}}(3)調(diào)味品抽象類CondimentDecorator.java:publicabstractclassCondimentDecoratorextendsFoodStuff{publicabstractStringgetDescription();}(4)調(diào)料裝飾者的各個(gè)子類: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)佐料裝飾者的各個(gè)子類: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