1 概述
建造者模式(Builder Pattern)主要用於“分步驟構建一個複雜的對象”,在這其中“分步驟”是一個穩定的算法,而復雜對象的各個部分則經常變化。因此, 建造者模式主要用來解決「物件部分」的需求變化。 這樣可以對物件構造的過程進行更精細的控制。
2 例
以生產手機為例,每個手機分為螢幕Screen、CPU、Battery。現在要生產兩種手機,蘋果機和三星。
蘋果:
package org.scott.builder.before.use; import java.util.ArrayList; import java.util.List; /** * @author Scott * @version 2013-11-20 * @description */ public class ApplePhone { List<String> parts = new ArrayList<String>(); public void createCPU() { parts.add("CUP: Qualcomm"); } public void createScreen() { parts.add("SCREEN: JDI"); } public void createBattery() { parts.add("BATTERY: DeSai"); } public void show(){ System.out.print("产品部件信息:"); for(String part : parts){ System.out.print(part + "\t"); } } }
三星:
package org.scott.builder.before.use; import java.util.ArrayList; import java.util.List; /** * @author Scott * @version 2013-11-20 * @description */ public class SamsungPhone { List<String> parts = new ArrayList<String>(); public void createCPU() { parts.add("CUP: MTK"); } public void createScreen() { parts.add("SCREEN: Samsung"); } public void createBattery() { parts.add("BATTERY: DeSai"); } public void show(){ System.out.print("产品部件信息:"); for(String part : parts){ System.out.print(part + "\t"); } } }
測試客戶端:
package org.scott.builder.before.use; /** * @author Scott * @version 2013-11-20 * @description */ public class BuilerTest { private static ApplePhone iphone = new ApplePhone(); private static SamsungPhone samPhone = new SamsungPhone(); public static void main(String args[]){ iphone.createCPU(); iphone.createScreen(); iphone.createBattery(); iphone.show(); samPhone.createCPU(); samPhone.createScreen(); samPhone.createBattery(); samPhone.show(); } }
是不是發現個問題?那就是生產手機的每一道工序都是一樣的,確切的說是工序名稱一樣,只是具體的每個工序的處理不同,工序是不變的,就這麼幾步,每道工序的具體處理是變化的,由此,我們可以把不變的抽取出來,以“不變應萬變”,將變化的,交給具體的產品來做。
具體怎麼做?這回的Builder模式派上用場了。
首先來個Phone的介面:
package org.scott.builder.after.use; import java.util.ArrayList; import java.util.List; /** * @author Scott * @version 2013-11-20 * @description */ public abstract class Phone { protected List<String> parts = new ArrayList<String>(); public void add(String part){ parts.add(part); } public void show(){ System.out.print("产品部件信息:"); for(String part : parts){ System.out.print(part + "\t"); } } }
蘋果手機類:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class ApplePhone extends Phone{ }
三星手機類:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class SamsungPhone extends Phone{ }
再定義個生產步驟的介面Builder:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public interface Builder { public void buildCPU(); public void buildScreen(); public void buildBattery(); public Phone getPhone(); }
蘋果手機的Builder:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class ApplePhoneBuilder implements Builder{ private Phone phone = new ApplePhone(); @Override public void buildCPU() { phone.add("CUP: Qualcomm"); } @Override public void buildScreen() { phone.add("SCREEN: JDI"); } @Override public void buildBattery() { phone.add("BATTERY: DeSai"); } @Override public Phone getPhone() { return phone; } }
三星手機的Builder:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class SamsungPhoneBuilder implements Builder{ private Phone phone = new SamsungPhone(); @Override public void buildCPU() { phone.add("CUP: MTK"); } @Override public void buildScreen() { phone.add("SCREEN: Samsung"); } @Override public void buildBattery() { phone.add("BATTERY: DeSai"); } @Override public Phone getPhone() { return phone; } }
指導具體生產手機的Director:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class Director { private Builder builder; public Director(Builder builder){ this.builder = builder; } public void construct(){ builder.buildCPU(); builder.buildScreen(); builder.buildBattery(); } }
最後寫個測試類別:
package org.scott.builder.after.use; /** * @author Scott * @version 2013-11-20 * @description */ public class BuilderTest { private static Builder iPhoneBuilder = new ApplePhoneBuilder(); private static Builder samPhoneBuilder = new SamsungPhoneBuilder(); public static void main(String[] args) { Director director = new Director(iPhoneBuilder); director.construct(); Phone phone = iPhoneBuilder.getPhone(); System.out.println("iphone"); phone.show(); director = new Director(samPhoneBuilder); director.construct(); phone = samPhoneBuilder.getPhone(); System.out.println("\nsamSung"); phone.show(); } }
運行結果:
iphone 产品部件信息:CUP: Qualcomm SCREEN: JDI BATTERY: DeSai samSung 产品部件信息:CUP: MTK SCREEN: Samsung BATTERY: DeSai
這裡的兩個Phone實體類別是空的,如果是這種情況,那麼它們可以省略掉,如果 Phone介面也可以被省略掉,最終剩下的就只有 Director、Builder、和具體的 Bulider 實作類別。並且,ApplePhone類和SamsungPhone類是有關係的兩個類,它們不同的手機品牌,如果遇到兩個或多個沒有太多關係的類,公共的接口Phone就沒有存在的必要,但是這時候,那麼Builder 介面的規定的getPhone() 方法的回傳值怎麼決定呢?
無論傳回值類型是 ApplePhone還是SamsungPhone,都會產生問題,因為傳回結果的類型不統一。此時,可以將Phone定義成一個空接口(不包含任何方法的接口),再讓這些沒有相互關係的具體產品類都去實現這個接口,那麼Builder 接口裡面規定的getPhone() 方法的返回值類型依然是Phone 類型,就解決問題了。不過這種情況下,也就沒有使用Builder模式的必要了。
更多 java設計模式之建造者模式學習相關文章請關注PHP中文網!