In object-oriented programming, we often use the keyword "new". At the same time, object-oriented programming can achieve polymorphism. In this way, we often use the parent class or interface to define a variable, when this variable is used, a new specific object is added. However, sometimes, the new object is not definite. It may be that different subclasses of the new object are generated according to different scenarios, this is very simple and can be determined through if or switch. However, if this happens in many places, isn't it necessary to write the same code in these places? Is that true?
The factory mode is used to generate such objects. There are three factory models: simple factory, factory method, and abstract factory.
Simple Factory:
A simple factory abstracts the generation of an object into a separate class, so that you can directly call the create method of this class to return an object when generating the object. Sometimes, this method of generating classes can be written as static factory.
However, sometimes, the generation process of this class is different for different classes. For example, Class A only needs three classes: 1, 2, and 3, class B only generates Class 4, Class 5, and Class 6. If a simple factory is used, it seems redundant and logic processing is annoying, second, every multiple if statements will reduce the efficiency. We put the corresponding factory methods into different classes.
Factory method:
The factory method does not separate the process of generating classes into a class, but defines an abstract method createxx () in the parent classes A and B, and then in, the two subclasses of B Implement the createxx () method on their own.
Abstract Factory:
Abstract Factory can be used to generate a group of objects. For example, Class A and Class B must have a class 1, 2, and 3. These three classes are three types, which are not a parent class, there are also several types of objects. Class 1 includes 1A, 1B, 1C, and Class 2. What we need for Class A is 1A, 2B, 3A, objects B need 1b, 2a, 3c, So we define an abstract factory class, which can be divided into three methods: create1 (), create2 (), creat3 (), and, then we inherit the two factory classes and return the factories required by Class A and Class B respectively. We inject these two factory implementation classes into the constructors of Class A and Class B, you can call their create1, create2, and create3 methods to obtain the expected 3 types: 1, 2, and 3.
Design principles:
A design principle is used in the factory model, and the dependency inversion principle is as follows:
Dependency inversion principle: dependency abstraction, not dependent on specific classes
In fact, I feel that the parent class is defined when a variable is defined, instead of a specific subclass. The object construction requires dynamic generation. to invert dependencies, you can follow the following method:
Variables cannot hold references to specific classes. Do not allow classes to be derived from specific classes. Do not overwrite methods implemented in the base classes.
Design Mode-factory Mode