本篇內(nèi)容介紹了“java裝飾者模式如何使用”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)公司專注于云溪企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,成都商城網(wǎng)站開發(fā)。云溪網(wǎng)站建設(shè)公司,為云溪等地區(qū)提供建站服務(wù)。全流程按需定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
1、使用說明
(1)裝飾者模式可以帶來比繼承更加靈活的擴(kuò)展功能,使用更加方法,可以通過組合不同的裝飾者對(duì)象來獲取具有不同行為狀態(tài)的多樣化的結(jié)果。裝飾者模式比繼承更具良好的擴(kuò)展性,完美的遵循開閉原則,繼承是靜態(tài)的附加責(zé)任,裝飾者則是動(dòng)態(tài)的附加責(zé)任。
(2)裝飾類和被裝飾類可以獨(dú)立發(fā)展,不會(huì)相互耦合,裝飾模式是繼承的一個(gè)替代模式,裝飾模式可以動(dòng)態(tài)擴(kuò)展一個(gè)實(shí)現(xiàn)類的功能。
2、實(shí)例
public class HelloWorld { public static void main(String[] args) { //點(diǎn)一份炒飯 FastFood food = new FriedRice(); //花費(fèi)的價(jià)格 System.out.println(food.getDesc() + " " + food.cost() + "元"); System.out.println("========"); //點(diǎn)一份加雞蛋的炒飯 FastFood food1 = new FriedRice(); food1 = new Egg(food1); //花費(fèi)的價(jià)格 System.out.println(food1.getDesc() + " " + food1.cost() + "元"); System.out.println("========"); //點(diǎn)一份加培根的炒面 FastFood food2 = new FriedNoodles(); food2 = new Bacon(food2); //花費(fèi)的價(jià)格 System.out.println(food2.getDesc() + " " + food2.cost() + "元"); } } // 快餐抽象類 abstract class FastFood { private float price; private String desc; public FastFood() {} public FastFood(float price, String desc) { this.price = price; this.desc = desc; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } // 獲取價(jià)格 public abstract float cost(); } // 炒飯 class FriedRice extends FastFood { public FriedRice() { super(10, "炒飯"); } @Override public float cost() { return getPrice(); } } // 炒面 class FriedNoodles extends FastFood { public FriedNoodles() { super(12, "炒面"); } @Override public float cost() { return getPrice(); } } // 配料 abstract class Garnish extends FastFood { private FastFood fastFood; public FastFood getFastFood() { return fastFood; } public void setFastFood(FastFood fastFood) { this.fastFood = fastFood; } public Garnish(FastFood fastFood, float price, String desc) { super(price, desc); this.fastFood = fastFood; } } // 雞蛋配料 class Egg extends Garnish { public Egg(FastFood fastFood) { super(fastFood, 1, "雞蛋"); } @Override public float cost() { return getPrice() + getFastFood().getPrice(); } @Override public String getDesc() { return super.getDesc() + getFastFood().getDesc(); } } //培根配料 class Bacon extends Garnish { public Bacon(FastFood fastFood) { super(fastFood,2,"培根"); } @Override public float cost() { return getPrice() + getFastFood().getPrice(); } @Override public String getDesc() { return super.getDesc() + getFastFood().getDesc(); } }
“java裝飾者模式如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前名稱:java裝飾者模式如何使用
網(wǎng)站鏈接:http://muchs.cn/article14/pipcge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、全網(wǎng)營銷推廣、面包屑導(dǎo)航、軟件開發(fā)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)