Factory Method Factory Method Pattern (Java Code Implementation) - Creation Pattern

In the article I wrote about 23 design patterns, the preface is basically the same, readers can start reading from Chapter 2, this article is about 工厂方法模式(Factory Method Pattern)the detailed explanation of the creation pattern.

1 Introduction

According to the book Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Patterns - The Foundation of Reusable Object-Oriented Software), the four authors divide design patterns into three categories, as follows:

1. Creation Patterns
These design patterns provide a way to hide the creation logic while creating an object, instead of using the new operator to instantiate the object directly. This makes the program more flexible in deciding which objects need to be created for a given instance.

  • Singleton Pattern
  • Abstract Factory Pattern
  • 工厂方法模式(Factory Method Pattern)
  • Builder Pattern
  • Prototype Pattern

2. Structural Patterns
These design patterns focus on the composition of classes and objects. The concept of inheritance is used to compose interfaces and to define the way in which composite objects obtain new functionality.

  • Adapter Pattern
  • Bridge Pattern
  • Composite Pattern
  • Decorator Pattern
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern

3. Behavioral Patterns
These design patterns are particularly concerned with communication between objects.

  • Chain of Responsibility Pattern
  • Command Pattern
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern
  • Memento Pattern
  • Observer Pattern
  • State Pattern
  • Strategy Pattern
  • Template Pattern
  • Visitor Pattern

工厂方法模式(Factory Method Pattern)This article is about the detailed explanation in the creation mode .

2. Factory Method Pattern

2.1. Intent

Define an interface for creating objects and let subclasses decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.

2.2UML class diagram

In the factory mode, we do not expose the creation logic to the consumer when creating objects, and point to the newly created object by using a common interface (TeaFactory in the figure below), as follows (take the now popular milk tea as an example) :
insert image description here

2.3. Java code specific implementation

2.3.1 The package structure is as follows:

insert image description here

2.3.2tea package

public interface Tea {
    
    
    void make();
}
public class BlackTea implements Tea {
    
    


    @Override
    public void make() {
    
    
        System.out.println("make the black tea!");
    }
}
public class GreenTea implements Tea {
    
    
    @Override
    public void make() {
    
    
        System.out.println("make the green tea!");
    }
}
public class MilkTea implements Tea {
    
    
    @Override
    public void make() {
    
    
        System.out.println("make the milk tea!");
    }
}

2.3.3factory package

/**
 * @Author: YuShiwen
 * @Date: 2022/2/3 10:43 PM
 * @Version: 1.0
 */
public class TeaFactory {
    
    
    public Tea getTea(String tea) {
    
    
        if ("blackTea".equalsIgnoreCase(tea)) {
    
    
            return new BlackTea();
        } else if ("greenTea".equalsIgnoreCase(tea)) {
    
    
            return new GreenTea();
        } else if ("milkTea".equalsIgnoreCase(tea)) {
    
    
            return new MilkTea();
        } else {
    
    
            throw new RuntimeException("其他茶品暂不支持!");
        }
    }
}

2.3.4main package

public class CustomerDemo {
    
    
    public static void main(String[] args) {
    
    
        new TeaFactory().getTea("milktea").make();
        new TeaFactory().getTea("blacktea").make();
    }
}

2.3.5 Running Results

insert image description here

2.4. Advantages and disadvantages

Advantages:
1. If a caller wants to create an object, it only needs to know its name.
2. High scalability. If you want to add a product, you only need to extend a factory class.
3. The specific implementation of the shielding product is shielded, and the caller only cares about the interface of the product.

Disadvantage:
Every time a product is added, a specific class and object implementation factory need to be added, which doubles the number of classes in the system, increases the complexity of the system to a certain extent, and also increases the specific class of the system. rely.

Recommendation:
As a class creation pattern, wherever complex objects need to be generated, the factory method pattern can be used. One thing to note is that complex objects are suitable for using the factory pattern, while simple objects, especially those that can be created only through new, do not need to use the factory pattern. If you use the factory pattern, you need to introduce a factory class, which will increase the complexity of the system.

2.5 Application in source code

2.5.1Integer.valueOf() method

  • Integer It provides static method valueOf() to create Integer. So what is the difference between this method and writing new Integer(3) directly? We observe the valueOf() method:
    its advantage is that valueOf() internally may use new to create a new Integer instance, but it may also directly return a cached Integer instance.
  • Integer defaults to the cache range of [-128, 127]. We also found out when we looked at the source code. The lower limit of low is the final int low = -128, which was fixed at the beginning; The final int is set, but at the beginning, it has not been assigned; so we can configure the runtime parameters by setting the parameters of java:
    java -D java.lang.Integer.IntegerCache.high=1024 TestAutoBoxCache
    insert image description here

It is not necessary for the caller to know the details of Integer creation.
The factory method can hide the details of creating a product, and does not necessarily create a product every time, it can return a cached product, which improves speed and reduces memory consumption.

2.5.2List.of() method

  • List.of() is a method provided by Java9. This static factory method accepts variable parameters and returns the List interface. Compared with the Arrays.asList() method, the modification of the original array has an effect on the List generated by Arrays.asList, but has no effect on the List generated by List.of.
  • The product obtained by the caller is always the List interface and does not care about its actual type. Even if the caller knows that the actual type of the List product is java.util.ImmutableCollections$ListN, do not cast it to a subclass, because the static factory method List.of() is guaranteed to return a List, but it can be modified to return java.util .ArrayList. This is the Liskov Substitution Principle: returning any subclass that implements the interface can satisfy the method's requirements without affecting the caller.
    insert image description here
    insert image description here

2.5.3MessageDigest.getInstance()方法

insert image description here

The same is true for the MessageDigest.getInstance() method, for example, we can MessageDigest.getInstance("MD5");get the MD5 algorithm through.

And in the Effective Java book, Chapter 2, Article 1 is 用静态工厂方法代替构造器.


Completed, in the early morning of the fourth day of the new year in 2022.

Guess you like

Origin blog.csdn.net/MrYushiwen/article/details/122778335
Recommended