Abstract Factory Pattern of java design pattern (Abstract Factory Pattern)

concept

Abstract Factory Pattern: Provides an interface for creating a series of related or interdependent objects without specifying their specific classes. The abstract factory pattern, also known as the Kit pattern, belongs to the object creation pattern.

The Abstract Factory pattern provides a way to encapsulate individual factories of the same product family. In normal use, a client program needs to create a concrete implementation of the abstract factory, and then use the abstract factory as an interface to create concrete objects of this subject. The client program does not need to know (or care) the concrete type of objects it gets from these internal factory methods, because the client program only uses the common interface of these objects. The Abstract Factory pattern separates the implementation details of a set of objects from their general use.

product family

Let's understand what a product family is: a family of functionally related products located in different product hierarchy structures. As in the example below, there are two product families: a sports car family and a business car family.

[QQ20160419-0][5]

use

The abstract factory pattern, like the factory method pattern, conforms to the open-closed principle. But the difference is that when the factory method pattern adds a specific product, the corresponding factory must be added. But the abstract factory pattern only needs to add a new factory when adding a specific type of product. In other words, a factory in the factory method pattern can only create one specific product. A factory in the abstract factory pattern can create multiple concrete products belonging to one type. The number of products created by the factory is between the simple factory pattern and the factory method pattern.

The abstract factory pattern can be used when:

It is important for all types of factory patterns that a system should not depend on the details of how product class instances are created, composed, and represented.

There are more than one product families in the system, and only one of them is used at a time.

Products belonging to the same product family will be used together and this constraint must be reflected in the design of the system.

The system provides a library of product classes, and all products appear with the same interface, so that the client does not depend on the specific implementation.

Method to realize

The abstract factory pattern includes the following roles:

AbstractFactory (abstract factory): used to declare the method of generating abstract products

ConcreteFactory (concrete factory): implements the method of generating abstract products declared by the abstract factory, and generates a set of specific products. These products form a product family, and each product is located in a product hierarchy structure;

AbstractProduct (abstract product): declares an interface for each product, and defines the abstract business method of the product in the abstract product;

Product (concrete product): define the specific product object produced by the specific factory, and realize the business method defined in the abstract product interface.

The example in this article uses an example of a car foundry manufacturing a car. Suppose we are a car OEM manufacturer, and we are responsible for manufacturing cars for two companies, Mercedes-Benz and Tesla. We simply understand Mercedes-Benz as a car that needs to be refueled, and Tesla as a car that needs to be charged. Among them, Mercedes-Benz includes sports cars and commercial vehicles, and Tesla also includes sports cars and commercial vehicles.

[QQ20160419-1][6]

In the above scenarios, we can treat sports cars and commercial vehicles separately. There is a separate factory for sports cars and a separate factory for commercial vehicles. In this way, no matter whether we help any other manufacturers to build cars in the future, as long as they are sports cars or commercial vehicles, we don't need to introduce factories. Likewise, if we were to add another type of vehicle, such as an SUV, we wouldn't need to modify anything from a sports car or a business vehicle.

Here are the abstract products, Mercedes and Tesla:

public interface BenzCar {

    //加汽油
    public void gasUp();

}

public interface TeslaCar {

    //充电
    public void charge();
}

The following are the specific products, Mercedes-Benz sports car, Mercedes-Benz commercial vehicle, Tesla sports car, Tesla commercial vehicle:

public class BenzSportCar implements BenzCar {
    public void gasUp() {
        System.out.println("给我的奔驰跑车加最好的汽油");
    }
}

public class BenzBusinessCar implements BenzCar{
    public void gasUp() {
        System.out.println("给我的奔驰商务车加一般的汽油");
    }
}

public class TeslaSportCar implements TeslaCar {
    public void charge() {
        System.out.println("给我特斯拉跑车冲满电");
    }
}

public class TeslaBusinessCar implements TeslaCar {
    public void charge() {
        System.out.println("不用给我特斯拉商务车冲满电");
    }
}

The following is an abstract factory:

public interface CarFactory {

    public BenzCar getBenzCar();
    public TeslaCar getTeslaCar();
}

The following is the specific factory:

public class SportCarFactory implements CarFactory {
    public BenzCar getBenzCar() {
        return new BenzSportCar();
    }

    public TeslaCar getTeslaCar() {
        return new TeslaSportCar();
    }
}

public class BusinessCarFactory implements CarFactory {
    public BenzCar getBenzCar() {
        return new BenzBusinessCar();
    }

    public TeslaCar getTeslaCar() {
        return new TeslaBusinessCar();
    }
}

The inclination of the "open-closed principle"

The "open-closed principle" requires that the system is open to extension and closed to modification, and the purpose of enhancing its function can be achieved through extension. For systems involving multiple product families and multiple product hierarchy structures, the functional enhancement includes two aspects:

Adding a product family: For adding a new product family, the factory method pattern well supports the "open-close principle". For a newly added product family, it is only necessary to add a new specific factory correspondingly, and there is no need to do anything for the existing code. any modification.

Adding a new product level structure: For adding a new product level structure, all factory roles need to be modified, including abstract factory classes. In all factory classes, methods for producing new products need to be added, which cannot well support "opening and closing in principle".

This nature of the abstract factory pattern is called the inclination of the "opening and closing principle". The abstract factory pattern supports the addition of new products in an inclined manner. It provides convenience for the addition of new product families, but it cannot provide new product levels The addition of structure provides such convenience.

The relationship between the three factory models

When each specific factory class in the abstract factory pattern only creates one product object, that is, when there is only one product hierarchy structure, the abstract factory pattern degenerates into a factory method pattern;

The biggest difference between the abstract factory pattern and the factory method pattern is that the factory method pattern is aimed at a product hierarchy structure, while the abstract factory pattern needs to face multiple product hierarchy structures.

When the abstract factory in the factory method pattern is merged with the concrete factory, a unified factory is provided to create product objects, and the factory method for creating objects is designed as a static method, the factory method pattern degenerates into a simple factory pattern.

Summarize

The abstract factory pattern provides an interface for creating a series of related or interdependent objects without specifying their concrete classes. The abstract factory pattern, also known as the Kit pattern, belongs to the object creation pattern.

The abstract factory pattern is the most abstract and general form of all factory patterns.

The main advantage of the abstract factory pattern is that it isolates the generation of specific classes, so that customers do not need to know what is created, and each time multiple objects in a product family can be created through the specific factory class, it is more convenient to add or replace product families. It is very convenient to add new specific factories and product families; the main disadvantage is that it is very complicated to add a new product level structure, and it is necessary to modify the abstract factory and all specific factory classes, and the support for the "opening and closing principle" is inclined.

Guess you like

Origin blog.csdn.net/zy_dreamer/article/details/132364238