A detailed example of the java factory pattern

  • 2020-11-03 22:12:20
  • OfStack

A detailed example of the java factory pattern

The "factory" in the factory method is the same as what we normally understand: used to produce products.

And the customer is to deal with the product, so the significance of the factory method model is to separate the customer and product, to achieve decoupling and more flexible purposes.

1 Usually we have an abstract class of a product, and then we have several concrete products, as follows:


// Abstract product roles 
public interface Product{
  void product();
}
// Specific products 1
public class Pro1 implements Product{
  @Override
  public void product() {
    System.out.println(" product 1");
  }
}
// Specific products 2
public class Pro2 implements Product{
  @Override
  public void product() {
    System.out.println(" product 2");
  }
}

Then the customer wants to buy the product. Instead of directly contacting with the product, he USES the factory, so we have one factory:


public class Factory { 
    public static Product buy(type) { 
      switch (type) { 

      case 1: 
        return new Pro1(); 

      case 2: 
        return new Pro2(); 

      default: 
        break; 
      } 
      return null; 
    } 
  } 

Customer via ES14en. buy(type); You can buy the products you want.

This is the simple factory pattern, also known as the static factory method pattern.

If one new product is added, we can write one more product class Pro3. However, in the factory, we need to add new code to produce, which obviously does not conform to the "open closed principle (open to expansion; So we abstract the factory and add a concrete factory class. The customer chooses a certain factory as follows:


// The abstract factory 
public interface Factory {
  Product buy();
}
// The specific factory 1
public class Factory1 implements Factory {
  @Override
  public Product buy() {
    return new Pro1();
  }
}
// The specific factory 2
public class Factory2 implements Factory {
  @Override
  public Product buy() {
    return new Pro2();
  }
}

When a new product is added, we can create one more specific factory to produce it. But when the product category is very large, there will be a large number of corresponding factory objects, which is not what we want.

-- This is the factory method pattern

When a product has multiple abstractions, we may have multiple product interfaces, and this scenario becomes what we call it

-- Abstract factory pattern

Conclusion:

Whether it is the simple factory pattern, the factory method pattern, or the abstract factory pattern, they all belong to the factory pattern and are very similar in form and characteristics, and their ultimate goal is to understand each other. When used, we don't have to worry about whether the pattern is a factory method pattern or an abstract factory pattern, because the evolution between them is often confusing. Often you will find that the factory method pattern you are clearly using becomes an abstract factory pattern because the products in the class make up a family of products in a different hierarchy when a new requirement comes along and a new method is added. In the case of the abstract factory pattern, it evolves into the factory method pattern when one method is removed so that the supplied products no longer constitute a family of products. Therefore, when using factory mode, you only need to care about whether the purpose of reducing coupling is achieved.

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


Related articles: