Explanation of java factory pattern and introduction of advantages and disadvantages

  • 2020-09-16 07:29:09
  • OfStack

Simple factory model introduction:

Summary:

The simple factory pattern, also known as the static factory method (Static Factory Method) pattern, belongs to the class creation pattern. In the simple factory pattern, the production method of a product is encapsulated into a factory class, which can return instances of different product classes depending on the parameters. The factory class is the class used to produce the product. The method of producing the product is put into the factory class. In the factory class, switch statement is used to control which kind of goods are produced.

Implementation code:


package scut.designmodel.SimpleFactoryPattern;

// Abstract product class 
abstract class Product{
  public abstract void Show();
}

// product A class 
class ProductA extends Product{

  @Override
  public void Show() {
    System.out.println(" The product was produced A");
  }
}

// product B class 
class ProductB extends Product{

  @Override
  public void Show() {
    System.out.println(" The product was produced C");
  }
}

// product C class 
class ProductC extends Product{

  @Override
  public void Show() {
    System.out.println(" The product was produced C");
  }
}

// Simple factory class 
class Factory {
  public static Product Manufacture(String ProductName){
    switch (ProductName){
      case "A":
        return new ProductA();

      case "B":
        return new ProductB();

      case "C":
        return new ProductC();

      default:
        return null;

    }
  }
}

// Production process of factory products 
public class SimpleFactoryPattern {
  public static void main(String[] args){
    Factory mFactory = new Factory();

    // Customers want products A
    try {
      mFactory.Manufacture("A").Show();
    }catch (NullPointerException e){
      System.out.println(" Without this 1 Kind of product ");
    }

    // Customers want products B
    try {
      mFactory.Manufacture("B").Show();
    }catch (NullPointerException e){
      System.out.println(" Without this 1 Kind of product ");
    }

    // Customers want products C
    try {
      mFactory.Manufacture("C").Show();
    }catch (NullPointerException e){
      System.out.println(" Without this 1 Kind of product ");
    }

    // Customers want products D
    try {
      mFactory.Manufacture("D").Show();
    }catch (NullPointerException e){
      System.out.println(" Without this 1 Kind of product ");
    }
  }
}

Results:


 The product was produced A
 The product was produced C
 The product was produced C
 Without this 1 Kind of product 

Application Scenario:

When not to use the factory pattern, each of the "product" classes are distributed, don't use a factory interface to integrate them, and to join productA case production need to 3 "raw material" parameter, the user must clearly know these parameters to the productA class instantiation, parameters of each product is different, will let users very messy, using a "factory", can encapsulate parameters inside, let users don't need to know the specific parameters can be needed to instantiate the class "products".

Advantages:

The job of creating an instance is separated from the job of working with an instance, and the consumer does not have to care about how class objects are created, thus clarifying the responsibilities.

Putting the work of initializing an instance in a factory makes the code easier to maintain. More object-oriented, programming to interfaces rather than to implementations.

Disadvantages:

Because the factory class centralizes all the product creation logic, once 1 does not work properly, the entire system is affected. To add a product class, you have to modify the factory class code, violating the open closure principle (open to extensions, closed to modifications). Because static factory methods are used in the simple factory pattern, static methods cannot be inherited or overridden, resulting in factory roles not being able to form an inheritable hierarchy.

Despite its drawbacks, the simple factory approach is widely used. Let's take a look at an updated version of it, the factory method pattern.

The above is the introduction of java factory mode, if you have any questions, please leave a message or go to the community of this site to exchange and discuss, thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: