說明
建造者模式(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函數。