【创建模式-构建模式(Builder Pattern)】

这里写目录标题

概览

构建者设计模式是“Gang of Four”的设计模式中的一种,《设计模式》一书旨在阐述如何解决面向对象软件开发中反复出现的设计问题。

《设计模式》解决如下类似问题:

How can a class (the same construction process) create different representations of a complex object?
How can a class that includes creating a complex object be simplified?
Creating and assembling the parts of a complex object directly within a class is inflexible.
It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class.

The Builder design pattern describes how to solve such problems:

Encapsulate creating and assembling the parts of a complex object in a separate Builder object.
A class delegates object creation to a Builder object instead of creating the objects directly.
A class (the same construction process) can delegate to different Builder objects to create different representations of a complex object.

定义

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.[1]

优点

构建设计模式的优点如下:

Allows you to vary a product’s internal representation.
Encapsulates code for construction and representation.
Provides control over steps of construction process.
Disadvantages
Disadvantages of the Builder pattern include:[3]

Requires creating a separate ConcreteBuilder for each different type of product.
Requires the builder classes to be mutable.
Dependency injection may be less supported.

代码示例

package org.cqcs.knowledge.designpattern.creation.builder;

/**
 * The intent of the Builder design pattern is to separate the construction of a complex object from its representation.
 * By doing so, the same construction process can create different representations.
 * <p>
 * 对象表示:由Product类(即Computer类)负责。它只关注对象最终的状态(即属性的值),而不关心这些属性是如何被设置的。
 * <p>
 * Computer的构造函数就是Computer的构建过程,该过程会出现如下过程:
 * <p>
 * 1-当一个构造函数需要接受大量参数时,代码可读性会急剧下降
 * 2-如果很多参数是可选的情况,那么构造函数会很多。
 *
 * @author Samson Bruce
 */
public class Computer {

    /**
     * 中央处理器
     */
    private String cpu;

    /**
     * 内存
     */
    private String ram;

    /**
     * 存储
     */
    private String storage;

    /**
     * 键盘
     */
    private String keyBoard;

    /**
     * 鼠标
     */
    private String mouse;

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

    void setRam(String ram) {
        this.ram = ram;
    }

    void setStorage(String storage) {
        this.storage = storage;
    }

    public void setKeyBoard(String keyBoard) {
        this.keyBoard = keyBoard;
    }

    public void setMouse(String mouse) {
        this.mouse = mouse;
    }

    @Override
    public String toString() {
        return "Computer{" + "CPU='" + cpu + '\'' + ", RAM='" + ram + '\'' + ", storage='" + storage + '\'' + keyBoard + '\'' + mouse + '\'' + '}';
    }

}

如果我们想创建Computer对象, 那么可以使用构建函数但是会有如下问题:

  1. 当一个构造函数需要接受大量参数时,代码可读性会急剧下降
  2. 如果很多参数是可选的情况,那么构造函数会很多。
package org.cqcs.knowledge.designpattern.creation.builder;

/**
 * 构建过程:由Builder接口和ConcreteBuilder实现类负责。它们定义了如何一步步设置对象的属性(如CPU、RAM、存储等)。
 * <p>
 * 1-通过将原本在Computer构造函数中的构造逻辑,转移到Builder中避免构造函数臃肿,同时构建模式通过分步设置参数,让代码更直观。
 * 2-隔离复杂逻辑
 */
public interface Builder {
    void buildCPU();

    void buildRAM();

    void buildStorage();

    void buildKeyBoard();

    void buildMouse();

    Computer build();
}
package org.cqcs.knowledge.designpattern.creation.builder;

public class GamingBuilder implements Builder {
    private final Computer computer;

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

    @Override
    public void buildCPU() {
        computer.setCpu("Intel i9");
    }

    @Override
    public void buildRAM() {
        computer.setRam("32GB DDR4");
    }

    @Override
    public void buildStorage() {
        computer.setStorage("1TB SSD");
    }

    @Override
    public void buildKeyBoard() {
        computer.setKeyBoard("cherry keyBoard");
    }

    @Override
    public void buildMouse() {
        computer.setMouse("Bluetooth mouse");
    }

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

package org.cqcs.knowledge.designpattern.creation.builder;

public class OfficeBuilder implements Builder {
    private final Computer computer;

    public OfficeBuilder() {
        this.computer = new Computer();
    }
    @Override
    public void buildCPU() {

    }

    @Override
    public void buildRAM() {

    }

    @Override
    public void buildStorage() {

    }

    @Override
    public void buildKeyBoard() {

    }

    @Override
    public void buildMouse() {

    }

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

同一对象可以有多个不同的 Builder 实现(例如构建“游戏电脑”和“办公电脑”),通过切换 Builder 即可生成不同配置的对象。

package org.cqcs.knowledge.designpattern.creation.builder;

/**
 * Director解决:
 * 1-希望所有对象的构造都遵循相同的流程(例如必须校验参数后才生成对象)。
 * 2-通过 Director 类统一控制构建步骤,确保逻辑一致性。
 */
public class Director {
    private Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public void build() {
        builder.buildCPU();
        builder.buildRAM();
        builder.buildStorage();
        builder.buildKeyBoard();
        builder.buildMouse();
    }

    public Computer getComputer() {
        return builder.build();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值