An example of the factory method pattern of Java design pattern

  • 2020-04-01 04:21:36
  • OfStack

This article illustrates the factory method pattern of Java design patterns. Share with you for your reference, as follows:

The factory method pattern is used so frequently that we see it in our daily development. Define an interface for creating an object, but let subclass decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses. A factory method is an instantiation of a class deferred to its subclasses.

Advantages of the factory method model:

1. Good encapsulation, clear code structure. An object creation is conditional, if a caller needs a specific product object, just know the product's class name (or constraint string), not the painstaking process of object creation, and reduce the coupling between modules.
2. The expansibility of the factory method pattern is excellent. In the case of adding a product class, you can "embrace change" by modifying a specific factory class appropriately or extending a factory class.
3. Blocking products. It is very important that the caller does not care how the implementation of the product class changes, it only CARES about the interface of the product, and as long as the interface remains the same, 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, it is the factory class that determines which product a product object is generated from.
4, the factory method pattern is a typical decoupling framework. High-level module values need to know the abstract class of the product, other implementation classes are not concerned, in accordance with Demeter's law, we do not need to communicate; Also conforms to the dependency inversion principle, which only depends on the abstract class of the product; And, of course, in line with the Richter substitution principle, you can replace the parent with a subclass of the product, no problem.

The general code for the factory method pattern is as follows:


//Abstract product class
public abstract class Product {
  //Public methods of the product class
  public void method1() { 
    //Business logic processing
  } 
  //Abstract methods
  public abstract void method2();
} 

The concrete product class can have many, all inherit from the abstract product class, 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 {
  public void method2() { 
    //Business logic processing
  } 
}

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


//Abstract factory class
public abstract class Creator { 
   
  public abstract <T extends Product> T createProduct(Class<T> cls);
} 

Specific how to produce a product object, is a specific factory class, the source code is as follows:


//Specific factory
public 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 invocation method of the scenario class is as follows:


//Scene: the class
public class Client { 
  public static void main(String[] args) { 
    Creator creator = new ConcreteCreator(); 
    Product product = creator.createProduct(ConcreteProduct1.class);
     
  } 
}

To change the general code is a more practical, easy to extend the framework, readers can according to the actual project needs to extend.

I hope this article has been helpful to you in Java programming.


Related articles: