<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 310, comments - 6939, trackbacks - 0, articles - 3
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
     

    Head First設(shè)計(jì)模式】-Decorator模式

     

    一、要完成的任務(wù)

    星巴茲(Starbuzz)是以擴(kuò)張速度最快而聞名的咖啡連鎖店。如果你在街角看到它的店,在對面街上肯定還會看到另一家。因?yàn)閿U(kuò)張速度實(shí)在太快了,他們準(zhǔn)備更新訂單系統(tǒng),以合乎他們的飲料供應(yīng)要求。他們原先的類設(shè)計(jì)是這樣的……

     

    購買咖啡時(shí),也可以要求在其中加入各種調(diào)料,例如:蒸奶(Steamed Milk)、豆?jié){(Soy)、摩卡(Mocha,也就是巧克力風(fēng)味)或覆蓋奶泡。星巴茲會根據(jù)所加入的調(diào)料收取不同的費(fèi)用。所以訂單系統(tǒng)必須考慮到這些調(diào)料部分。

     

    二、Decorator模式

     

    1、一個(gè)原則

     

    類應(yīng)該對擴(kuò)展開放,對修改關(guān)閉

     

    2、定義裝飾者模式

    裝飾者模式動態(tài)地將責(zé)任附加到對象上。若要擴(kuò)展功能,裝飾者提供了比繼承更有彈性的替代方案。

       

    3.分析任務(wù)





    4.設(shè)計(jì)任務(wù)


     

     

    三、代碼實(shí)現(xiàn)

     

    1.定義抽象類

     

    1)飲料抽象類Beverage

    Beverage.java

     
    package com.sterning.ch3_decorator;

    /*
     * Beverage是一個(gè)抽象類,有兩個(gè)方法
     
    */

    public abstract class Beverage {
            
    public String description="Unknown Beverage";
            
            
    /*
             * getDescription()已經(jīng)在此實(shí)現(xiàn)了,但是cost()必須在子類中實(shí)現(xiàn)
             
    */

            
    public String getDescription() {
                
    return description;
            }

            
            
    public abstract double cost();
    }

    2)調(diào)料抽象類CondimentDecorator

    CondimentDecorator.java

    package com.sterning.ch3_decorator;

    /*
     * 首先,必須讓CondimentDecorator能夠取代Beverage,所以將CondimentDecorator擴(kuò)展自Beverage類
     
    */

    public abstract class CondimentDecorator extends Beverage {

        
    //所有的調(diào)料裝飾者都必須重新實(shí)現(xiàn)getDescription()方法.
        public abstract String getDescription();
    }

     

    2.飲料實(shí)現(xiàn)

     

    1Espresso

    Espresso.java

    package com.sterning.ch3_decorator;

    /*
     * 首先,必須讓CondimentDecorator能夠取代Beverage,所以將CondimentDecorator擴(kuò)展自Beverage類
     
    */

    public abstract class CondimentDecorator extends Beverage {

        
    //所有的調(diào)料裝飾者都必須重新實(shí)現(xiàn)getDescription()方法.
        public abstract String getDescription();
    }


     

    2HouseBlend

    HouseBlend.java

    package com.sterning.ch3_decorator.drink;

    import com.sterning.ch3_decorator.Beverage;

    public class HouseBlend extends Beverage {
        
        
        
    public HouseBlend() {
            description
    ="House Blend Coffee";
        }


        @Override
        
    public double cost() {
            
    return 0.89;
        }


    }

     

    3DarkRoast

    DarkRoast.java

    package com.sterning.ch3_decorator.drink;

    import com.sterning.ch3_decorator.Beverage;

    public class DarkRoast extends Beverage {
        
        
        
    public DarkRoast() {
            description
    ="Dark Roast Coffee";
        }


        @Override
        
    public double cost() {
            
    return 0.99;
        }

    }

     

    4Decaf

    Decaf.java

    package com.sterning.ch3_decorator.drink;

    import com.sterning.ch3_decorator.Beverage;

    public class Decaf extends Beverage {
        
        
        
    public Decaf() {
            description
    ="Decaf Coffee";
        }


        @Override
        
    public double cost() {
            
    return 1.05;
        }

    }


     

    3.調(diào)料實(shí)現(xiàn)

    1Mocha

    Mocha.java

    package com.sterning.ch3_decorator.condiment;

    import com.sterning.ch3_decorator.Beverage;
    import com.sterning.ch3_decorator.CondimentDecorator;

    public class Mocha extends CondimentDecorator {
        
    /*
         * 要讓Mocha能夠引用一個(gè)Beverage,做法如下:一是用一個(gè)實(shí)例變量記錄飲料,也就是被裝飾者.
         * 二是想辦法讓裝飾者(飲料)記錄到實(shí)例變量中,即把飲料當(dāng)作構(gòu)造器的參數(shù),再由構(gòu)造器將此飲料記錄在實(shí)例變量中
         
    */

        Beverage beverage;

        
    public Mocha(Beverage beverage) {
            
    this.beverage = beverage;
        }


        @Override
        
    public String getDescription() {
            
    /*
             * 我們希望敘述不只是描述飲料,而是完整的連調(diào)料都描述出來
             
    */

            
    return beverage.getDescription()+",Mocha";
        }

                
        @Override
        
    public double cost() {
            
    /*
             * 要計(jì)算帶Mocha飲料的價(jià)錢,首先把調(diào)用委托給裝飾對象,以計(jì)算價(jià)錢,然后再加上Mocha的價(jià)錢,得到最后結(jié)果
             
    */

            
    return 0.20+beverage.cost();
        }

    }


     

    2Soy

    Soy.java

    package com.sterning.ch3_decorator.condiment;

    import com.sterning.ch3_decorator.Beverage;
    import com.sterning.ch3_decorator.CondimentDecorator;

    public class Soy extends CondimentDecorator {
        Beverage beverage;

        
    public Soy(Beverage beverage) {
            
    this.beverage = beverage;
        }


        
    public String getDescription() {
            
    return beverage.getDescription() + ", Soy";
        }


        
    public double cost() {
            
    return .15 + beverage.cost();
        }

    }


     

    3Whip

    Whip.java

    package com.sterning.ch3_decorator.condiment;

    import com.sterning.ch3_decorator.Beverage;
    import com.sterning.ch3_decorator.CondimentDecorator;
     
    public class Whip extends CondimentDecorator {
        Beverage beverage;
     
        
    public Whip(Beverage beverage) {
            
    this.beverage = beverage;
        }

     
        
    public String getDescription() {
            
    return beverage.getDescription() + ", Whip";
        }

     
        
    public double cost() {
            
    return .10 + beverage.cost();
        }

    }


     

    4.測試類StarbuzzCoffee

    StarbuzzCoffee.java

    package com.sterning.ch3_decorator;

    import com.sterning.ch3_decorator.condiment.Mocha;
    import com.sterning.ch3_decorator.condiment.Soy;
    import com.sterning.ch3_decorator.condiment.Whip;
    import com.sterning.ch3_decorator.drink.DarkRoast;
    import com.sterning.ch3_decorator.drink.Espresso;
    import com.sterning.ch3_decorator.drink.HouseBlend;

    public class StarbuzzCoffee {
        
    public static void main(String args[]){
            
    /*
             * 訂一杯Espresso,不需要調(diào)料,打印出它的描述和價(jià)錢.
             
    */

            Beverage beverage
    =new Espresso();
            System.out.println(beverage.getDescription()
    +" $"+beverage.cost());
            
            
    /*
             * 制造一個(gè)DarkRoast對象,用Mocha,Whip裝飾它
             
    */

            Beverage beverage2
    =new DarkRoast();
            beverage2
    =new Mocha(beverage2);
            beverage2
    =new Mocha(beverage2);
            beverage2
    =new Whip(beverage2);
            System.out.println(beverage2.getDescription()
    +" $"+beverage2.cost());    
            
            
    /*
             * 最后,再來一杯調(diào)料為豆?jié){,摩卡\奶泡的HouseBlend咖啡
             
    */

            Beverage beverage3
    =new HouseBlend();
            beverage3
    =new Soy(beverage3);
            beverage3
    =new Mocha(beverage3);
            beverage3
    =new Whip(beverage3);
            System.out.println(beverage3.getDescription()
    +" $"+beverage3.cost());    
        }

    }


    源代碼下載:ch3_decorator.rar

    評論

    # re: 【Head First設(shè)計(jì)模式】-Decorator模式  回復(fù)  更多評論   

    2008-08-01 11:32 by xue
    好,好好

    # re: 【Head First設(shè)計(jì)模式】-Decorator模式  回復(fù)  更多評論   

    2008-08-11 12:08 by zx
    (1)Espresso 代碼貼錯(cuò)了
    主站蜘蛛池模板: 四虎亚洲国产成人久久精品| 亚洲免费网站在线观看| 国产成人免费片在线观看| 亚洲最大黄色网站| 亚洲成人免费网站| 亚洲国产美女精品久久| 亚洲美女视频免费| 在线综合亚洲中文精品| 免费看片A级毛片免费看| 亚洲国产精品无码中文lv| 日日夜夜精品免费视频| 黄页网址在线免费观看| www.亚洲精品| 成全视频免费观看在线看| 亚洲尹人九九大色香蕉网站| 成人浮力影院免费看| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 亚洲五月六月丁香激情| 最近最新高清免费中文字幕| 亚洲一区二区三区播放在线| 成年人免费视频观看| 美景之屋4在线未删减免费 | 999国内精品永久免费视频| 亚洲日本VA中文字幕久久道具| 国产三级免费电影| 国产一级在线免费观看| 亚洲欧洲日产v特级毛片| 国产片免费在线观看| 3344在线看片免费| 激情五月亚洲色图| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 亚洲精品无码久久久久久久| 国产精品免费观看调教网| 亚洲高清日韩精品第一区| 日韩电影免费在线| 三年片免费高清版| 亚洲色丰满少妇高潮18p| 亚洲男人第一无码aⅴ网站| 91短视频在线免费观看| 精品在线观看免费| 亚洲视频在线观看网址|