23 design patterns (2) Java factory pattern
23 design patterns Part 2: Java factory pattern
definition:
Factory pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creation pattern, which provides the best way to create objects.
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 pattern is divided into three types according to the degree of abstraction:
Simple factory pattern (also known as static factory pattern), factory method pattern (also known as polymorphic factory) and abstract factory pattern (also known as toolbox)
Simple factory mode
In essence, a factory class dynamically determines which instance of a product class (which inherits from a parent class or interface) should be created according to the passed parameters. For the creation target of the simple factory pattern, all the created objects are instances of a specific class acting as this role.
Factory method model
Factory method is a design pattern with small granularity, because the representation of pattern is only an abstract method. Define the interface for creating objects in advance, and let the subclass decide to instantiate a specific class, that is, add an interface between the factory and the product. The factory is no longer responsible for the creation of the product. The interface returns specific class instances according to different conditions, which are implemented by specific class instances.
Abstract factory pattern
A factory pattern used when there are multiple Abstract roles. The abstract factory pattern can provide an interface for the client to create multiple product objects without specifying the specific product. It has multiple abstract product classes. Each abstract product class can derive multiple specific product classes. An abstract factory class can derive multiple specific factory classes. Each specific factory class can create multiple instances of specific product classes.
The factory method mode should be used more in practice. We take the factory method mode as an example (the example comes from Baidu to help understand)
Abstract product class: define car vehicle class
Define the actual product classes. There are two in total. Bike and bus represent different vehicle classes respectively
Define abstract factory interfaces
Specific factory subclasses. Create different factory subclasses for each specific product class
Simple test classes to verify that different factories can produce different product objects
Advantages of factory mode:
1. If a caller wants to create an object, he just needs to know its name, which reduces the degree of coupling. 2. High scalability. If you want to add a product, you can only extend a factory class. Make the code structure clearer. 3. Mask the specific implementation of the product, and the caller only cares about the product interface.
Disadvantages of factory mode:
Each time you add a product, you need to add a specific class and object implementation factory (which can be avoided by using reflection mechanism here), so that the number of classes in the system increases exponentially, which not only increases the complexity of the system to a certain extent, but also increases the dependence of the specific class of the system. Therefore, for simple objects, using factory mode increases the complexity.
Applicable scenarios of factory mode:
1. An object has many subclasses. 2. Creating an object requires many additional operations. 3. The system needs to be extended frequently in the later stage. It assigns the task of object instantiation to the implementation class, which has good expansibility.
Some frequently asked questions about factory patterns in Java:
Using the downward transformation of the parent class (using the reference of the parent class type to point to the object of the child class) can achieve the effect similar to the factory mode. Why use the factory mode? Assigning the parent class reference pointing to the child class object to the child class reference is called downward transformation, such as:
When using downward transformation to instantiate a subclass on the client side, it heavily depends on the name of the specific subclass. When we need to change the construction method of subclasses, such as adding a parameter or changing the class name of subclasses, all new subclasses need to be changed. However, if we use the factory mode, we only need to modify the code of new in the factory, and the code used in this instance in other projects will be changed accordingly without manual operation. (???)
Summary:
Whether simple factory pattern, factory pattern or abstract factory pattern, they essentially extract the invariant parts and leave the variable parts as interfaces to achieve maximum reuse. Which design pattern is more suitable depends on the specific business needs.
Transferred from: Java confidant
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.