Simple comparison of three factory models and three factory models
For a long time, I wanted to thoroughly understand the design pattern, suffer from laziness, and I have been thinking about it all the time. Recently I have arranged some time, as if I had a lot of time out of thin air. Now I am working on something in the design pattern. The learning of the design pattern is necessary, especially for optimizing the code structure, the most obvious is the withdrawal.
1. Simple factory Model
UML:
Advantages and disadvantages:
This method demonstrates the advantages of the basic factory model. You do not need to pay attention to the creation process, but only need to tell the factory what it wants. The disadvantages of this method are also obvious, which violates the principle of opening and closing the design model (opening and closing the extension), because the factory must be rebuilt to increase the product.
2. Factory method mode
The factory method mode is used to solve the disadvantages of the simple factory mode. The factory method mode has an abstract factory. Different products that users need must inherit different instance factories of the abstract factory to create, in this way, whenever users need to produce different products, they only need to instantiate the corresponding factory instance and product model. The scalability is much better than that of the simple factory, but its disadvantages are also obvious, it is necessary to create many factory instances, and the number of classes increases a lot.
3. Abstract Factory Model
Compared with the factory method model, the abstract factory model can produce multiple products and have multiple abstract products. Each instance factory is equivalent to a family with the same product types among the many families, the instance factory has the function of customizing different types of accessories for a product.