|
@@ -1,65 +1,62 @@
|
|
|
package cn.qinys.learn.creational.builder;
|
|
package cn.qinys.learn.creational.builder;
|
|
|
|
|
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.Serializable;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Product - 需要通过建造者构造的复杂对象
|
|
* Product - 需要通过建造者构造的复杂对象
|
|
|
*/
|
|
*/
|
|
|
-public class Computer {
|
|
|
|
|
|
|
+@Data
|
|
|
|
|
+public class Computer implements Serializable {
|
|
|
private String cpu;
|
|
private String cpu;
|
|
|
private int ram; // GB
|
|
private int ram; // GB
|
|
|
private int storage; // GB
|
|
private int storage; // GB
|
|
|
private String gpu;
|
|
private String gpu;
|
|
|
private boolean wifi;
|
|
private boolean wifi;
|
|
|
|
|
|
|
|
- // getters and setters
|
|
|
|
|
- public String getCpu() {
|
|
|
|
|
- return cpu;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void setCpu(String cpu) {
|
|
|
|
|
- this.cpu = cpu;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Fluent static builder helper: Computer.builder()
|
|
|
|
|
+ */
|
|
|
|
|
+ public static FluentBuilder builder() {
|
|
|
|
|
+ return new FluentBuilder();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public int getRam() {
|
|
|
|
|
- return ram;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public void setRam(int ram) {
|
|
|
|
|
- this.ram = ram;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Nested static fluent builder. Named FluentBuilder to avoid clashing with package-level Builder interface.
|
|
|
|
|
+ */
|
|
|
|
|
+ public static class FluentBuilder {
|
|
|
|
|
+ private final Computer computer = new Computer();
|
|
|
|
|
|
|
|
- public int getStorage() {
|
|
|
|
|
- return storage;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public FluentBuilder cpu(String cpu) {
|
|
|
|
|
+ computer.setCpu(cpu);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public void setStorage(int storage) {
|
|
|
|
|
- this.storage = storage;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public FluentBuilder ram(int gb) {
|
|
|
|
|
+ computer.setRam(gb);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public String getGpu() {
|
|
|
|
|
- return gpu;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public FluentBuilder storage(int gb) {
|
|
|
|
|
+ computer.setStorage(gb);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public void setGpu(String gpu) {
|
|
|
|
|
- this.gpu = gpu;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public FluentBuilder gpu(String gpu) {
|
|
|
|
|
+ computer.setGpu(gpu);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public boolean isWifi() {
|
|
|
|
|
- return wifi;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ public FluentBuilder wifi(boolean wifi) {
|
|
|
|
|
+ computer.setWifi(wifi);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- public void setWifi(boolean wifi) {
|
|
|
|
|
- this.wifi = wifi;
|
|
|
|
|
|
|
+ public Computer build() {
|
|
|
|
|
+ // return the constructed instance
|
|
|
|
|
+ return computer;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
|
- public String toString() {
|
|
|
|
|
- return "Computer{" +
|
|
|
|
|
- "cpu='" + cpu + '\'' +
|
|
|
|
|
- ", ram=" + ram +
|
|
|
|
|
- ", storage=" + storage +
|
|
|
|
|
- ", gpu='" + gpu + '\'' +
|
|
|
|
|
- ", wifi=" + wifi +
|
|
|
|
|
- '}';
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|