说明
建造者模式(builder pattern)是一个创建型模式。将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
应用场景
- 需要生成的产品对象有复杂的内部结构,这些产品对象具备共性;
- 隔离复杂对象的创建和使用,并使得相同的创建过程可以创建不同的产品。
- 需要生成的对象内部属性本身相互依赖。
- 适合于一个具有较多的零件(属性)的产品(对象)的创建过程。
快速使用方法:
- Builder Generator插件
- lombok插件注解 @Builder(需配合注解@NoArgsConstructor 和 @AllArgsConstructor使用)
代码实现
- 方式一:
1.安装Builder Generator插件
2.在需要生成建造者模式代码的产品类上按下快捷键Alt + Insert,选择Builder
3.生成代码如下:
@Getter
@Setter
public class Computer {
/**
* 名字
*/
private String name;
/**
* CPU
*/
private String cpu;
/**
* 显卡
*/
private String graphicsCard;
/**
* 内存
*/
private String memory;
/**
* 主板
*/
private String mainBoard;
/**
* 显示器
*/
private String screen;
/**
* 键盘
*/
private String keyboard;
/**
* 电源
*/
private String powerSupply;
/**
* 机箱
*/
private String box;
public static final class ComputerBuilder {
private String name;
private String cpu;
private String graphicsCard;
private String memory;
private String mainBoard;
private String screen;
private String keyboard;
private String powerSupply;
private String box;
private ComputerBuilder() {
}
public static ComputerBuilder aComputer() {
return new ComputerBuilder();
}
public ComputerBuilder withName(String name) {
this.name = name;
return this;
}
public ComputerBuilder withCpu(String cpu) {
this.cpu = cpu;
return this;
}
public ComputerBuilder withGraphicsCard(String graphicsCard) {
this.graphicsCard = graphicsCard;
return this;
}
public ComputerBuilder withMemory(String memory) {
this.memory = memory;
return this;
}
public ComputerBuilder withMainBoard(String mainBoard) {
this.mainBoard = mainBoard;
return this;
}
public ComputerBuilder withScreen(String screen) {
this.screen = screen;
return this;
}
public ComputerBuilder withKeyboard(String keyboard) {
this.keyboard = keyboard;
return this;
}
public ComputerBuilder withPowerSupply(String powerSupply) {
this.powerSupply = powerSupply;
return this;
}
public ComputerBuilder withBox(String box) {
this.box = box;
return this;
}
public Computer build() {
Computer computer = new Computer();
computer.memory = this.memory;
computer.screen = this.screen;
computer.graphicsCard = this.graphicsCard;
computer.mainBoard = this.mainBoard;
computer.box = this.box;
computer.powerSupply = this.powerSupply;
computer.cpu = this.cpu;
computer.keyboard = this.keyboard;
computer.name = this.name;
return computer;
}
}
public static void main(String[] args) {
Computer computer = ComputerBuilder.aComputer()
.withName("电脑名")
.withCpu("CPU")
.withGraphicsCard("显卡")
.withKeyboard("键盘")
.withMainBoard("主板")
.withMemory("内存")
.withPowerSupply("电源")
.withScreen("屏幕")
.withBox("机箱")
.build();
System.out.println(JSON.toJSONString(computer));
}
}
- 方式二:
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Computer1 {
/**
* 名字
*/
private String name;
/**
* CPU
*/
private String cpu;
/**
* 显卡
*/
private String graphicsCard;
/**
* 内存
*/
private String memory;
/**
* 主板
*/
private String mainBoard;
/**
* 显示器
*/
private String screen;
/**
* 键盘
*/
private String keyboard;
/**
* 电源
*/
private String powerSupply;
/**
* 机箱
*/
private String box;
public static void main(String[] args) {
Computer1 computer = Computer1.builder()
.name("电脑名")
.cpu("CPU")
.graphicsCard("显卡")
.keyboard("键盘")
.mainBoard("主板")
.memory("内存")
.powerSupply("电源")
.screen("屏幕")
.box("机箱")
.build();
System.out.println(JSON.toJSONString(computer));
}
}
最后
本文介绍的是最简单的一种使用方式,没有涉及到指挥者、抽象建造者等角色。笔者在平时工作的过程中也是这么使用的,对于大型类的构造,避免书写很多setter函数。