I learned the simple factory model and did not understand it very deeply. Today I just saw the factory method model, and I have a better understanding of it.
First, both the simple factory mode and the factory method mode extract the unchanged places and encapsulate the changes easily. To achieve a large degree of reuse, user changes, and project expansion.
Simple factory model understanding
Also known as the static factory mode, it specifically defines a class to create instances of other classes. The created instances usually have the same parent class. The factory class dynamically determines the product class instance to be created based on the input parameters. It contains the necessary judgment logic and determines the object of the specific class according to the information given by the outside world. The simple factory model can be understood as a father giving his son a certain amount of money, specifying the money that can be used to go to school, buy a house or buy a car, and then letting him choose which one to use.
Advantages
The factory class contains the necessary logical judgment to determine when to create an instance of the class. The client can avoid directly creating objects. In this way, responsibility can be divided, coupling can be reduced, specific responsibilities and powers can be clearly defined, and the optimization of the entire system can be realized.
Disadvantages
When a product has a complex multi-layer structure, there is only one factory class. At this time, it will become its biggest disadvantage when it remains unchanged. Because the factory class is the core of the entire organization, it aggregates the creation logic of all products. Once the factory fails to work normally, the entire system will be affected and the scalability will be poor. If there are new requirements for poor scalability, You have to modify the factory logic, which leads to complicated factory logic and violates the open-close principle. At the same time, the static factory method is not conducive to the formation of a hierarchy based on inheritance.
Understanding of factory method Modes
It is a small-Granularity Design Model, because the expression of the model is only an abstract method. The factory method mode defines an interface for creating objects, so that the subclass determines which class to instantiate. That is to say, when an interface is added between the factory and the product, the factory is no longer responsible for product implementation. There is an excuse to return different class instances for different conditions and then implement them by specific class instances. The derivative of the simple factory in the factory method improves the disadvantages of many simple factories and follows the open-closed principle to achieve scalability and can be used in more complex product results. The factory method can be understood as the father gave his son a sum of money, and then directly let the son go to control, how to spend his father no matter.
Advantages
Factory method mode Customer Service has many disadvantages of a simple factory. Each specific factory only performs a single task and follows the open-close principle. The code is concise and has good scalability.
Disadvantages
If a product class needs to be modified, the corresponding factory class also needs to be modified. Once there are multiple product categories that need to be modified, the question of "check" appears. This is a complicated change to the factory category. Therefore, although the factory method mode is conducive to expansion, it is not conducive to maintenance.
To sum up, we can know which mode is better for programming for different situations. When few objects need to be created, the customer only knows the parameters passed in to the factory, and does not care about how to create objects, the simple factory mode can be used; the factory method mode can be used when the class delegates the responsibility of creating an object to one of multiple help sub-classes.