国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java設計模式之裝飾模式詳解

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:松下一田 欄目: 編程語(yǔ)言

目錄

    一、裝飾模式引入例子

    一個(gè)快餐店計算價(jià)格問(wèn)題舉例:

    快餐店有炒面、炒飯這些快餐,可以額外附加雞蛋、火腿、培根這些配菜,加配菜需要額外加錢(qián),并且每個(gè)配菜的價(jià)錢(qián)不一樣,計算快餐價(jià)格如何實(shí)現?

    1.1 一般設計

    1.2 使用繼承方式的一般設計存在的問(wèn)題

    橫向擴展性不好:如果要再加一種配料(火腿腸),我們就會(huì )發(fā)現需要給FriedRice和FriedNoodles分別定義一個(gè)子類(lèi)。如果要新增一個(gè)快餐品類(lèi)(炒河粉)的話(huà),就需要定義更多的子,會(huì )出現類(lèi)爆炸的問(wèn)題。

    繼承適合于縱向擴展

    二、裝飾模式

    2.1 裝飾(Decorator)模式中的角色

    •  抽象構件(Component)角色 :定義一個(gè)抽象接口以規范準備接收附加責任的對象。
    • 具體構件(Concrete Component)角色 :實(shí)現抽象構件,通過(guò)裝飾角色為其添加一些職責。
    • 抽象裝飾(Decorator)角色 : 繼承或實(shí)現抽象構件,并包含具體構件的實(shí)例,可以通過(guò)其子類(lèi)擴展具體構件的功能。
    • 具體裝飾(ConcreteDecorator)角色 :實(shí)現抽象裝飾的相關(guān)方法,并給具體構件對象添加附加的責任。

    2.2 裝飾模式改進(jìn)設計UML

    2.3 裝飾模式代碼實(shí)現

    (1)構件代碼

    //快餐接口--抽象類(lèi)或接口實(shí)現都可以
    public abstract class FastFood {
        private float price;
        private String desc;
    ​
        public FastFood() {
        }
    ​
        public FastFood(float price, String desc) {
            this.price = price;
            this.desc = desc;
        }
    ​
        public void setPrice(float price) {
            this.price = price;
        }
    ​
        public float getPrice() {
            return price;
        }
    ​
        public String getDesc() {
            return desc;
        }
    ​
        public void setDesc(String desc) {
            this.desc = desc;
        }
    ​
        public abstract float cost();  //獲取價(jià)格
    }
    ​
    //炒飯
    public class FriedRice extends FastFood {
    ​
        public FriedRice() {
            super(10, "炒飯");
        }
    ​
        public float cost() {
            return getPrice();
        }
    }
    ​
    //炒面
    public class FriedNoodles extends FastFood {
    ​
        public FriedNoodles() {
            super(12, "炒面");
        }
    ​
        public float cost() {
            return getPrice();
        }
    }
    

    (2)抽象裝飾代碼

    package com.fupinng3.gar;
     
    /**
     * 抽象裝飾
     * 即繼承自FastFood,又聚合FastFood
     */
    public abstract class Garnish extends FastFood{
        private FastFood fastFood;
     
        public Garnish() {
        }
     
        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;
        }
    }
    

    (3)具體裝飾

    package com.fupinng3.gar;
     
    public class Egg extends Garnish{
        public Egg(FastFood fastFood) {
            super(fastFood, 2, "雞蛋");
        }
     
        @Override
        public float cost() {
            return getPrice()+getFastFood().cost();
        }
     
        @Override
        public String getDesc() {
            String str1=super.getDesc();
            String str2=getFastFood().getDesc();
            return str1+str2;
        }
    }
     
     
     
     
     
    package com.fupinng3.gar;
     
    public class Bacon extends Garnish{
     
        public Bacon(FastFood fastFood) {
            super(fastFood, 5, "培根");
        }
     
        public float cost() {
            return getPrice()+getFastFood().cost();
        }
     
        @Override
        public String getDesc() {
            return super.getDesc()+getFastFood().getDesc();
        }
    }
    

    (4)測試代碼

    package com.fupinng3.gar;
     
    public class Test {
        public static void main(String[] args) {
            //來(lái)個(gè)炒面
            FastFood fastFood=new FriedNoodles();
            System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
     
            //加個(gè)雞蛋
            fastFood=new Egg(fastFood);
            System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
     
            //再加個(gè)雞蛋
            fastFood=new Egg(fastFood);
            System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
     
            //再加個(gè)培根
            fastFood=new Bacon(fastFood);
            System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
     
        }
     
    }
    

    (5)輸出

    炒面    20.0元
    雞蛋炒面    22.0元
    雞蛋雞蛋炒面    24.0元
    培根雞蛋雞蛋炒面    29.0元

    一個(gè)現實(shí)生活中的裝飾模式例子:各種顏色、圖案形成的俄羅斯套娃

    三、靜態(tài)代理和裝飾者模式的區別

    (1)相同點(diǎn):都是增強目標方法

    (2)不同點(diǎn):(如下2個(gè)不同點(diǎn)可以理解為1個(gè))

    • 目的不同 :裝飾者是為了增強目標對象,靜態(tài)代理是為了保護和隱藏目標對象(代理未對外暴露目標對象)
    • 獲取目標對象構建的地方不同:裝飾者是由外界傳遞進(jìn)來(lái),可以通過(guò)構造方法傳遞 靜態(tài)代理是在代理類(lèi)內部創(chuàng )建,以此來(lái)隱藏目標對象

    到此這篇關(guān)于Java設計模式之裝飾模式詳解的文章就介紹到這了,更多相關(guān)Java裝飾模式內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

    亚洲一线产区二线产区精华| JLZZ大全高潮多水老师| 国产女人喷潮视频在线观看| 亚洲国产日产无码精品| 强壮的公次次弄得我高潮电影| 男女乱婬真视频|