Three factory brothers

Simple factory

1. When we instantiate an object, we usually use the  New keyword, but with a factory, we can use the factory when declaring the object. Using new makes the code not flexible enough, and using the factory to instantiate objects is very flexible!

2. The advantages and disadvantages of the simple factory model (look at the code)
Advantages
(1) The simple factory contains the necessary judgment logic, and the simple factory realizes the separation of the creation and use of objects. 
(2) The client does not need to know the category name of the specific product category created, only the parameters corresponding to the specific product category! 
(3) Replace and add new specific product categories without modifying any client code, which improves the flexibility of the system to a certain extent

Disadvantages
(1) The responsibilities of the factory class are too heavy. It can be seen from the class diagram that the simple factory contains logical judgment statements of addition, subtraction, multiplication, and division. Once it has problems, the entire system will have problems 
(2) When adding new classes Sometimes, for example, if I added the root-opening operation, the simple factory class in the system would have to be modified, which violated the open-closed principle! This is not conducive to the expansion and maintenance of the system! 
(3) The static method of the simple factory prevents the role of the factory from forming a hierarchical structure based on inheritance!
Factory method

1. The factory method is a further extension of the simple factory. This is because the simple factory violates the open-closed principle, while the factory method can perfectly solve this problem at this time! Let's take a look at how it is solved!

2.
Advantages and disadvantages of the factory method Advantages:
(1) The factory method is used to create the product that the customer needs, and at the same time hides the details of which specific product category will be instantiated. The user only needs to pay attention to the factory, not the created one. detail! You can see from the client code! Just know the corresponding factory! 
(2) There is no need to modify the code when adding and modifying a new calculation class, just add the corresponding factory, which is in full compliance with the open-closed principle! 
(3) The details of creating objects are completely encapsulated in concrete factories, and with abstract factory classes, all concrete factories inherit their parent classes! Perfectly embodies polymorphism!

Disadvantages:
(1) When adding new products (algorithms corresponding to UML diagrams), new factory classes must also be added, which will bring additional overhead
(2) The addition of  abstraction layers increases the degree of understanding

Abstract factory

1. The abstract factory pattern is a further extension of the factory method pattern, because it provides a more powerful factory class and has better scalability

2. From the class diagram, we will find that adding an algorithm family (product family) is very simple, while adding a new algorithm (product) will be very complicated! For example, if I add addition, subtraction, multiplication, and division, then I can directly increase, and the factory adds a factory by the way, so that the principle of openness and closedness is perfectly utilized! Excellent! But if I add a new algorithm, such as exponentiation, then it will also increase the types of exponentiation, exponentiation, and exponentiation. At the same time, I need to modify Ifactory, Zhaozhao Factory, and Liguang Factory! Violated the principle of openness and closedness
 

Guess you like

Origin blog.csdn.net/yanghezheng/article/details/104010953