[design pattern](6) Absract Factory
前言
在前面的章节中,我们先后介绍了简单工厂模式和工厂方法模式。他们都是工厂模式大家族的一员,那么,本章将会接着上一章,来说一说工厂模式的最后一员,那就是抽象工厂模式。
思考题
首先,来思考下下面的问题怎么实现:
在工厂方法模式中,博主在四川和上海开了一家饭店,目前生意相当的火爆,但是还是不赚钱。分析原因发现,目前各家分店都是自己采购原料,并且很贵。那博主就不愿意了,博主决定,自己开一家生产原料的工厂。那么我们应该去实现这个工厂呢?
要是学习了 抽象工厂模式 ,你就会很惊喜的发现,那家伙可以很好的满足我们的需求。
抽象工厂模式
定义:抽象工厂模式就是在抽象工厂中提供一系列的接口,用这一系列的接口创建相关或者依赖对象的家族,而不需要指定具体的类。
类图:
上面的类图,涉及以下几个角色:
- 抽象产品A,B:为产品A,B定义了一些接口。
- 具体产品A,B:抽象产品A,B的具体实现。
- 抽象工厂:定义了一些列的接口,用于创建具体的产品A,B。
- 具体工厂:实现了抽象工厂的一系列接口,可以生产一些相关的产品。
思考题实现
首先,定义抽象产品:
Salt.java:
public interface Salt { String put(); }
然后,定义抽象产品的实现:
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(); } }