Creational pattern-abstract factory pattern understanding

Table of contents

1 Introduction:

2. Abstract factory demo optimization

2.1 Scenario description

2.2 Abstract factory implementation

3. Advantages and disadvantages


1 Introduction:

The previous article wrote about creational mode-factory method mode demo understanding

One disadvantage of the factory method pattern is that with the increase of new subclasses, the number of java classes will be created more

How to optimize this shortcoming, you can use the abstract factory pattern to optimize,

The premise is to try to expand the full amount of subcategories at one time, which is inconvenient for later maintenance;

The simple point is to create a set of corresponding different indicator calculation APIs in a common class when all indicators have been determined

2. Abstract factory demo optimization

2.1 Scenario description

To quote a blog:

In the factory method pattern, specific factories are responsible for producing specific products. Each specific factory corresponds to a specific product. The factory method is unique. Generally, there is only one or a group of overloaded factory methods in a specific factory. But sometimes we hope that a factory can provide multiple product objects instead of a single product object, such as an electrical appliance factory, which can produce TVs, refrigerators, air conditioners and other electrical appliances instead of only producing a certain type of electrical appliance. In order to better understand the abstract factory pattern, we first introduce two concepts:

       (1) Product level structure: The product level structure is the product inheritance structure. For example, if an abstract class is TV, and its subclasses include Haier TV, Hisense TV, and TCL TV, then the abstract TV and the specific brand of TV A product hierarchy structure is formed among them, the abstract TV set is the parent category, and the specific brand TV set is its subcategory.

       (2) Product family: In the abstract factory model, a product family refers to a group of products produced by the same factory and located in different product hierarchy structures, such as Haier TVs, Haier refrigerators, and Haier TVs produced by Haier Electric Factory. Televisions are located in the hierarchical structure of television products, and Haier refrigerators are located in the hierarchical structure of refrigerators. Haier TVs and Haier refrigerators constitute a product family.

       The schematic diagram of product level structure and product family is shown in the figure:

Multiple squares, circles and ellipses of different colors form three different product hierarchy structures, while squares, circles and ellipses of the same color form a product family, and each shape object belongs to a product family , and belongs to a product hierarchy. There are five product families in Figure 3, belonging to three different product hierarchy structures. We can uniquely identify a product only by specifying the product family it belongs to and the hierarchical structure it belongs to.

       The abstract factory pattern can be used when the specific product produced by the factory provided by the system is not a simple object, but multiple specific products that are located in different product hierarchy structures and belong to different types. The abstract factory pattern is the most abstract and general form of all factory patterns. 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, and a factory hierarchy structure can be responsible for multiple different product hierarchy structures. Creation of product objects. When a factory hierarchy can create all objects in a product family belonging to different product hierarchies, the abstract factory pattern is simpler and more efficient than the factory method pattern. The schematic diagram of the abstract factory pattern is shown in Figure 4
 

Each specific factory can produce all products belonging to a product family, such as producing squares, circles and ovals with the same color, and the products produced are located in different product hierarchy structures. If the factory method pattern is used, the structure shown in Figure 4 needs to provide 15 concrete factories, while the abstract factory pattern only needs to provide 5 concrete factories, which greatly reduces the number of classes in the system.

2.2 Abstract factory implementation

The following roles are included in the abstract factory pattern structure diagram:

       ● AbstractFactory (abstract factory): It declares a set of methods used to create a family of products, each method corresponds to a product.

       ● ConcreteFactory (concrete factory): It implements the method of creating products declared in the abstract factory, and generates a group of specific products. These products form a product family, and each product is located in a product hierarchy.

       ● AbstractProduct (abstract product): It declares the interface for each product, and declares the business methods of the product in the abstract product.

       ● ConcreteProduct (concrete product): It defines the specific product object produced by the specific factory, and realizes the business method declared in the abstract product interface.

       Multiple factory methods are declared in the abstract factory to create different types of products. The abstract factory can be an interface, an abstract class or a concrete class. The typical code is as follows:
 

abstract class AbstractFactory {
public abstract AbstractProductA createProductA(); //工厂方法一
public abstract AbstractProductB createProductB(); //工厂方法二
……
}

具体工厂实现了抽象工厂,每一个具体的工厂方法可以返回一个特定的产品对象,而同一个具体工厂所创建的产品对象构成了一个产品族。对于每一个具体工厂类,其典型代码如下所示:

class ConcreteFactory1 extends AbstractFactory {
    //工厂方法一
public AbstractProductA createProductA() {
    return new ConcreteProductA1();
}
 
//工厂方法二
public AbstractProductB createProductB() {
    return new ConcreteProductB1();
}
 
……
}

3. Advantages and disadvantages

To put it simply, the factory method pattern can be optimized,

 However, if it is troublesome to add a new product level structure later, the original system needs to be greatly modified, and even the code of the abstraction layer needs to be modified. This will obviously bring great inconvenience and violate the "open and close principle".


 

Guess you like

Origin blog.csdn.net/qq_44691484/article/details/130528314