`
hulianwang2014
  • 浏览: 686099 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

抽象工厂模式(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:

分享到:
评论

相关推荐

    创建型模式之抽象工厂模式(Abstract Factory Pattern)

    3、抽象工厂模式(Abstract Factory Pattern) 用意:一个工厂生产一系列产品

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器模式ADAPTER PATTERN 9、模板方法模式TEMPLATE METHOD PATTERN 10、建造者模式BUILDER PATTERN 11、桥梁模式BRIDGE PATTERN 12、...

    创建型-抽象工厂模式(Abstract Factory)

    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式,属于对象创建型模式。

    设计模式 之 “抽象工厂模式[Abstract Factory Pattern]”

    NULL 博文链接:https://lym6520.iteye.com/blog/701742

    [创建型模式] 设计模式之抽象工厂模式(Abstract Factory Pattern)

    NULL 博文链接:https://jacky-dai.iteye.com/blog/2294531

    设计模式_抽象工厂模式.zip

    抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 在抽象工厂模式中,接口是负责...

    Python设计模式之抽象工厂模式原理与用法详解

    抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类 下面是一个抽象工厂的demo: #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = 'Andy' 大话...

    C#版 24种设计模式

    备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...

    abstract-factory-pattern

    Java抽象工厂模式:解锁代码复用的新境界 在编程世界中,设计模式是一组被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。它们能够帮助我们更好地组织代码,提高代码的可重用性、可维护性和可扩展性...

    用Java实现23种设计模式

    抽象工厂模式(Abstract Factory Pattern) 单例模式(Singleton Pattern) 建造者模式(Builder Pattern) 原型模式(Prototype Pattern) 2. 结构型模式 适配器模式(Adapter Pattern) 桥接模式(Bridge ...

    python 抽象工厂模式(示例)

    # 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。# 该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。# 在抽象工厂模式中,接口是...

    设计模式PPT

     抽象工厂模式(Abstract Factory Pattern)  建造者模式(Builder Pattern)  原型模式(Prototype Pattern)  单例模式(Singleton Pattern) 结构型模式用来处理类或者对象的组合,主要包含以下7种设计...

    抽象工厂模式

    抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    33种JAVA设计模式DEMO

    抽象工厂模式(Abstract Factory Pattern) 单例模式(Singleton Pattern) 建造者模式(Builder Pattern) 原型模式(Prototype Pattern) 2 结构型模式 这些设计模式关注类和对象的组合。继承的概念被用来组合接口...

    C#设计模式_设计模式_C#_

    抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator ...

    C#设计模式(23种设计模式)

    抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰...

    Java设计模式,并加上个人理解

    5. 抽象工厂模式 (Abstract Factory) 6. 策略模式 (Strategy Pattern) 7. 适配器模式 (Adapter Pattern) 8. 模板方法模式 (Template Pattern) 9. 建造者模式 (Builder Pattern) 10. 原型模式 (Prototype ...

    C++设计模式(Design Pattern)范例源代码

    抽象工厂模式(Abstract Factory) 生成器模式(Builder) 工厂方法模式(Factory Method) 原型模式(Prototype) 单件模式(Singleton) 结构型: 适配器模式(Adapter) 桥接模式(Bridge) 组合模式(Composite) 装饰者模式...

Global site tag (gtag.js) - Google Analytics