引言
抽象工厂模式是基于工厂模式之上的另一种创造性的设计模式,是工厂模式的一种抽象;基于工厂模式的案例抽象出抽象工厂模式。
抽象工厂模式设计全球化汽车工厂
在工厂模式中我们将不同类型的汽车使用工厂流水线来生产,隐藏其复杂的生产逻辑(构建过程);假如我们生产的汽车是面向全球地区的,那么应该如何让我们的生产面向全球化。
为了支持全球化运营,我们需要增强我们应用程序,以便生产出支持不同国家地区风格的汽车,比如我们国家汽车的驾驶室在前排左侧,而美国等欧洲国家驾驶室在前排的右侧,我们必须支持这种差异化生产。
为了描述抽象工厂模式,我们将考虑3种产品 - 美国,亚洲和默认(所有其他国家),支持多个位置需要进行关键的设计更改。
为了支持这种多样化的生产细节,我们需要增加工厂流水线,即USACarFactory,AsiaCarFactory和DefaultCarFactory。在新增3条流水线之后,我们的工厂便足以能够面向多样化生产,而使用者只需要通过抽象工厂类来制造自己想要的汽车,不要关注其中的细节。
抽象工厂是基于工厂类的基础上的再一层抽象,我们需要将工厂类根据不同类型进行改造,然后在抽象工厂中对新流水线进行再次抽象。
抽象工厂的解决方案
抽象工厂封装类图:
使用CarFactory隐藏位置工厂的生产细节。
抽象工厂实现
为汽车新增Location属性:
package com.iblog.pattern.factory;
public enum Location {
DEFAULT, USA, ASIA
}
抽象工厂类实现:
package com.iblog.pattern.factory;
public abstract class Car {
private CarType model;
private Location location;
public Car(CarType model, Location location) {
this.model = model;
this.location = location;
}
private void arrangeParts() {
// Do one time processing here
}
// Do subclass level processing in this method
protected abstract void construct();
public CarType getModel() {
return model;
}
public void setModel(CarType model) {
this.model = model;
}
}
将Location属性增加至各种汽车模型中(LuxuryCar):
package com.iblog.pattern.factory;
public class LuxuryCar extends Car {
LuxuryCar(Location location) {
super(CarType.LUXURY, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building luxury car");
// add accessories
}
}
将之前的工厂实现根据Location属性进行抽象:
package com.iblog.pattern.factory;
public class USACarFactory {
public static Car buildCar(CarType model) {
Car car = null;
switch (model) {
case SMALL:
car = new SmallCar(Location.USA);
break;
case SEDAN:
car = new SedanCar(Location.USA);
break;
case LUXURY:
car = new LuxuryCar(Location.USA);
break;
default:
//throw some exception
break;
}
return car;
}
}
package com.iblog.pattern.factory;
public class AsiaCarFactory {
public static Car buildCar(CarType model) {
Car car = null;
switch (model) {
case SMALL:
car = new SmallCar(Location.ASIA);
break;
case SEDAN:
car = new SedanCar(Location.ASIA);
break;
case LUXURY:
car = new LuxuryCar(Location.ASIA);
break;
default:
//throw some exception
break;
}
return car;
}
}
package com.iblog.pattern.factory;
public class DefaultCarFactory {
public static Car buildCar(CarType model) {
Car car = null;
switch (model) {
case SMALL:
car = new SmallCar(Location.DEFAULT);
break;
case SEDAN:
car = new SedanCar(Location.DEFAULT);
break;
case LUXURY:
car = new LuxuryCar(Location.DEFAULT);
break;
default:
//throw some exception
break;
}
return car;
}
}
然后我们在3个Location工厂之上抽象出来一个新的抽象工厂类,我们通过不同Location工厂类来生产汽车:
package com.iblog.pattern.factory;
public class CarFactory {
private CarFactory() {
//Prevent instantiation
}
public static Car buildCar(CarType model) {
Car car = null;
//Read location property somewhere from configuration, also as a param.
Location location = Location.USA;
//Use location specific car factory
switch (location) {
case USA:
car = USACarFactory.buildCar(model);
break;
case ASIA:
car = AsiaCarFactory.buildCar(model);
break;
case DEFAULT:
car = DefaultCarFactory.buildCar(model);
break;
default:
// throw some exception
break;
}
return car;
}
}
测试:
package com.iblog.pattern.factory;
import org.junit.Test;
import static org.junit.Assert.*;
public class CarFactoryTest {
@Test
public void buildCar() {
System.out.println(CarFactory.buildCar(CarType.SMALL));
System.out.println(CarFactory.buildCar(CarType.SEDAN));
System.out.println(CarFactory.buildCar(CarType.LUXURY));
}
}
结果:
Building small car
com.iblog.pattern.factory.SmallCar@5b80350b
Building sedan car
com.iblog.pattern.factory.SedanCar@5d6f64b1
Building luxury car
com.iblog.pattern.factory.LuxuryCar@32a1bec0
Summary
当我们在工厂模式实现中需要对工厂进行抽象编程时,应用抽象工厂实现,与工厂模式之间多了一层抽象逻辑,在Jdk中抽象工厂模式也是常见的: