顾名思义,它是属于创建设计模式之一,作为工厂模式上的抽象层。抽象设计模式是比工厂模式更抽象一级的模式。
在工厂模式的例子基础之上,添加地域功能,以实现全局范围的汽车类型。UML 类图如下:
完整代码如下:
public enum Location {
DEFAULT, USA, ASIA
}
public enum CarType {
SMALL, SEDAN, LUXURY
}
public abstract class Car {
public Car(CarType model, Location location){
this.model = model;
this.location = location;
System.out.println("location is " + location);
}
protected abstract void construct();
private CarType model = null;
private Location location = null;
public CarType getModel() {
return model;
}
public void setModel(CarType model) {
this.model = model;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
@Override
public String toString() {
return "Model- "+model + " built in "+location;
}
}
public class SmallCar extends Car {
SmallCar(Location location) {
super(CarType.SMALL, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building small car");
// add accessories
}
}
public class SedanCar extends Car {
SedanCar(Location location) {
super(CarType.SEDAN, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building sedan car");
// add accessories
}
}
public class LuxuryCar extends Car{
public LuxuryCar(Location location)
{
super(CarType.LUXURY, location);
construct();
}
@Override
protected void construct() {
System.out.println("Building luxury car");
//add accessories
}
}
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;
}
}
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;
}
}
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;
}
}
public class CarFactory
{
private CarFactory() {
//Prevent instantiation
}
public static Car buildCar(CarType type)
{
Car car = null;
Location location = Location.ASIA; //Read location property somewhere from configuration
//Use location specific car factory
switch(location)
{
case USA:
car = USACarFactory.buildCar(type);
break;
case ASIA:
car = AsiaCarFactory.buildCar(type);
break;
default:
car = DefaultCarFactory.buildCar(type);
}
return car;
}
}
public class CarAbstractFactoryTest {
@SuppressWarnings("deprecation")
@Test
public void testCarFactory() {
Assert.assertEquals(true, CarFactory.buildCar(CarType.SMALL) instanceof Car) ;
Assert.assertEquals(true, CarFactory.buildCar(CarType.SEDAN) instanceof Car) ;
Assert.assertEquals(true, CarFactory.buildCar(CarType.LUXURY) instanceof Car) ;
}
}
默认情况是亚洲,结果如下:
location is ASIA
Building small car
location is ASIA
Building sedan car
location is ASIA
Building luxury car
正如你看到的,抽象工厂模式是在工厂模式的抽象级别。如果想更深入研究,请查阅Java API: