[design pattern](6) Absract Factory

前言

在前面的章節中,咱們前後介紹了簡單工廠模式和工廠方法模式。他們都是工廠模式你們族的一員,那麼,本章將會接着上一章,來講一說工廠模式的最後一員,那就是抽象工廠模式。java

 

 思考題

首先,來思考下下面的問題怎麼實現:ide

在工廠方法模式中,博主在四川和上海開了一家飯店,目前生意至關的火爆,可是仍是不賺錢。分析緣由發現,目前各家分店都是本身採購原料,而且很貴。那博主就不肯意了,博主決定,本身開一家生產原料的工廠。那麼咱們應該去實現這個工廠呢?

 

要是學習了 抽象工廠模式 ,你就會很驚喜的發現,那傢伙能夠很好的知足咱們的需求。學習

 

抽象工廠模式

定義:抽象工廠模式就是在抽象工廠中提供一系列的接口,用這一系列的接口建立相關或者依賴對象的家族,而不須要指定具體的類。測試

類圖:this

上面的類圖,涉及如下幾個角色:spa

  • 抽象產品A,B:爲產品A,B定義了一些接口。
  • 具體產品A,B:抽象產品A,B的具體實現。
  • 抽象工廠:定義了一些列的接口,用於建立具體的產品A,B。
  • 具體工廠:實現了抽象工廠的一系列接口,能夠生產一些相關的產品。

思考題實現

首先,定義抽象產品:code

Salt.java:對象

public interface Salt {
    String put();
}

 

而後,定義抽象產品的實現:blog

ShangHaiSalt.java:接口

public class ShangHaiSalt implements Salt {
    public String put() {
        return "上海的鹽";
    }
}

 

SiChuanSalt.java:

public class SiChuanSalt implements Salt {
    public String put(){
        return "四川的鹽";
    }
}

 

而後,定義抽象原料工廠:

IngredientFactory.java:

public interface IngredientFactory {
    Salt createSalt();
}

 

而後,實現抽象原料工廠:

ShangHaiIngredientFactory.java:

public class ShangHaiIngredientFactory implements IngredientFactory {
    public Salt createSalt() {
        return new ShangHaiSalt();
    }
}

 

SiChuanIngredientFactory.java:

public class SiChuanIngredientFactory implements IngredientFactory {
    public Salt createSalt() {
        return new SiChuanSalt();
    }
}

 

而後,定義抽象產品:

Food.java:

public abstract class Food {
    protected IngredientFactory ingredientFactory;


    public Food(IngredientFactory ingredientFactory) {
        this.ingredientFactory = ingredientFactory;
    }

    public void fry() {
        System.out.println("翻炒");
    }

    public void eat() {
        System.out.println("吃");
    }

    public abstract void putSeasoning();
}

 

而後,實現抽象產品:

KungPaoChicken.java:

public class KungPaoChicken extends Food {
    public KungPaoChicken(IngredientFactory ingredient) {
        super(ingredient);
    }

    @Override
    public void putSeasoning() {
        Salt salt = ingredientFactory.createSalt();
    System.out.println("放入" + salt.put());
    }
}

 

TomatoEgg.java:

public class TomatoEgg extends Food {
      
    public TomatoEgg(IngredientFactory ingredientFactory) {
        super(ingredientFactory);
    }
    @Override
    public void putSeasoning() {
    Salt salt = ingredientFactory.createSalt();
    System.out.println("放入" + salt.put());        
    }
}

 

而後,定義抽象工廠:

AbstractHotal.java:

public abstract class AbstractHotal {
    public Food saleFood(String foodName) {
        Food food = createFood(foodName);
    food.putSeasoning();
    food.fry();
    return food;
    }

    public abstract Food createFood(String foodName);
}

 

而後,實現抽象工廠:

ShangHaiHotal.java:

public class ShangHaiHotal extends AbstractHotal {
    @Override
    public Food createFood(String foodName) {
        IngredientFactory ingredient = new ShangHaiIngredientFactory();
    switch(foodName) {
        case "KungPaoChicken": 
            return new KungPaoChicken(ingredient);
        case "TomatoEgg":
            return new TomatoEgg(ingredient);
        default:
            return null;
    }
    }
}

 

SiChuanHotal.java:

public class SiChuanHotal extends AbstractHotal {
    @Override
    public Food createFood(String foodName) {
    IngredientFactory ingredient = new SiChuanIngredientFactory();
        switch(foodName) {
        case "KungPaoChicken":
            return new KungPaoChicken(ingredient);
        case "TomatoEgg":
            return new TomatoEgg(ingredient);
        default:
            return null;
    }
    }
}

 

最後附上測試代碼:

Custom.java:

public class Custom {
    public static void main(String ...args) {
        AbstractHotal shangHaiHotal = new ShangHaiHotal();
    AbstractHotal siChuanHotal = new SiChuanHotal();

    Food shangHaiKungPaoChicken = shangHaiHotal.saleFood("KungPaoChicken");
    Food shangHaiTomatoEgg = shangHaiHotal.saleFood("TomatoEgg");

    Food siChuanKungPaoChicken = siChuanHotal.saleFood("KungPaoChicken");
    Food siChuanTomatoEgg = siChuanHotal.saleFood("TomatoEgg");

    shangHaiKungPaoChicken.eat();
    shangHaiTomatoEgg.eat();

    siChuanKungPaoChicken.eat();
    siChuanTomatoEgg.eat();
    }
}
相關文章
相關標籤/搜索