抽象工厂模式(Abstract Factory Pattern)

       顾名思义,它是属于创建设计模式之一,作为工厂模式上的抽象层。抽象设计模式是比工厂模式更抽象一级的模式。

     在工厂模式的例子基础之上,添加地域功能,以实现全局范围的汽车类型。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:

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值