A brief analysis of factory method pattern examples of Java design patterns

This paper describes the factory method pattern of Java design pattern. Share with you for your reference, as follows:

The frequency of using factory method pattern is very high, and we can always meet it in our daily development. Define an interface for creating an object, but let subclass decide which class to instance Factory Method lets a class defer instantiation to subclasses. (define an interface for creating objects and let subclasses decide which class to instantiate. Factory method is to delay the instantiation of a class to its subclasses.)

Advantages of factory method mode:

1. Good encapsulation and clear code structure. The creation of an object is conditional. For example, a caller needs a specific product object, You only need to know the class name (or constraint string) of the product. You don't need to know the arduous process of creating objects to reduce the coupling between modules. 2. The extensibility of the factory method pattern is very excellent. When adding product classes, you can complete "embracing change" by appropriately modifying specific factory classes or extending a factory class 。 3. Shielding products. This is very important. The caller does not need to care about how the implementation of the product class changes. It only needs to care about the product interface. As long as the interface remains unchanged, the upper module in the system does not need to change. Because the instantiation of a product class is the responsibility of the factory class, the specific product of a product object is determined by the factory class. 4. The factory method pattern is a typical decoupling framework.. The high-level module value needs to know the abstract class of the product. Other implementation classes don't need to care. It conforms to the Demeter's law. We don't communicate what we don't need; It also conforms to the dependency inversion principle and only depends on the abstract class of the product; Of course, it also complies with the Richter replacement principle. It is no problem to replace the product parent class with the product subclass.

The general code of the factory method mode is as follows:

There can be more than one specific product class, all of which are inherited from the abstract product class. The source code is as follows:

The abstract factory class is responsible for defining the generation of product objects. The source code is as follows:

How to generate a product object is implemented by a specific factory class. The source code is as follows:

The calling method of scenario class is as follows:

Changing the general code is a more practical and easy to expand framework. Readers can expand it according to the actual needs of the project.

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>