This article describes the factory method model of Java design pattern. Share it for your reference, as follows:
The factory method model is used very frequently and can always be seen in our daily development. It is defined as: Define an interface for creating an object, but let subclass decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses .(Define an interface for creating an object, letting the subclass decide which class to instantiate . A factory method is the instantiation of a class delayed to its subclass. )
Advantages of factory method model:
1. Good encapsulation and clear code structure. An object creation is conditional constraint. If a caller needs a specific product object, just know the class name (or constraint string) of the product. There is no need to know the difficult process of creating an object, and reduce the coupling between modules. .
2. The factory method model has excellent scalability. When adding product categories, as long as you appropriately modify the specific factory category or expand a factory category, you can complete the "embrace change".
3. Block product categories. This is very important. Callers do not need to care about how the implementation of product classes changes. It only needs to care about the product interface. As long as the interface remains unchanged, the upper-level modules in the system do not need to change. Because the instantiation of a product class is the responsibility of the factory class, the specific product generation of a product object is determined by the factory class.
4. The factory method model is a typical decoupling framework. . High-level module values need to know the abstract class of the product. No need to care about other implementation classes. They comply with the Dimitese law. If we don’t need it, don’t communicate. They also comply with the principle of dependency inversion and only rely on the abstract class of the product. Of course, they also comply with the inside. The principle of replacement is to use product subclass to replace product parent class, no problem.
The common code for the factory method pattern is as follows:
//Abstract product class public abstract class Product { //Public method of product class public void method1() { //Business logic processing} //Abstract method public abstract void method2();}There can be multiple specific product categories, all inherited from abstract product categories, the source code is as follows:
//Specific product category public class ConcreteProduct1 extends Product { public void method2() { //Business logic processing} } public class ConcreteProduct2 extends Product { publ ic void method2() { //Business logic processing} }The abstract factory class is responsible for defining the generation of product objects, the source code is as follows:
//Abstract factory class public abstract class Creator { /** *Create a product class whose input parameter types can be set by yourself, usually String, Enum, Class, of course, can be empty*/ public abstract <T extends Product> T createProdu ct (Class<T> cls);}How to generate a product object is implemented in specific factory classes, the source code is as follows:
//Specific factory class ConcteteCreator extends Creator { public <T extends Product> T createProduct(Class<T> cls) { Product product = null; try { product = (Product)Class.forName(cls.getName()). newInstance(); } catch (Exception e) { //Exception handling} return (T)product; } }The calling method of the scene class is as follows:
//Scene class public class Client { public static void main(String[] args) { Creator creator = new ConcreteCreator(); Product product = creator.createProduct(ConcretePr oduct1.class); /** *Continue business processing*/ } }Changing general code is a relatively practical and easy-to-extend framework, and readers can expand according to actual project needs.
I hope this article will be helpful to everyone's Java programming.