Intensive reading of design pattern abstract factory Abstract Factory


Abstract factory is a kind of creation pattern. The abstract degree of factory class pattern is simple factory pattern, factory pattern and abstract factory pattern.

Intent: provide an interface to create a series of related or interdependent objects without specifying their specific classes.

For example

If you don’t understand the above intention introduction, it doesn’t matter. Design patterns need to be used in daily work. Combining with examples can deepen your understanding. Now I’ve prepared three examples to let you understand what scenarios will use this design pattern.

Automobile factory

We all know that there are many parts in the automobile. With the division of labor brought about by the industrial revolution, many parts can be easily replaced. But in real life, we consumers are not willing to do this. We hope that the parts of the BMW we buy are all of the same series, so as to ensure the maximum matching degree and bring better performance and comfort.

Therefore, consumers are not willing to go to the tire factory, steering wheel factory and window factory to purchase one by one. Instead, they put the demand to the abstract factory BMW, which is responsible for assembly. You are the boss of this factory. As we all know, the components of a car are fixed, but different parts have different models and come from different manufacturers. You need to launch several different combinations of models to meet the needs of consumers at different prices. How would you design them?

maze game

You make a maze game. The known elements are room, door and wall. The combination relationship between them is fixed. You generate a random maze through a set of algorithms, which call the factory of room, door and wall to generate the corresponding instance. But with the release of the new expansion, you need to generate rooms with new functions (which can restore physical strength), doors with new functions (which can only be opened by magic keys) and walls with new functions (which can be destroyed by bombs). However, modifying the existing maze generation algorithm violates the principle of opening and closing (which needs to be modified in existing objects). If you want to generate maze algorithm, you will not feel it at all How would you design for the existence of new materials?

Event linkage

Suppose we do a front-end building engine, and now we want to do a set of association mechanism, so that we can click on the cell of the table component to pop up a modal box and display a line chart inside. It is known that the business side needs to customize the table component, modal box component and line chart component, but the linkage between components is certain. How would you design it?

Interpretation of intention

In the case of a car factory, we know the components of a car,In order to assemble a car, the parts need to be assembled in a certain way, and the specific parts need to be expandable

In the example of maze game, we know that the components of maze are room, door and wall,In order to generate a maze, we need to use some algorithm to generate many instances of rooms, doors and walls. The algorithm does not care about which rooms, doors and walls to use, and it needs to be expanded

In the case of event linkage, we know that the basic elements of the Interaction scenario of this table pop-up trend chart are table component, modal box component and line chart component,We need to use some linkage mechanism to create linkage relationship among the three. What table, modal box component and line chart component are not concerned by this event linkage, and they need to be expanded, the form can be replaced by the form registered by any business party, as long as it meets the requirements of clickonClickThe mechanism is fine.

Intent: provide an interface to create a series of related or interdependent objects without specifying their specific classes.

Aren’t these three examples in line with the above intention? The abstract factory we want to design is toCreate a series of related or interdependent objectsIn the above examples, they are automobile components, maze game materials and event linkage components.There is no need to specify their specific classesThis means that we don’t care what brand the car’s steering wheel is made of, whether the maze room is an ordinary room, and whether the broken line diagram of the linkage mechanism is usedEchartsWe just need to describe the relationship between them,The advantage of this is that we don’t need to modify the abstract factory when we expand new steering wheel, new room and new line chart in the future.

Structure chart

Intensive reading of design pattern abstract factory Abstract Factory

AbstractFactoryThis is the abstract factory we want. It describes the abstract relationship of creating products, such as how to generate mazes and how to link tables and trend charts.

As for the specific steering wheel and room, it is decided by theConcreteFactorySo we may have multipleConcreteFactoryFor exampleConcreteFactory1The instantiated wall is an ordinary wall,ConcreteFactory2The instantiated wall is a magic wall, but its effect onAbstractFactoryThe interface is consistent, soAbstractFactoryYou don’t need to care which factory you call.

AbstractProductIs the product abstract class, which describes the creation methods of such as steering wheel, wall and line chartConcreteProductIt’s a specific way to realize the product, such asConcreteProduct1The created form is created usingcanvasYes, the line chart is in ChineseG2It’s painted, not paintedConcreteProduct2The created form is created usingdivYes, the line chart is in ChineseEchartsIt was painted.

In this way, when we want to expand a useRchartsDraw a line chart withsvgDraw a table withdivWhen drawing the event mechanism composed of modal boxes, you only need to create another oneConcreteFactory3Do the corresponding implementation, and then put thisConcreteFactory3Pass on toAbstractFactoryThere is no need to modifyAbstractFactoryThe method itself.

Code examples

The following example is written in JavaScript.

class AbstractFactory {
  createProducts(concreteFactory: ConcreteFactory) {
    const productA = concreteFactory.createProductA();
    const productB = concreteFactory.createProductB();
    //Establish a fixed association between ﹣ a ﹣ and ﹣ B ﹣ even if the implementation of ﹣ a ﹣ and ﹣ B ﹣ is replaced by any implementation, it will not be affected

productA.bind(productB)It is an abstract representation

  • For the example of automobile factory, it shows the process of assembling automobile.
  • For the example of maze game, it shows the process of generating maze.
  • For the example of event linkage, it represents the process of creating association between components.

Suppose we have two sets of materials in our maze, namely ordinary material and magic material. As long as we create ordinary material factories in our mazeConcreteFactoryA, and magic material factoryConcreteFactoryB, callcreateProductsWhen the input is ordinary material, the output is the maze built by ordinary material. When the input is magic material, the output is the maze built by magic material.

When we want to create a new set of maze materials, such as lava maze, we just need to create a set of lava materials (lava room, lava door, lava wall), and then assemble oneConcreteFactoryCLava material generation plant passes toAbstractFactory.createProductsThat’s it.

We can find that using the abstract factory mode, we can easily expand new materials, such as a new set of auto parts, a new set of maze materials, and a new set of event linkage components,This process only needs to create a new class, does not need to modify any class, in line with the open close principle


Any design pattern has its applicable scenarios, which in turn indicates that it is not applicable in some scenarios.

Or the above example, if our demand is not to expand a new wheel, new wall, new line chart, but to:

  • The car factory has to add a new component to the car: the autopilot system.
  • Maze game to add a functional material: trap.
  • Event linkage needs to add a new linkage object: detailed trend statistics table.

You see, this situation is not to add a new set of implementations for the existing elements, but to implement some new elements, which will be very complicated, because we not only need to add a new set of implementations for all elementsConcreteFactoryEach element is added and the abstract factory is modified to establish a connection between the new element and the old element, which violates the open close principle.

Therefore, for the system with fixed elements, it is suitable to use abstract factory, otherwise it is not.


Abstract factory is applicable to the implementation of a new existing product, but not to a new product category. You can refer to the following figure combined with an example for further understanding

Intensive reading of design pattern abstract factory Abstract Factory

How to expand a lava material packageAdd a product styleIt is suitable to use abstract factory design patternAdd a product category, not suitable for using abstract factory design pattern. Why? Look at the picture below

Intensive reading of design pattern abstract factory Abstract Factory

Creating labyrinth is what the abstract factory does,It is to associate existing rooms, doors and wallsBecause the operation is abstract class, it is easy to expand a set of concrete implementation (lava material package) without perception of this abstract factory.

However, if a new product category trap is added, we can see that the abstract factory must re-establish the association between the trap and the first three, which means modifying the abstract factory, which does not conform to the open close principle. At the same time, if we have material package 1 ~ 999, we need to add 999 corresponding traps (ordinary traps, magic traps, lava traps) at the same time, and the workload will be very heavy.

Therefore, the abstract factory design pattern is suitable only when the product type is stable and the product style needs to be expanded frequently.

The address for discussion is: Intensive Reading of “design pattern abstract factory abstract factory” · issue # 271 · DT Fe / weekly

If you want to participate in the discussion, please click here. There are new topics every week, which will be released on weekends or Mondays. Front end intensive reading – help you screen reliable content.

followFront end intensive reading WeChat official account

Intensive reading of design pattern abstract factory Abstract Factory

Copyright notice: free reproduction – non commercial – non derivative – keep signature (Creative sharing 3.0 License)