欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java設(shè)計模式之裝飾模式詳解

 更新時間:2021年04月30日 08:37:31   作者:松下一田  
這篇文章主要介紹了Java設(shè)計模式之裝飾模式詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下

一、裝飾模式引入例子

一個快餐店計算價格問題舉例:

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

1.1 一般設(shè)計

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

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

繼承適合于縱向擴展

二、裝飾模式

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

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

2.2 裝飾模式改進設(shè)計UML

2.3 裝飾模式代碼實現(xiàn)

(1)構(gòu)件代碼

//快餐接口--抽象類或接口實現(xiàn)都可以
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();  //獲取價格
}
​
//炒飯
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) {
        //來個炒面
        FastFood fastFood=new FriedNoodles();
        System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
 
        //加個雞蛋
        fastFood=new Egg(fastFood);
        System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
 
        //再加個雞蛋
        fastFood=new Egg(fastFood);
        System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
 
        //再加個培根
        fastFood=new Bacon(fastFood);
        System.out.println(fastFood.getDesc()+"\t"+fastFood.cost()+"元");
 
    }
 
}

(5)輸出

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

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

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

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

(2)不同點:(如下2個不同點可以理解為1個)

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

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

相關(guān)文章

最新評論