Let's talk about the simple factory model, factory method model, and abstract factory model:
Simple factory pattern ):The factory class contains the necessary logical judgment, and the related class is dynamically instantiated Based on the client selection conditions. That is to say, the product creation logic is concentrated in a factory class, and the client only needs to pass different parameters to the factory, in this case, a factory creates a product, and all the specific factories inherit from an abstract factory. for the client, there is no dependency with the specific product;
Factory method pattern ):Define the interface used to create objects in advance so that the subclass decides to instantiate a specific class, that is, add interfaces between the factory and the product, and the factory is no longer responsible for product creation, the interface returns a specific class instance based on different conditions, which is implemented by a specific class instance;
Abstract factory pattern ):Provides an interface for creating a system or mutually dependent objects. You do not need to specify a specific class. Abstract Factory, as its name says, abstracts factory interfaces, so it targets multiple equal level structures, the object creation principle is to divide the objects to be created based on objects with similar functions.
Simple factory does not belong to 23 basic design patterns. It is a special case of abstract factory patterns. The difference between Abstract Factory methods and factory methods lies in their different abstract objects: factory methods abstract products, abstract Factory abstraction targets factories. Therefore, we can think that the factory method is an extreme situation of abstract factories. The factory method mode is used to create a hierarchical structure of a product. Generally, there is only one method to create a product; abstract Factory is used to create the hierarchical structure of multiple products. Generally, there are multiple methods to create a series of products.