Model motivation

In the factory method pattern, each specific factory is responsible for a specific product, and the factory method is also unique. Generally, there is only one factory method in a specific factory or a set of overloaded factory methods. But sometimes we need a factory that can provide multiple product objects instead of a single product object.

To help you understand the above paragraph, go back to the mobile phone store example from the factory approach above. In the above scenario, the store only mentioned mobile phones, but have you ever seen a brand store selling only mobile phones? More than that! For example, Huawei exclusive stores in the market now sell product families rather than single products. They also produce and sell Huawei mobile phones, Huawei tablets, Huawei laptops and other products in different product grade structures.

Huawei mobile phones are in the mobile phone product grade structure, Huawei tablets are in the tablet PC product grade structure, and Huawei laptops are in the laptop product grade structure.

Diversified product structure, organized in a hierarchical structure

This pattern is defined as the abstract factory pattern.

⭐ Factory Patterns seen so far: Simple Factory Pattern — Factory Method Pattern — Abstract Factory Pattern (this article)

  1. Abstract factory pattern is the most abstract and general form of all factory patterns.
  2. The biggest difference between abstract factory mode and factory method mode is that the factory method mode aims at a product hierarchy structure; Abstract factory pattern needs to face multiple product level structure, one factory level structure can be responsible for the creation of product objects in multiple different product level structure.
  3. When a factory hierarchy needs to create all objects in a product family that belong to different product hierarchies, it is simpler and more efficient to use the abstract factory pattern than the factory method pattern.

define

Abstract factory pattern, also known as Kit pattern, is a creation pattern.

It provides an interface for creating a series of related or interdependent objects without specifying their concrete classes.

UML class diagrams

The model structure

The abstract factory pattern contains the following roles:

  • AbstractFactoryAbstract factory declares a set of methods for creating various abstract products
  • ConcreteFactory: Specific factory
  • AbstractProductAbstract product
  • ProductConcrete products are different types of implementations of abstract products

More instances

The sample code

AbstractFactory.java

public abstract class AbstractFactory {
    public abstract AbstractProductA createProductA(a);
    public abstract AbstractProductB createProductB(a);
}
Copy the code

ConcreteFactory1.java

public class ConcreteFactory1 extends AbstractFactory {
    @Override
    public AbstractProductA createProductA(a) {
        return new ProductA1();
    }

    @Override
    public AbstractProductB createProductB(a) {
        return newProductB1(); }}Copy the code

ConcreteFactory2.java

public class ConcreteFactory2 extends AbstractFactory {
    @Override
    public AbstractProductA createProductA(a) {
        return new ProductA2();
    }

    @Override
    public AbstractProductB createProductB(a) {
        return newProductB2(); }}Copy the code

AbstractProductA.java

public abstract class AbstractProductA {
    public abstract void use(a);
}
Copy the code

ProductA1.java

public class ProductA1 extends AbstractProductA {
    @Override
    public void use(a) {
        // TODO}}Copy the code

ProductB1.java

public class ProductB1 extends AbstractProductB {
    @Override
    public void employ(a) {
        // TODO}}Copy the code

AbstractProductB.java

public abstract class AbstractProductB {
    public abstract void employ(a);
}
Copy the code

ProductA2.java

public class ProductA2 extends AbstractProductA {
    @Override
    public void use(a) {
        // TODO}}Copy the code

ProductB2.java

public class ProductB2 extends AbstractProductB {
    @Override
    public void employ(a) {
        // TODO}}Copy the code

The advantages and disadvantages

✔ The abstract factory pattern isolates the generation of concrete classes so that clients don’t need to know what is being created (same as the factory method pattern). Because of this isolation, it is relatively easy to change a specific plant. All concrete factories implement the common interfaces defined in the abstract factory, so simply changing an instance of a concrete factory can change the behavior of the entire software system to some extent. In addition, high cohesion and low coupling can be achieved by using abstract factory pattern.

➤ When multiple objects in a product family are designed to work together, it can ensure that the client always uses only objects in the same product family. This is a very useful design pattern for software systems that need to determine their behavior based on the current environment.

✔ It is convenient to add new specific factories and product families, without modifying the existing system, in line with the “open and closed principle”.

❌ product in to add a new object, it is difficult to extend the abstract factory to produce new kinds products, because in the abstract factory has ruled all the product collection, may be created to support new kinds of products (that is, the extension product hierarchy) means to interface provided by the abstract factory to expand, and this will involve the role of the abstract factory and all its subclasses of modification, It will obviously cause great inconvenience.

❌ Inclination of the open-close principle: it is easy to add new factories and product families, but troublesome to add new product grade structures.

Applicable scenario

The abstract factory pattern is recommended 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, combined, and expressed.
  • There is more than one product family 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, all of which appear in the same interface, so that the client is implementation-independent.

“Abstract Factory pattern” landing

(1) Java SE AWT: In the AWT of Java language, the abstract factory mode is used. It uses the abstract factory mode to realize the appearance of the application in different operating systems and the operating system.

(2) In many software systems, the interface theme needs to be changed. When buttons, text boxes and background colors are required to change together in the interface, abstract factory mode can be used for design.

Model extension

Degradation of factory mode:

  • When each concrete factory class in the abstract factory pattern creates only one product object (that is, only one product hierarchy exists), the abstract factory pattern degenerates into the factory method pattern.
  • The factory method pattern degenerates into a simple factory pattern when abstract factories are merged with concrete factories in the factory method pattern, providing a unified factory to create product objects, and designing the factory method for creating objects as a static method.

Summary “Factory Model”

The factory pattern is one of the most commonly used design patterns. This type of design pattern is the creation pattern, which provides the best way to create objects.

There are three factory modes:

  • Simple Factory model
  • Factory method pattern
  • Abstract Factory pattern

The factory pattern allows code to be structured cleanly and effectively encapsulate points of change (the concept of the open close principle); At the same time, the specific product class is shielded for callers; High cohesion and low coupling are achieved.

The last

👆 Previous article: “Design Patterns” 🏭 Factory Method

👇 Next article: Continuing article, please look forward to…

❤️ Good code without explanation, pay attention to the “rip design patterns” column, with me to learn design patterns, your code can be as elegant as poetry!

❤️ / END/If this article is helpful to you, click a “like” to support it, your support is my biggest motivation!