Abstract Factory Pattern in Java – Java中的抽象工厂模式

最后修改: 2018年 11月 27日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll discuss the Abstract Factory design pattern.

在这篇文章中,我们将讨论抽象工厂的设计模式。

The book Design Patterns: Elements of Reusable Object-Oriented Software states that an Abstract Factory “provides an interface for creating families of related or dependent objects without specifying their concrete classes”. In other words, this model allows us to create objects that follow a general pattern.

设计模式。可重用的面向对象软件的要素指出,抽象工厂“为创建相关或依赖对象的家族提供了一个接口,而无需指定其具体类别”。换句话说,这种模式允许我们创建遵循一般模式的对象。

An example of the Abstract Factory design pattern in the JDK is the newInstance() of javax.xml.parsers.DocumentBuilderFactory class.

JDK中抽象工厂设计模式的一个例子是javax.xml.parsers.DocumentBuilderFactory类的newInstance()

2. Abstract Factory Design Pattern Example

2.抽象工厂设计模式示例

In this example, we’ll create two implementations of the Factory Method Design pattern: AnimalFactory and ColorFactory.

在这个例子中,我们将创建两个工厂方法设计模式的实现。AnimalFactoryColorFactory。

After that, we’ll manage access to them using an Abstract Factory AbstractFactory:

之后,我们将使用一个抽象工厂AbstractFactory来管理对它们的访问:

updated abstract factory

First, we’ll create a family of Animal class and will, later on, use it in our Abstract Factory.

首先,我们将创建一个Animal class的家族,以后将在我们的抽象工厂中使用它。

Here’s the Animal interface:

这里是Animal界面。

public interface Animal {
    String getAnimal();
    String makeSound();
}

and a concrete implementation Duck:

和一个具体的实现Duck

public class Duck implements Animal {

    @Override
    public String getAnimal() {
        return "Duck";
    }

    @Override
    public String makeSound() {
        return "Squeks";
    }
}

Furthermore, we can create more concrete implementations of Animal interface (like Dog, Bear, etc.) exactly in this manner.

此外,我们可以完全按照这种方式创建Animal接口的更多具体实现(如Dog、Bear、等)。

The Abstract Factory deals with families of dependent objects. With that in mind, we’re going to introduce one more family Color as an interface with a few implementations (White, Brown,…).

抽象工厂处理的是依赖对象的族。考虑到这一点,我们将再引入一个家族颜色作为一个接口,并有一些实现(白色,棕色,…)。

We’ll skip the actual code for now, but it can be found here.

我们暂时跳过实际的代码,但它可以在这里找到。

Now that we’ve got multiple families ready, we can create an AbstractFactory interface for them:

现在我们已经准备好了多个家族,我们可以为它们创建一个AbstractFactory接口。

public interface AbstractFactory<T> {
    T create(String animalType) ;
}

Next, we’ll implement an AnimalFactory using the Factory Method design pattern that we discussed in the previous section:

接下来,我们将使用上一节中讨论的工厂方法设计模式实现一个AnimalFactory

public class AnimalFactory implements AbstractFactory<Animal> {

    @Override
    public Animal create(String animalType) {
        if ("Dog".equalsIgnoreCase(animalType)) {
            return new Dog();
        } else if ("Duck".equalsIgnoreCase(animalType)) {
            return new Duck();
        }

        return null;
    }

}

Similarly, we can implement a factory for the Color interface using the same design pattern.

同样地,我们可以使用相同的设计模式为Color接口实现一个工厂。

When all this is set, we’ll create a FactoryProvider class that will provide us with an implementation of AnimalFactory or ColorFactory depending on the argument that we supply to the getFactory() method:

当所有这些都设置好后,我们将创建一个FactoryProvider类,它将为我们提供AnimalFactoryColorFactory的实现,这取决于我们提供给getFactory()方法的参数。

public class FactoryProvider {
    public static AbstractFactory getFactory(String choice){
        
        if("Animal".equalsIgnoreCase(choice)){
            return new AnimalFactory();
        }
        else if("Color".equalsIgnoreCase(choice)){
            return new ColorFactory();
        }
        
        return null;
    }
}

3. When to Use Abstract Factory Pattern:

3.何时使用抽象工厂模式:

  • The client is independent of how we create and compose the objects in the system
  • The system consists of multiple families of objects, and these families are designed to be used together
  • We need a run-time value to construct a particular dependency

While the pattern is great when creating predefined objects, adding the new ones might be challenging. To support the new type of objects will require changing the AbstractFactory class and all of its subclasses.

虽然该模式在创建预定义的对象时很好,但添加新的对象可能是个挑战。为了支持新类型的对象,需要改变AbstractFactory类及其所有的子类。

4. Summary

4.摘要

In this article, we learned about the Abstract Factory design pattern.

在这篇文章中,我们了解了抽象工厂设计模式。

Finally, as always, the implementation of these examples can be found over on GitHub.

最后,像往常一样,这些例子的实现可以在GitHub上找到over