使用场景
当我们构建一个对象的时候,这个对象的字段比较多(比如超过四个以上)
而又存在某些字段是可选的不用额外传入进行构造的场景
这个时候我们有这些方式:
这里还是最推荐使用建造者设计模式
传统的设计方式
假设我们要构造一个电脑对象,其中 CPU 和 RAM 是必选的构造参数(符合实际)
折叠构造器
这种方式写的时候爽,但是维护起来难受,在实例化之前需要明白所有构造器的传参规范,都有哪些,构造器内是做什么,后期维护困难
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public class ConventionalConstructorComputer { private final String cpu;
private final String ram;
private String display;
private String keyboard;
private String usbCount;
public ConventionalConstructorComputer(String cpu, String ram) { this.cpu = cpu; this.ram = ram; }
public ConventionalConstructorComputer(String cpu, String ram, String display) { this.cpu = cpu; this.ram = ram; this.display = display; }
public ConventionalConstructorComputer(String cpu, String ram, String display, String keyboard) { this.cpu = cpu; this.ram = ram; this.display = display; this.keyboard = keyboard; }
public ConventionalConstructorComputer(String cpu, String ram, String display, String keyboard, String usbCount) { this.cpu = cpu; this.ram = ram; this.display = display; this.keyboard = keyboard; this.usbCount = usbCount; } }
|
Java Bean
这种就是最基础的 get set 方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class ConventionalBeanComputer { private String cpu;
private String ram;
private String display;
private String keyboard;
private String usbCount;
public String getCpu() { return cpu; }
public void setCpu(String cpu) { this.cpu = cpu; }
public String getRam() { return ram; }
public void setRam(String ram) { this.ram = ram; }
public String getDisplay() { return display; }
public void setDisplay(String display) { this.display = display; }
public String getKeyboard() { return keyboard; }
public void setKeyboard(String keyboard) { this.keyboard = keyboard; }
public String getUsbCount() { return usbCount; }
public void setUsbCount(String usbCount) { this.usbCount = usbCount; } }
|
虽然比较简洁而且也便于维护
但是可能会导致对象的其他属性在不可见的情况下被修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class ConventionalPattern { public static void main(String[] args) { ConventionalConstructorComputer computer1 = new ConventionalConstructorComputer("Intel", "16g"); ConventionalConstructorComputer computer2 = new ConventionalConstructorComputer("Intel", "16g", "Display"); ConventionalConstructorComputer computer3 = new ConventionalConstructorComputer("Intel", "16g", "Display", "keyboard"); ConventionalConstructorComputer computer4 = new ConventionalConstructorComputer("Intel", "16g", "Display", "keyboard", "usbCount");
ConventionalBeanComputer beanComputer = new ConventionalBeanComputer(); beanComputer.setCpu("Intel"); beanComputer.setRam("16G"); beanComputer.setDisplay("display"); } }
|
建造者模式使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public class Computer {
private final String cpu;
private final String ram;
private final String display;
private final String keyboard;
private final String usbCount;
private Computer(Builder builder) { this.cpu = builder.cpu; this.ram = builder.ram; this.display = builder.display; this.keyboard = builder.keyboard; this.usbCount = builder.usbCount; }
public static class Builder {
private final String cpu;
private final String ram;
private String display;
private String keyboard;
private String usbCount;
public Builder(String cpu, String ram) { this.cpu = cpu; this.ram = ram; }
public Builder buildDisplay(String display) { this.display = display; return this; }
public Builder buildKeyboard(String keyboard) { this.keyboard = keyboard; return this; }
public Builder buildUsbCount(String usbCount) { this.usbCount = usbCount; return this; }
public Computer build(){ return new Computer(this); } }
}
|
程序可以调用 builder 的任何一个方法来组装拼接对象的部分可选属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class BuilderPattern {
public static void main(String[] args) {
Computer intelComputer = new Computer.Builder("INTEL", "16G") .buildDisplay("DISPLAY") .build();
Computer amdComputer = new Computer.Builder("AMD", "32G") .buildKeyboard("Cherry") .buildDisplay("DISPLAY") .buildUsbCount("4") .build(); }
}
|