Detailed explanation of factory pattern implementation method of Java design pattern
This paper describes the factory pattern implementation method of Java design pattern. Share with you for your reference, as follows:
Factory mode mainly provides a transition interface for creating objects, so as to shield and isolate the specific process of creating objects, so as to improve flexibility
The factory mode is divided into three categories:
1) Simple factory: it is not conducive to the generation of series products; 2) factory method: it is also called polymorphic factory; 3) Abstract Factory: it is also called toolbox, which generates product family, but it is not conducive to the generation of new products;
1、 Simple factory mode
Simple factory pattern is also called static factory method pattern. You can see from renaming that this pattern must be very simple. It exists for a simple purpose: to define an interface for creating objects. In the simple factory mode, a factory class is at the center of the instantiation call of the product class. It determines which product class should be instantiated, just as a traffic policeman stands in the traffic flow and decides to release vehicles in that direction. Let's take a look at its composition:
1) Factory role: This is the core of this model and contains certain business logic and judgment logic. In Java, it is often implemented by a concrete class. 2) Abstract product role: it is generally the parent class inherited by a specific product or the interface implemented. In Java, it is implemented by interfaces or abstract classes. 3) Specific product role: the object created by the factory class is an instance of this role. It is implemented by a concrete class in Java.
2、 Factory method model
The factory method pattern is a further abstraction and generalization of the simple factory pattern. In the factory method pattern, it is no longer determined by only one factory class that a product class should be instantiated. This decision is handed over to the subclass of the abstract factory. Let's look at its composition:
1) Abstract factory role: This is the core of the factory method pattern, which is independent of the application. It is the interface that the specific factory role must implement or the parent class that must inherit. In Java, it is implemented by abstract classes or interfaces. 2) Specific factory role: it contains code related to specific business logic. Called by the application to create an object corresponding to a specific product. 3) Abstract product role: it is the parent class inherited by a specific product or the interface implemented. In Java, there are usually abstract classes or interfaces to implement. 4) Specific product role: the object created by a specific factory role is an instance of this role. It is implemented by concrete classes in Java.
The factory method pattern replaces the "God class" in the simple factory pattern with multiple subclasses inherited from the abstract factory role. As mentioned above, this will share the pressure on the object; Moreover, this makes the structure more flexible - when a new product (i.e. an upstart car) is produced, as long as it is generated according to the contract provided by the abstract product role and the abstract factory role, it can be used by customers without modifying any existing code. It can be seen that the structure of the factory role also conforms to the opening and closing principle!
The code is as follows:
3、 Abstract factory pattern
The code is as follows
In the abstract factory pattern, abstract products may be one or more, thus forming one or more product families. In the case of only one product family, the abstract factory pattern actually degenerates to the factory method pattern.
Summary:
(1) Simple factory mode is that a specific class creates instances of other classes. The parent class is the same and the parent class is concrete. (2) factory method mode has an abstract parent class to define a public interface, and the subclass is responsible for generating specific objects. The purpose of this is to delay the instantiation of the class to the subclass. (3) Abstract factory pattern provides an interface to create a series of related or interdependent objects without specifying their specific classes. It is aimed at the hierarchical structure of multiple products, while factory method pattern is aimed at the hierarchical structure of a product.
For more Java related content, interested readers can view the special topics of this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills