设计模式4-建造者模式

定义

Builder Partern:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。(针对于构建对象的参数众多而提出的解耦合,创造复杂对象)

目的

● 使参数更加可视化和可以明确各个参数的定义

● 定义规范和约束

场景

● 当构建一个类的参数众多避免参数混乱时,可以使用建造者模式

实际使用

java.lang.StringBuilderjava.lang.StringBufferappend() 方法返回自身,类似于建造者的链式调用。

java.util.stream.Stream.Builder

代码

java 复制代码
// 产品类 - 计算机
public class Computer {
    // 必需参数
    private String cpu;
    private String ram;

    // 可选参数
    private String storage;
    private String graphicsCard;
    private boolean bluetoothEnabled;

    // 私有构造函数,只能通过Builder构建
    private Computer(ComputerBuilder builder) {
        this.cpu = builder.cpu;
        this.ram = builder.ram;
        this.storage = builder.storage;
        this.graphicsCard = builder.graphicsCard;
        this.bluetoothEnabled = builder.bluetoothEnabled;
    }

    // 省略 getter 方法...

    // 静态内部类 - 建造者
    public static class ComputerBuilder {
        // 必需参数
        private String cpu;
        private String ram;

        // 可选参数 - 初始化默认值
        private String storage = "500GB HDD";
        private String graphicsCard = "Integrated";
        private boolean bluetoothEnabled = false;

        // 建造者的构造函数,接收必需参数
        public ComputerBuilder(String cpu, String ram) {
            this.cpu = cpu;
            this.ram = ram;
        }

        // 用于设置可选参数的方法,并返回建造者本身以实现链式调用
        public ComputerBuilder setStorage(String storage) {
            this.storage = storage;
            return this;
        }

        public ComputerBuilder setGraphicsCard(String graphicsCard) {
            this.graphicsCard = graphicsCard;
            return this;
        }

        public ComputerBuilder setBluetoothEnabled(boolean bluetoothEnabled) {
            this.bluetoothEnabled = bluetoothEnabled;
            return this;
        }

        // 最终的构建方法,返回构建好的产品
        public Computer build() {
            // 可以在这里进行校验
            if (cpu == null || ram == null) {
                throw new IllegalArgumentException("CPU and RAM are required");
            }
            return new Computer(this);
        }
    }

    @Override
    public String toString() {
        return "Computer [cpu=" + cpu + ", ram=" + ram + ", storage=" + storage + ", graphicsCard=" + graphicsCard
                + ", bluetoothEnabled=" + bluetoothEnabled + "]";
    }
}

class Client {
    public static void main(String[] args) {
        // 使用建造者一步步构建产品,链式调用,清晰易懂
        Computer gamingComputer = new Computer.ComputerBuilder("Intel i9", "32GB")
                .setStorage("1TB NVMe SSD")
                .setGraphicsCard("NVIDIA RTX 4080")
                .setBluetoothEnabled(true)
                .build(); // 最终调用build方法构建对象

        System.out.println(gamingComputer);

        // 构建一个基础配置的电脑,只设置必需参数,使用可选参数的默认值
        Computer officeComputer = new Computer.ComputerBuilder("Intel i5", "16GB")
                .build();

        System.out.println(officeComputer);
    }
}

建造者模式代码

相关推荐
七月丶4 小时前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞4 小时前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼4 小时前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟21 小时前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder21 小时前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦2 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo5 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4965 天前
js设计模式 --- 工厂模式
设计模式
逆境不可逃5 天前
【从零入门23种设计模式08】结构型之组合模式(含电商业务场景)
线性代数·算法·设计模式·职场和发展·矩阵·组合模式