设计模式 - 建造者模式(Builder Pattern)
-
Separate the construction of a complex object from its representation so that the sameconstruction process can create different representations.(將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創建不同的表示。)
建造者模式通用類圖
?
?
image.png
看上面圖可能有點懵,舉一個簡單的例子
假如現在有一個生產電腦代工廠,可能會生產很多電腦品牌的組裝電腦,不同的電腦品牌組裝電腦的順序可能不一樣。
假如現在要為兩個電腦品牌(聯想、華為)組裝電腦
這些部件假如只有顯示器、鍵盤、鼠標、硬盤
需求就是這樣,為不同電腦品牌組裝順序不一樣,但是都提供一個公共的方法assemble()
需求有點復雜我們現在看具體代碼
在ComputerModel中我們定義了一個setSequence方法,組裝點的這幾個動作要如何排布,是在這個ArrayList中定義的。然后assemble()方法根據sequence定義的順序完成指定的順序動作
首先創建ComputerModel
import org.apache.commons.lang3.StringUtils;import java.util.ArrayList; import java.util.List;/*** @author shuliangzhao* @Title: ComputerModel* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:40*/ public abstract class ComputerModel {//sequence控制組裝順序private List<String> sequence = new ArrayList<>();protected abstract void productScreen();protected abstract void productMouse();protected abstract void productKeyboard();protected abstract void productHardDisk();public void assemble() {for (String str:sequence) {if (StringUtils.equals(str,"screen")) {this.productScreen();}else if (StringUtils.equals(str,"mouse")) {this.productMouse();}else if (StringUtils.equals(str,"keyboard")) {this.productKeyboard();}else if (StringUtils.equals(str,"hardDisk")) {this.productHardDisk();}}}public void setSequence(List<String> sequence) {this.sequence = sequence;} }LenovoComputer
/*** @author shuliangzhao* @Title: LenovoComputer* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:49*/public class LenovoComputer extends ComputerModel {@Overrideprotected void productScreen() {System.out.println("聯想電腦組裝屏幕");}@Overrideprotected void productMouse() {System.out.println("聯想電腦組裝鼠標");}@Overrideprotected void productKeyboard() {System.out.println("聯想電腦組裝鍵盤");}@Overrideprotected void productHardDisk() {System.out.println("聯想電腦組裝硬盤");} }HuaweiComputer
/*** @author shuliangzhao* @Title: HuaweiComputer* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:51*/ public class HuaweiComputer extends ComputerModel {@Overrideprotected void productScreen() {System.out.println("華為電腦組裝屏幕");}@Overrideprotected void productMouse() {System.out.println("華為電腦組裝鼠標");}@Overrideprotected void productKeyboard() {System.out.println("華為電腦組裝鍵盤");}@Overrideprotected void productHardDisk() {System.out.println("華為電腦組裝硬盤");} }ComputerBuilder
/*** @author shuliangzhao* @Title: ComputerBuilder* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:51*/ public abstract class ComputerBuilder {//建造一個模型,你要給我一個順序要求,就是組裝順序public abstract void setSequence(List<String> sequence);//設置完畢順序后,就可以直接拿到這個電腦組裝順序public abstract ComputerModel getComputerModel(); }HuaweiBuilder
/*** @author shuliangzhao* @Title: HuaweiBuilder* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:56*/ public class HuaweiBuilder extends ComputerBuilder {private HuaweiComputer huaweiComputer = new HuaweiComputer();@Overridepublic void setSequence(List<String> sequence) {this.huaweiComputer.setSequence(sequence);}@Overridepublic ComputerModel getComputerModel() {return this.huaweiComputer;} }LenovoBuilder
/*** @author shuliangzhao* @Title: LenovoBuilder* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:54*/ public class LenovoBuilder extends ComputerBuilder {private LenovoComputer lenovoComputer = new LenovoComputer();@Overridepublic void setSequence(List<String> sequence) {lenovoComputer.setSequence(sequence);}@Overridepublic ComputerModel getComputerModel() {return this.lenovoComputer;} }場景類Director
/*** 場景類可以根據不同場景組裝電腦* @author shuliangzhao* @Title: Director* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 22:57*/ public class Director {private List<String> sequence = new ArrayList<>();private HuaweiBuilder huaweiBuilder = new HuaweiBuilder();private LenovoBuilder lenovoBuilder = new LenovoBuilder();//組裝華為A系列先屏幕、鼠標、鍵盤、硬盤public HuaweiComputer getAHuaweiComputer() {this.sequence.clear();sequence.add("screen");sequence.add("mouse");sequence.add("keyboard");sequence.add("hardDisk");this.huaweiBuilder.setSequence(sequence);return (HuaweiComputer) this.huaweiBuilder.getComputerModel();}//組裝華為B系列先鼠標、屏幕、硬盤、鍵盤public HuaweiComputer getBHuaweiComputer() {this.sequence.clear();sequence.add("mouse");sequence.add("screen");sequence.add("hardDisk");sequence.add("keyboard");this.huaweiBuilder.setSequence(sequence);return (HuaweiComputer) this.huaweiBuilder.getComputerModel();}//組裝聯想A系列先硬盤、鍵盤、鼠標、屏幕public LenovoComputer getALenovoComputer() {this.sequence.clear();sequence.add("hardDisk");sequence.add("keyboard");sequence.add("mouse");sequence.add("screen");this.lenovoBuilder.setSequence(sequence);return (LenovoComputer) this.lenovoBuilder.getComputerModel();}//組裝聯想B系列先鍵盤、鼠標、硬盤屏幕public LenovoComputer getBLenovoComputer() {this.sequence.clear();sequence.add("keyboard");sequence.add("mouse");sequence.add("hardDisk");sequence.add("screen");this.lenovoBuilder.setSequence(sequence);return (LenovoComputer) this.lenovoBuilder.getComputerModel();} }客戶端
/*** @author shuliangzhao* @Title: Client* @ProjectName design-parent* @Description: TODO* @date 2019/5/29 23:07*/ public class Client {public static void main(String[] args) {Director director = new Director();//組裝華為A系列10臺for (int i = 0;i<2;i++) {director.getAHuaweiComputer().assemble();}//組裝聯想B系列10臺for (int i=0;i<2;i++) {director.getBLenovoComputer().assemble();}} }運行結果
?
image.png
-
建造者模式優點:
1.具有封裝性,使用建造者模式可以使客戶端不必知道產品內部組成的細節。
2.建造者獨立,容易擴展
-
建造者模式使用場景:
1.相同的方法,不同的執行順序,產生不同的事件結果時,可以采用建造者模式
2.產品類非常復雜,或者產品類中的調用順序不同產生了不同的效能,這個時候使用建
造者模式非常合適。
???????
總結
以上是生活随笔為你收集整理的设计模式 - 建造者模式(Builder Pattern)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式-模板方法(Template M
- 下一篇: 设计模式-原型模式(Prototype)