建造者模式 (Builder) 全面讲解
学习笔记
Java
设计模式
面向对象
Builder模式
软件开发
编程技巧
本文全面讲解了建造者模式,包括其定义、适用场景、结构以及示例代码。通过对比传统构造方式,阐述了建造者模式的优势和在实际开发中的应用。
date: 2025-06-22T19:36:00 tags:
- Java
- 面向对象
- 设计模式
- 复习
建造者模式 (Builder) 全面讲解
1. 什么是建造者模式?
建造者模式是创建型设计模式,用于分步骤构建复杂对象,并允许相同的构建过程创建不同的表示。
- 核心思想:将对象的构造过程与表示分离,避免"重叠构造函数"或过多的
setter调用。 - 类比:组装电脑(选择CPU、内存、硬盘等),最终生成完整电脑。
🔹 适用场景
- 对象需要多个步骤构造(如订单、SQL查询)。
- 对象的构造过程需要不同的配置(如快餐套餐:汉堡+饮料+甜点)。
2. 为什么需要建造者模式?
问题场景
假设有一个Computer类,构造时需要多个参数:
// 传统方式:重叠构造函数或大量setter
Computer computer = new Computer("i7", "16GB", "1TB", "RTX3080", true);
// 问题:参数多、顺序易错、无法灵活选择可选参数!
解决方案
使用建造者模式分步设置参数,最后统一构建对象。
3. 建造者模式的结构
角色说明
Product(产品):最终要构建的复杂对象(如Computer)。Builder(抽象建造者):定义构建步骤的接口。ConcreteBuilder(具体建造者):实现构建步骤,返回最终产品。Director(指挥者):可选,封装构建流程(如固定套餐组合)。
4. 示例代码(组装电脑)
步骤1:定义产品(Computer)
public class Computer {
private String cpu;
private String ram;
private String storage;
private String gpu;
private boolean hasBluetooth;
// 私有构造函数,只能通过Builder创建
private Computer(Builder builder) {
this.cpu = builder.cpu;
this.ram = builder.ram;
this.storage = builder.storage;
this.gpu = builder.gpu;
this.hasBluetooth = builder.hasBluetooth;
}
// 静态内部类Builder
public static class Builder {
private String cpu; // 必选
private String ram; // 必选
private String storage = "512GB"; // 默认值
private String gpu = "Integrated"; // 默认值
private boolean hasBluetooth = false;
// 必选参数通过构造函数传入
public Builder(String cpu, String ram) {
this.cpu = cpu;
this.ram = ram;
}
// 可选参数通过链式方法设置
public Builder storage(String storage) {
this.storage = storage;
return this;
}
public Builder gpu(String gpu) {
this.gpu = gpu;
return this;
}
public Builder hasBluetooth(boolean hasBluetooth) {
this.hasBluetooth = hasBluetooth;
return this;
}
// 最终构建方法
public Computer build() {
return new Computer(this);
}
}
@Override
public String toString() {
return "Computer: " + cpu + ", " + ram + ", " + storage + ", " + gpu + ", Bluetooth=" + hasBluetooth;
}
}
步骤2:客户端调用
public class Client {
public static void main(String[] args) {
// 链式调用,灵活选择参数
Computer gamingPC = new Computer.Builder("i9", "32GB")
.storage("1TB SSD")
.gpu("RTX 4090")
.hasBluetooth(true)
.build();
Computer officePC = new Computer.Builder("i5", "8GB").build();
System.out.println(gamingPC); // Computer: i9, 32GB, 1TB SSD, RTX 4090, Bluetooth=true
System.out.println(officePC); // Computer: i5, 8GB, 512GB, Integrated, Bluetooth=false
}
}
5. 关键点总结
- 优点:
- 参数灵活:可选参数通过方法设置,避免构造函数爆炸。
- 代码可读性高:链式调用清晰表达构建过程。
- 不可变对象:通过
build()最终生成的对象通常是不可变的(线程安全)。
- 缺点:
- 代码量增加(需额外编写
Builder类)。
- 代码量增加(需额外编写
6. 互动实践
问题1:以下代码是建造者模式吗?为什么?
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
答案:不是!这是普通构造函数,建造者模式需要分步构建和链式调用。
问题2:如何用建造者模式实现一个Pizza类?
任务:
Pizza的属性:size(必选)、cheese、pepperoni、mushrooms(可选)。- 补全以下代码:
public class Pizza {
private String size;
private boolean cheese;
// 其他属性...
public static class Builder {
// 你的代码...
}
}
7. 考试练习题
题目1(概念题)
建造者模式的主要作用是?
A. 创建单一对象
B. 分步骤构建复杂对象
C. 实现多态
D. 优化算法
答案:B
题目2(代码填空)
补全建造者模式的代码:
public class Car {
private String engine;
private int wheels;
private boolean gps;
private Car(Builder builder) {
this.engine = builder.engine;
this.wheels = builder.wheels;
this.gps = builder.gps;
}
public static class Builder {
private String engine;
private int wheels;
private boolean gps = false;
public Builder(String engine, int wheels) {
this.engine = engine;
this.wheels = wheels;
}
public Builder gps(boolean gps) {
this.gps = gps;
return ____; // 填空1
}
public Car build() {
return ____; // 填空2
}
}
}
答案:
thisnew Car(this)
题目3(设计题)
用建造者模式设计一个Burger类,包含:
- 必选参数:
size(尺寸) - 可选参数:
cheese、lettuce、tomato
要求:写出完整代码,支持链式调用。