抽象工厂模式(Abstract Factory Pattern)

本文介绍了一种设计模式——抽象工厂模式,并通过一个具体的例子来解释其工作原理。该模式允许客户端独立于具体产品系列和产品等级结构进行操作,提供了一个接口,用于创建一系列相关或相互依赖的对象,而无需指定它们具体的类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

在工厂模式的例子基础之上,添加地域功能,以实现全球范围的汽车类型. 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 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、付费专栏及课程。

余额充值