設(shè)計(jì)模式系列:工廠方法、簡(jiǎn)單工廠、抽象工廠

場(chǎng)景

當(dāng)今每個(gè)人都有追求高品質(zhì)生活的能力或需求。相信你也不例外,現(xiàn)在你在陪男朋友(或女朋友)在咖啡廳里點(diǎn)咖啡。(咖啡廳里只有一個(gè)店員)你告訴店員說(shuō):我要一杯拿鐵;此時(shí)你男朋友(或女朋友)說(shuō):我要一杯卡布基諾。你喝到拿鐵并贊嘆:“我覺(jué)得口味偏奶氣”,你男朋友喝到卡布基諾并贊嘆:“我覺(jué)得口味糖分比較多”

代碼

public class Drink {
    private String name;
    private String taste;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTaste() {
        return taste;
    }
    public void setTaste(String taste) {
        this.taste = taste;
    }
}

public class Mocha extends Drink {
    public Mocha(){
        super.setName("mocha");
        super.setTaste("hot chocolate");
    }
}
public class Cappuccino extends Drink {

    public Cappuccino(){
        super.setName("cappuccino");
        super.setTaste("rich milk coffee");
    }
}
public class Customer {
        public void drinking(String drinkName) {
            Drink drink = Waiter.makeDrink(drinkName);
            System.out.println("我覺(jué)得口味"+drink.getTaste());
        }
}

public class Waiter {
    public static Drink makeDrink(String drinkName) {
        Drink drink = null;
        if(StringUtils.equals(drinkName, "latte")){
            drink = new Latte();
        } else if(StringUtils.equals(drinkName, "cappuccino")) {
            drink = new Cappuccino();
        } else if(StringUtils.equals(drinkName, "mocha")) {
            drink = new Mocha();
        }
        return drink;
    }
}

public class Test {
    public static void main(String[] args) {
        Customer me = new Customer();
        me.drinking("latte");
        Customer he = new Customer();
        he.drinking("cappuccino");
    }
}

簡(jiǎn)單工廠

概念

靜態(tài)工廠方法模式(Static FactoryMethod Pattern),是通過(guò)專門定義一個(gè)類來(lái)負(fù)責(zé)創(chuàng)建其他類的實(shí)例,被創(chuàng)建的實(shí)例通常都具有共同的父類。
注意:一般簡(jiǎn)單工廠要么是靜態(tài)方法提供,要么是以單例存在。

類圖

屏幕快照 2017-06-17 上午1.16.29.png

場(chǎng)景

大型的酒店后廚里,都有很多類型的廚師?,F(xiàn)在你來(lái)酒店點(diǎn)單,你點(diǎn)了如下單品:日式拉面、牛排再來(lái)個(gè)松子桂魚。訂單到廚師長(zhǎng)手里,后廚利有:

  • 日式廚師,擅長(zhǎng):生魚片、日式拉面;
  • 歐式廚師,擅長(zhǎng):牛排、意大利面;
  • 中式廚師,擅長(zhǎng):東北亂燉、松子桂魚;
    那對(duì)于廚師長(zhǎng)來(lái)說(shuō)怎么安排工作呢?使用代碼實(shí)現(xiàn)廚師長(zhǎng)工作安排。

代碼

public class JapaneseNoodles extends Food {
    public JapaneseNoodles(){
        setName("Japanese_noodles");
        setTaste("好吃還有豚骨口味");
    }
}

public interface IChef {
    public Food makeFood(String foodName);
    public List<String> supportFoodName();
}

public class ChineseChef implements IChef {
    public List<String> supportFoodName() {
        return new ArrayList<String>(){
            {
                add("東北亂燉");
                add("松子桂魚");
            }
        };
    }
    public Food makeFood(String foodName) {
        if(StringUtils.equals(foodName, "東北亂燉")) {
            return new NortheastChaosStew();
        }
        if(StringUtils.equals(foodName, "松子桂魚")){
            return new PineNutfish();
        }
        throw new RuntimeException("我不擅長(zhǎng)你點(diǎn)的菜品:"+ foodName);
    }
}

public class EuropeanChef implements IChef{
    public List<String> supportFoodName() {
        return new ArrayList<String>(){
            {
                add("Steak");
                add("spaghetti");
            }
        };
    }
    public Food makeFood(String foodName) {
        if(StringUtils.equals(foodName, "Steak")) {
            return new Steak();
        } else if(StringUtils.equals(foodName, "spaghetti")) {
            return new Spaghetti();
        }
        throw new RuntimeException("我不擅長(zhǎng)你點(diǎn)的菜品:"+ foodName);
    }
}

public class Test {
    public static void main(String[] args) {
        IChef chineseChef = new ChineseChef();
        Food chinaFood = chineseChef.makeFood("Japanese_noodles");
        System.out.println(chinaFood.getTaste());
        IChef japanChef = new JapaneseChef();
        Food japanFood = japanChef.makeFood("Japanese_noodles");
        System.out.println(japanFood.getTaste());
    }
}

工廠方法

概念

定義一個(gè)用于創(chuàng)建對(duì)象的接口,讓子類決定實(shí)例化哪一個(gè)類,工廠方法使一個(gè)類的實(shí)例化延遲到其子類。

類圖

屏幕快照 2017-06-17 上午1.52.26.png

場(chǎng)景:

現(xiàn)在車輪廠、車架廠和車座廠商給做車的廠商提供基礎(chǔ)材料,車場(chǎng)出很品牌很多類型的車,請(qǐng)實(shí)現(xiàn)此功能

代碼

public class Wheel {
    private String size;
    private String model;
    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
}

public class MiqlWheel extends Wheel {
    public void MiqlWheel() {
        setModel("miqilin");
        setSize("large");
    }
}

public class WheelFactory {
    public  Wheel getWheel(String wheelModel){
        if(StringUtils.equals(wheelModel, "miql")){
            return new MiqlWheel();
        }
        return null;
    }
}

public class Frame {
    private String size;
    private String color;
    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

public class TaiFrame extends Frame {
    public TaiFrame(){
        setColor("yellow");
        setSize("large");
    }
}

public class FrameFactory {
    public Frame makeFrame(String frameName) {
        if(StringUtils.equals("taiFrame", frameName)) {
            return new TaiFrame();
        }
        return null;
    }
}

public interface ICarFactory {
    public Wheel getWheel(String wheelName);
    public Frame getFrame(String frameName);
}

public class AutoCarFactory implements  ICarFactory {
    private FrameFactory frameFactory = new FrameFactory();
    private WheelFactory wheelFactory = new WheelFactory();
    public Wheel getWheel(String wheelName) {
        return wheelFactory.getWheel(wheelName);
    }
    public Frame getFrame(String frameName) {
        return frameFactory.makeFrame(frameName);
    }
}

如果需要增加材料廠,只需要增加材料選擇工廠,提供方法,對(duì)原有的接口提供無(wú)變化。

抽象工廠

概念

抽象工廠模式圍繞一個(gè)超級(jí)工廠,創(chuàng)造其他工廠。這個(gè)工廠也被稱為工廠的工廠。提供一個(gè)創(chuàng)建一系列相關(guān)或相互依賴對(duì)象的接口,而無(wú)需指定他們具體的類

類圖

屏幕快照 2017-06-17 上午2.36.23.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容