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模式的必要了。