抽象工厂模式是在工厂方法模式基础上演化而来的,在实现上的区别在于,工厂方法模式中的抽象工厂中只有一个方法,而抽象工厂模式的抽象工厂中有多个方法。
从应用场景来说,抽象工厂更具有一般性,抽象工厂生产的产品可以不是由一个接口或者一个抽象类派生来的。而工厂方法模式生产的产品必须是由一个接口或者抽象类派生来的。
也可以认为抽象工厂模式,是一定场景下,多个工厂方法模式的组合。
从另一个角度来说,抽象工厂提供的维度更多,可以看下面的例子:
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; /** * @description 抽象工厂模式 核心,抽象工厂 * @author panteng * @date 17-2-4. */ public abstract class AbstractAbstractFactoryCase { public abstract MountainBike getMountainBike(); public abstract RoadBikes getRoadBikes(); }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; import com.pt.factory.method.GiantBike; /** * @description * @author panteng * @date 17-2-4. */ public class GiantBikeFactory extends AbstractAbstractFactoryCase { @Override public MountainBike getMountainBike(){ GiantBike giant = new GiantBike(); giant.setType("山地车"); return giant; } @Override public RoadBikes getRoadBikes(){ GiantBike giant = new GiantBike(); giant.setType("公路车"); return giant; } }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; import com.pt.factory.method.MeridaBike; /** * @description * @author panteng * @date 17-2-4. */ public class MeridaBikeFactory extends AbstractAbstractFactoryCase { @Override public MountainBike getMountainBike(){ MeridaBike merida = new MeridaBike(); merida.setType("山地车"); return merida; } @Override public RoadBikes getRoadBikes(){ MeridaBike merida = new MeridaBike(); merida.setType("公路车"); return merida; } }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; /** * @description 山地车 * @author panteng * @date 17-2-4. */ public interface MountainBike { }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; /** * @description 公路车 * @author panteng * @date 17-2-4. */ public interface RoadBikes { }
由于项目整体原因,所以GiantBike与MeridaBike继承于AbstractBike,实际上但从抽象工厂模式角度来说,可以不继承此类!
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.method; import com.pt.factory.abstracted.MountainBike; import com.pt.factory.abstracted.RoadBikes; /** * @description * @author panteng * @date 17-2-3. */ public class GiantBike extends AbstractBike implements MountainBike, RoadBikes { /** 自行车种类:山地车 or 公路车 */ private String type = ""; /** * @description 展示外观 * @param * @return * @throws */ public void showAppearance(){ System.out.println("我是捷安特自行车,Type=" + this.type); } public String getType(){ return type; } public void setType(String type){ this.type = type; } }
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.method; import com.pt.factory.abstracted.MountainBike; import com.pt.factory.abstracted.RoadBikes; /** * @description * @author panteng * @date 17-2-3. */ public class MeridaBike extends AbstractBike implements MountainBike, RoadBikes { /** 自行车种类:山地车 or 公路车 */ private String type = ""; /** * @description 展示外观 * @param * @return * @throws */ public void showAppearance(){ System.out.println("我是美利达自行车,Type=" + this.type); } public String getType(){ return type; } public void setType(String type){ this.type = type; } }
测试代码
/* * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved */ package com.pt.factory.abstracted; import com.pt.factory.method.GiantBike; import com.pt.factory.method.MeridaBike; import org.junit.Test; /** * @description * @author panteng * @date 17-2-4. */ public class AbstractAbstractFactoryCaseTest { @Test public void testAbstractFactoryCase(){ AbstractAbstractFactoryCase abstractFactory = new GiantBikeFactory(); //抽象工厂派生的捷安特工厂 AbstractAbstractFactoryCase abstractFactory2 = new MeridaBikeFactory(); //抽象工厂派生的捷安特工厂 GiantBike giant1 = (GiantBike) abstractFactory.getMountainBike(); giant1.showAppearance(); GiantBike giant2 = (GiantBike) abstractFactory.getRoadBikes(); giant2.showAppearance(); MeridaBike meridaBike1 = (MeridaBike) abstractFactory2.getMountainBike(); meridaBike1.showAppearance(); MeridaBike meridaBike2 = (MeridaBike) abstractFactory2.getRoadBikes(); meridaBike2.showAppearance(); } }
下面简单总结一下工厂模式:
细分可分为三类 - 简单工厂模式、工厂方法模式、抽象工厂模式;
简单工厂模式一般适用于一类产品包含有限的几种产品的场景下;
工厂方法模式适用于具有某些共性的几类产品,每一类产品又可以细分为几种产品的场景下;
抽象工厂模式具有一般性,一般产品分类更加细化,需要从多维度分类的情况下。