Design Pattern (Design Pattern) Simple Factory Pattern (Simple Factory Pattern)

Design Pattern (Design Pattern) Simple Factory Pattern (Simple Factory Pattern)

The simple factory pattern is indeed simple enough to point the reference of the parent class to the object of the subclass, and hand the creation process of the subclass to the factory class to complete.

In the structure diagram, the four operations of addition, subtraction, multiplication and division are all inherited from the operation class. When the program needs to use one operation class, whether it is addition, subtraction, multiplication and division, you can use the following code:

Operation oper = Factory.createOperate("+");

Although the simple factory encapsulates the creation process of the object, the user does not need to know the specific object creation process, but when adding a new class, the creation method of the factory will also be modified, which obviously violates the open-closed principle.

The so-called open-closed principle refers to being open to expansion and closed to modification. In other words, we can carry out unlimited expansion on the basis of the existing code, but cannot modify the existing code.

Reference: https://cloud.tencent.com/developer/article/1513827 Design Pattern (Simple Factory Pattern)-Cloud + Community-Tencent Cloud