【JAVA】Builder Mode

【JAVA】Builder Mode

The Builder pattern is a creational design pattern that separates the construction process of an object from its representation. It can build complex objects step by step, so that the same building process can create different representations.

In Java, the builder pattern usually consists of the following components:

  1. Product: Represents the complex object being constructed. This class usually contains several properties and methods.

  2. Abstract builder: defines the abstract interface for building products. Typically includes methods for setting product properties and methods for obtaining build results.

  3. Concrete builder: implements the abstract builder interface and concretely implements each step of building a product. Usually also includes a method to obtain the final build result.

  4. Instructor: Responsible for calling the builder's methods to build the product, but does not know the specific construction details.

Here's an example showing how to use the builder pattern to build a simple computer object:

// 产品 - 电脑
class Computer {
    
    
    private String cpu;
    private String memory;
    private String hardDrive;

    public void setCpu(String cpu) {
    
    
        this.cpu = cpu;
    }

    public void setMemory(String memory) {
    
    
        this.memory = memory;
    }

    public void setHardDrive(String hardDrive) {
    
    
        this.hardDrive = hardDrive;
    }

    // 省略其他属性和方法
}

// 抽象建造者
interface ComputerBuilder {
    
    
    void setCPU(String cpu);
    void setMemory(String memory);
    void setHardDrive(String hardDrive);
    Computer build();
}

// 具体建造者
class StandardComputerBuilder implements ComputerBuilder {
    
    
    private Computer computer;

    public StandardComputerBuilder() {
    
    
        this.computer = new Computer();
    }

    @Override
    public void setCPU(String cpu) {
    
    
        computer.setCpu(cpu);
    }

    @Override
    public void setMemory(String memory) {
    
    
        computer.setMemory(memory);
    }

    @Override
    public void setHardDrive(String hardDrive) {
    
    
        computer.setHardDrive(hardDrive);
    }

    @Override
    public Computer build() {
    
    
        return computer;
    }
}

// 指导者
class Director {
    
    
    public Computer constructStandardComputer() {
    
    
        ComputerBuilder builder = new StandardComputerBuilder();
        builder.setCPU("Intel Core i5");
        builder.setMemory("8GB");
        builder.setHardDrive("1TB");
        return builder.build();
    }
}

// 示例用法
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Director director = new Director();
        Computer computer = director.constructStandardComputer();
        // 使用构建好的电脑对象进行后续操作
    }
}

In the above example, we defined the interface for building the product through an abstract builder. The concrete builder then implements the interface and implements the steps to build the concrete product. The mentor is responsible for invoking the concrete builder's methods to build the product.

The advantage of using the builder pattern is that it can separate the construction process and presentation of complex objects, making the construction process more flexible. At the same time, the builder pattern can also avoid the generation of immutable objects during the construction process, decoupling the creation and presentation of objects.

Summarize

The builder pattern has many practical applications in Java. Here are some common usage scenarios, with each scenario explained in detail:

  1. Creating complex objects: The builder pattern is suitable for creating complex objects with multiple properties or parameters. For example, create an e-commerce order object that contains many configuration options, including product, quantity, price, shipping address, payment method and other information. The builder pattern allows you to build an order object step by step and set its individual properties to the desired values.

  2. The order of object construction is not fixed: The builder pattern is useful when the order of object construction may change or when some properties are optional. For example, construct an airplane object in which some properties (e.g., wing type, engine type) are optional, while others (e.g., number of seats, maximum speed) are required. By using the Builder pattern, specific properties can be set or omitted as needed without affecting the object's construction process.

  3. Creating objects with different representations: The builder pattern allows creation of different representations within the same build process. For example, in game development, you can use the builder pattern to create different types of character objects, such as warriors, mages, shooters, etc. Each specific builder can set corresponding properties and behaviors according to different role types, thereby creating different role objects.

  4. Isolate the construction and use of complex objects: The builder pattern can separate the construction process of objects from the use of objects to improve the maintainability and flexibility of the code. The client only needs to focus on the use of the final product and does not need to know the details of its creation. This isolation reduces code coupling and makes the system easier to extend and modify.

  5. Avoid overlapping constructors: If a class has multiple properties that may be combined in multiple ways, using constructors may lead to many overlapping constructor problems. By using the builder pattern, this problem can be avoided, making the code clearer and easier to maintain. The builder pattern provides a flexible way to set the properties of an object without using a large number of constructors.

In summary, the builder pattern is suitable for creating complex objects, flexibly controlling the object construction process, creating objects with different representations, isolating the construction and usage processes, and avoiding overlapping constructors. It is a powerful design pattern that is widely used in various scenarios in Java, making the code more flexible, scalable and easy to understand.

Guess you like

Origin blog.csdn.net/qq_39017153/article/details/132499039