Java design pattern — detailed explanation of factory design pattern
Factory pattern: it is mainly used to instantiate classes with common interfaces. Factory pattern can dynamically determine which class should be instantiated.
Form of factory mode
The factory mode mainly uses the following forms:
1: Simple factory. 2: factory method. 3: abstract factory.
Simple factory
Also known as static factory, it is the simplest structure in the third state of factory mode. There is mainly a static method to accept parameters and decide to return instances of different classes implementing the same interface according to the parameters. Let's take a specific example: suppose a factory produces washing machines, refrigerators, air conditioners, etc We first define a common product interface for all products
Then we make all products in this factory must implement this interface
Next, let's write a factory class, which is responsible for producing the above products
Well, with this factory class, we can start placing orders. Simplefactory will decide what products to produce according to different order classes.
As can be seen from the above code, the core of the simple factory is a simplefactory class, which has the necessary logical judgment ability and the right to create all products. We only need to give him the order to get the products we want. It seems very convenient to use.
However, in fact, this simplefactory has many limitations. First, every time we want to add a new product, we must modify the original code of simplefactory. Secondly, when we have many products and there are complex hierarchical relationships between products, this class must have complex logical judgment ability, and its code volume will continue to surge, which is a terrible word for future maintenance
In addition, the whole system relies heavily on the simplefactory class. As soon as the simplefactory class goes wrong, the system will enter a state of failure, which is also the most fatal point
The above deficiencies will be solved in the other two states of factory mode.
Factory method
The above code tells us that a simple factory is not simple. It is the core of the whole model. Once something goes wrong, the whole model will be affected and cannot work. In order to reduce risks and prepare for future maintenance and expansion, we need to reconstruct it and introduce the factory method.
The factory method defines the interface for the factory class, and uses polymorphism to weaken the function of the factory class. The following is the definition of the factory interface:
Let's define a product interface again
The following is the product class that implements the product interface
Next, it is the core part of the factory method, that is, the specific factory class that specifically creates the product object,
As can be seen from the above code for creating product objects, the main difference between factory methods and simple factories is that simple factories place the functions of creating products in one class, while factory methods place different products in different factory classes that implement factory interfaces. In this way, even if one factory class has a problem, other factory classes can work normally, They are not affected by each other. When adding new products in the future, you only need to add a factory class that implements the factory interface, which can be achieved without modifying the existing code. However, the factory method also has its limitations, that is, when the products faced have a complex hierarchical structure. For example, the factory not only produces household appliances, but also produces mobile phone products. In this way, household appliances and mobile phones are two product families. These two families include a large number of products, and each product has multiple models, This forms a complex product tree. If you use the factory method to design this product family system, you must create a corresponding factory class for each model of products. When there are hundreds or even thousands of products, there must also be hundreds or thousands of factory classes. This leads to the legendary class explosion, which is a disaster for future maintenance
Abstract factory method
Abstract factory: the intention is to create a series of interrelated or interdependent objects<< Java Design Patterns > >
I think the abstract factory introduces the concept of classified management based on the factory method
The factory method is used to create a product without the concept of classification, while the abstract factory is used to create a series of products, so product classification has become the focus of the abstract factory. We continue to use the above example to illustrate:
All products produced by the factory use capital letters to indicate their models. For example, refrigerators have "refrigerator-a" and "refrigerator-b". Similarly, other products also follow this numbering rule, so there is a product family tree
Refrigerator:
Refrigerator-a refrigerator-b
Washing machine:
Washing machine-a washing machine-b
We can define two product interfaces for refrigerators and washing machines to classify them
Next, we create the specific products of these two interfaces respectively
Now, we are ready for the product part. Next, let's deal with the factory part. Let's define the factory behavior interface first
Next, I create a specific factory class. According to the above product interface, we divide the products of model a into one class, which is managed by one factory, and the products of model B are managed by another factory. According to this classification, we can realize the following two specific factory classes
In this way, our abstract factory is completed. As can be seen from the above, in terms of application, I think factory methods and abstract factories have their own application scenarios, and there is no difference between advantages and disadvantages. However, before applying Abstract factories, it is important to systematically classify the created objects. Good product classification rules can provide clear ideas for the selection, invocation and future expansion of specific factory classes
summary
The above is all about the detailed explanation of Java design pattern factory design pattern in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Visitor mode usage scenarios and code examples of Java design patterns, abstract factory code examples of Java design pattern notes, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!