抽象工厂模式是设计模式工厂设计模式较为复杂的一种设计模式,英文概念翻译是:为创建一组相关或相互依赖的对象提供一个借口,无需指定它们的具体类”。抽象工厂模式通常是用于创一族产品,并且这族产品分不同的等级;不同的具体工厂类生产不同等级的一族产品。抽象工厂模式也有四种角色:抽象工厂、具体工厂、抽象产品和具体产品.典型的面向接口/抽象类的编程模式。
抽象工厂模式的类图:
代码示例:
工程示例:
1.创建抽象工厂接口:
package test.edu.absfactory; public interface IAbstractFactory { public IRestaurant restaurantFactory(); public ISpecialDishes specialDisheFactory(); public IWine wineFactory(); }
package test.edu.absfactory; public interface IRestaurant { public void restaurantName(); }
package test.edu.absfactory; public interface ISpecialDishes { public void eatSpecialDishes (); }
package test.edu.absfactory; public interface IWine { public void drinkWine (); }3.创建具体产品类:
中国特色餐馆产品族实现类:
package test.edu.impl; import test.edu.absfactory.IRestaurant; public class ChineseRestaurant implements IRestaurant { @Override public void restaurantName() { System.out.println("这是中国餐馆"); } }
package test.edu.impl; import test.edu.absfactory.ISpecialDishes; public class ChineseDishes implements ISpecialDishes { @Override public void eatSpecialDishes() { System.out.println("吃特色菜满汉全席"); } }
package test.edu.impl; import test.edu.absfactory.IWine; public class ChineseWLY implements IWine { @Override public void drinkWine() { System.out.println("喝五粮液"); } }
package test.edu.impl; import test.edu.absfactory.IRestaurant; public class KORRestaurant implements IRestaurant { @Override public void restaurantName() { System.out.println("这是韩国菜馆"); } }
package test.edu.impl; import test.edu.absfactory.ISpecialDishes; public class KORDishes implements ISpecialDishes { @Override public void eatSpecialDishes() { System.out.println("吃韩国烤肉"); } }
package test.edu.impl; import test.edu.absfactory.IWine; public class KORWine implements IWine { @Override public void drinkWine() { System.out.println("喝韩国真露"); } }
中国餐馆的工厂类
package test.edu.impl; import test.edu.absfactory.IAbstractFactory; import test.edu.absfactory.IRestaurant; import test.edu.absfactory.ISpecialDishes; import test.edu.absfactory.IWine; public class ChineseDeliciousFoodFactory implements IAbstractFactory { public IRestaurant restaurantFactory(){ return new ChineseRestaurant(); } public ISpecialDishes specialDisheFactory(){ return new ChineseDishes(); } public IWine wineFactory(){ return new ChineseWLY(); } }
package test.edu.impl; import test.edu.absfactory.IAbstractFactory; import test.edu.absfactory.IRestaurant; import test.edu.absfactory.ISpecialDishes; import test.edu.absfactory.IWine; public class KORDeliciousFoodFactory implements IAbstractFactory { public IRestaurant restaurantFactory(){ return new KORRestaurant(); } public ISpecialDishes specialDisheFactory(){ return new KORDishes(); } public IWine wineFactory(){ return new KORWine(); } }
测试客户端:
去中国菜馆
package test.edu.client; import test.edu.absfactory.IAbstractFactory; import test.edu.impl.ChineseDeliciousFoodFactory; public class AbsFactoryClient { /** * @param args */ public static void main(String[] args) { IAbstractFactory dff = new ChineseDeliciousFoodFactory(); dff.restaurantFactory().restaurantName(); dff.specialDisheFactory().eatSpecialDishes(); dff.wineFactory().drinkWine(); } }结果:
这是中国餐馆 吃特色菜满汉全席 喝五粮液
IAbstractFactory dff = new ChineseDeliciousFoodFactory();为:
IAbstractFactory dff = new KORDeliciousFoodFactory();
package test.edu.client; import test.edu.absfactory.IAbstractFactory; import test.edu.impl.ChineseDeliciousFoodFactory; import test.edu.impl.KORDeliciousFoodFactory; public class AbsFactoryClient { /** * @param args */ public static void main(String[] args) { IAbstractFactory dff = new KORDeliciousFoodFactory(); dff.restaurantFactory().restaurantName(); dff.specialDisheFactory().eatSpecialDishes(); dff.wineFactory().drinkWine(); } }结果:
这是韩国菜馆 吃韩国烤肉 喝韩国真露