Jelajahi Sumber

feat(builder): add Computer.builder() fluent helper and demo/test

檀烈 2 bulan lalu
induk
melakukan
df672d319a

+ 11 - 0
src/main/java/cn/qinys/learn/creational/builder/BuilderDemo.java

@@ -13,6 +13,17 @@ public class BuilderDemo {
 
         System.out.println("Gaming PC: " + gaming);
         System.out.println("Office PC:  " + office);
+
+        // 使用 Computer.builder() 的 fluent/static helper 演示
+        Computer custom = Computer.builder()
+                .cpu("AMD Ryzen 9 7950X")
+                .ram(64)
+                .storage(4000)
+                .gpu("NVIDIA RTX 4090")
+                .wifi(true)
+                .build();
+
+        System.out.println("Custom PC (fluent builder): " + custom);
     }
 }
 

+ 40 - 43
src/main/java/cn/qinys/learn/creational/builder/Computer.java

@@ -1,65 +1,62 @@
 package cn.qinys.learn.creational.builder;
 
+import lombok.Data;
+
+import java.io.Serializable;
+
 /**
  * Product - 需要通过建造者构造的复杂对象
  */
-public class Computer {
+@Data
+public class Computer implements Serializable {
     private String cpu;
     private int ram; // GB
     private int storage; // GB
     private String gpu;
     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 +
-                '}';
-    }
 }
-

+ 26 - 0
src/test/java/cn/qinys/learn/creational/builder/ComputerFluentBuilderTest.java

@@ -0,0 +1,26 @@
+package cn.qinys.learn.creational.builder;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class ComputerFluentBuilderTest {
+
+    @Test
+    public void testFluentBuilderCreatesExpectedComputer() {
+        Computer c = Computer.builder()
+                .cpu("AMD Ryzen 9 7950X")
+                .ram(64)
+                .storage(4000)
+                .gpu("NVIDIA RTX 4090")
+                .wifi(true)
+                .build();
+
+        assertEquals("AMD Ryzen 9 7950X", c.getCpu());
+        assertEquals(64, c.getRam());
+        assertEquals(4000, c.getStorage());
+        assertEquals("NVIDIA RTX 4090", c.getGpu());
+        assertTrue(c.isWifi());
+    }
+}
+