建造者模式(Builder Pattern),也叫生成器模式,屬於建立型模式。它使用多個簡單的物件一步一步構建成一個複雜的物件。它允許你使用相同的建立程式碼生成不同型別和形式的物件。
當你希望使用程式碼建立不同形式的產品 (例如各種商品和訂單) 時, 一些基本部件不會變,而其組合經常變化的時候,就可以考慮建造者模式。
// Builder.java 建造者介面,定義基本建造方法 public interface Builder { public void reset(); public void setName(String name); public void setScreen(Integer[] screen); public void setGPU(Integer no); }
// ManualBuilder.java 使用手冊建造者類也實現了建造者介面 public class ManualBuilder implements Builder { private Manual manual; public void reset() { this.manual = new Manual(); } public void setName(String name) { this.manual.setName(name); } public void setScreen(Integer[] screen) { this.manual.setScreen(screen); } public void setGPU(Integer no) { this.manual.setGpuType(no); } public Manual getProduct() { return this.manual; } } // PhoneBuilder.java 手機建造者實現了建造者介面 public class PhoneBuilder implements Builder { private Phone phone; public void reset() { this.phone = new Phone(); } public void setName(String name) { this.phone.setName(name); } public void setScreen(Integer[] screen) { this.phone.setScreen(screen); } public void setGPU(Integer no) { this.phone.setGpuType(no); } public Phone getProduct() { return this.phone; } }
// Manual.java 手冊產品類 public class Manual { private String name = "PhoneManualName"; private Integer[] screen = { 0, 0 }; private Integer gpuType = 0; private Integer pages = 0; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setScreen(Integer[] screen) { this.screen = screen; } public Integer[] getScreen() { return this.screen; } ...... } // Phone.java 手機產品類 public class Phone { private String name = "PhoneName"; private Integer[] screen = { 0, 0 }; private Integer gpuType = 0; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void setScreen(Integer[] screen) { this.screen = screen; } public Integer[] getScreen() { return this.screen; } ...... }
// Director.java 指揮排程類,負責利用建造者建造產品,隔離需求與功能 public class Director { // 建造phone1 public void buildIPhone(Builder builder) { builder.reset(); Integer[] screen = { 120, 500 }; builder.setName("iPhone"); builder.setScreen(screen); builder.setGPU(100); } // 建造phone2 public void buildHuaweiPhone(Builder builder) { builder.reset(); Integer[] screen = { 130, 600 }; builder.setName("HuaweiPhone"); builder.setScreen(screen); builder.setGPU(102); } // 建造phone3 public void buildMiPhone(Builder builder) { builder.reset(); Integer[] screen = { 120, 650 }; builder.setName("MiPhone"); builder.setScreen(screen); builder.setGPU(103); } }
/** * 建造者模式是使用多個簡單的物件一步一步構建出一個複雜的物件來。 * 分為主管類和建造這類,主管類負責具體指揮排程,建造負責具體實施。 * 主管類通過一步一步呼叫各種建造者實現複雜物件。 */ // 宣告指揮者 Director director = new Director(); // 建立手機 PhoneBuilder phoneBuilder = new PhoneBuilder(); director.buildMiPhone(phoneBuilder); Phone miPhone = phoneBuilder.getProduct(); System.out.println("miPhone:" + miPhone.getName() + " | " + miPhone.getGpuType().toString()); // 建立手冊 ManualBuilder manualBuilder = new ManualBuilder(); director.buildMiPhone(manualBuilder); Manual manual = manualBuilder.getProduct(); System.out.println("manual:" + manual.getName() + " | " + manual.getGpuType().toString());
不同語言實現設計模式:https://github.com/microwind/design-pattern