Builder
定義
一步一步從簡單對象建立起復雜對象
何處使用和益處
僅僅明確類型和內容就可以做出一個復雜對象。建成的對象與創建它的細節分開。
將創建一個復雜對象的過程和該對象所包含的組件做接耦化。
將創建和表達的代碼分離。
程序員對創建的過程有更好的控制。
相關模式包括:
抽象工廠模式,它主要是集中在工廠模式的層面,也許簡單,也許復雜。而一個Builder模式主要用來建立基於簡單對象的復雜對象。
復合模式,主要用來建立復雜對象。
舉例
比如建一個房子,我們需要這麼幾步:
1 打基礎
2 建框架
3 建室外
4 建室內
讓我們用一個抽象類HouseBuilder來定義這四步。任何HouseBuilder類的子類將遵循這四步來建這個房子(也就是說,實施子類裡的四個方法)。然後我們用一個WorkShop的類來強制實施這四步的順序(也就是說,我們必須要在前三步都完成的情況下,才能建室內)。TestBuilder類將用來測試這些類之間的協調性以及檢驗建房的過程。
import java.util.*;
class WorkShop {
//force the order of building process
public void construct(HouseBuilder hb) {
hb.buildFoundation();
hb.buildFrame();
hb.buildExterior();
hb.buildInterior();
}
}
//建房的抽象類中定義了建房的4個方法
abstract class HouseBuilder {
protected House house = new House();
protected String showProgress() {
return house.toString();
}
abstract public void buildFoundation();
abstract public void buildFrame();
abstract public void buildExterior();
abstract public void buildInterior();
}
//建一層房子,注意它的4個方法的具體實施
class OneStoryHouse extends HouseBuilder {
public OneStoryHouse(String features) {
house.setType(this.getClass() + " " + features);
}
public void buildFoundation() {
//doEngineering()
//doExcavating()
//doPlumbingHeatingElectricity()
//doSewerWaterHookUp()
//doFoundationInspection()
house.setProgress("foundation is done");
}
public void buildFrame() {
//doHeatingPlumbingRoof()
//doElectricityRoute()
//doDoorsWindows()
//doFrameInspection()
house.setProgress("frame is done");
}
public void buildExterior() {
//doOverheadDoors()
//doBrickWorks()
//doSidingsoffitsGutters()
//doDrivewayGarageFloor()
//doDeckRail()
//doLandScaping()
house.setProgress("Exterior is done");
}
public void buildInterior() {
//doAlarmPrewiring()
//doBuiltinVacuum()
//doInsulation()
//doDryWall()
//doPainting()
//doLinoleum()
//doCabinet()
//doTileWork()
//doLightFixtureBlinds()
//doCleaning()
//doInteriorInspection()
house.setProgress("Interior is under going");
}
}
//建兩層房子,注意它的4個方法的具體實施和建一層房子的不同
class TwoStoryHouse extends HouseBuilder {
public TwoStoryHouse(String features) {
house.setType(this.getClass() + " " + features);
}
public void buildFoundation() {
//doEngineering()
//doExcavating()
//doPlumbingHeatingElectricity()
//doSewerWaterHookUp()
//doFoundationInspection()
house.setProgress("foundation is done");
}
public void buildFrame() {
//doHeatingPlumbingRoof()
//doElectricityRoute()
//doDoorsWindows()
//doFrameInspection()
house.setProgress("frame is under construction");
}
public void buildExterior() {
//doOverheadDoors()
//doBrickWorks()
//doSidingsoffitsGutters()
//doDrivewayGarageFloor()
//doDeckRail()
//doLandScaping()
house.setProgress("Exterior is waiting to start");
}
public void buildInterior() {
//doAlarmPrewiring()
//doBuiltinVacuum()
//doInsulation()
//doDryWall()
//doPainting()
//doLinoleum()
//doCabinet()
//doTileWork()
//doLightFixtureBlinds()
//doCleaning()
//doInteriorInspection()
house.setProgress("Interior is not started yet");
}
}
class House {
private String type = null;
private List features = new ArrayList();
public House() {
}
public House(String type) {
this.type = type;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProgress(String s) {
features.add(s);
}
public String toString() {
StringBuffer ff = new StringBuffer();
String t = type.substring(6);
ff.append(t + "n ");
for (int i = 0; i < features.size(); i ++) {
ff.append(features.get(i) + "n ");
}
return ff.toString();
}
}
class TestBuilder {
public static void main(String[] args) {
HouseBuilder one = new OneStoryHouse("2 bedrooms, 2.5 baths, 2-car garage, 1500 sqft");
HouseBuilder two = new TwoStoryHouse("4 bedrooms, 4 baths, 3-car garage, 5000 sqft");
WorkShop shop = new WorkShop();
shop.construct(one);
shop.construct(two);
System.out.println("Check house building progress: n");
System.out.println(one.showProgress());
System.out.println(two.showProgress());
}
}
//需要用jdk1.5以上來編譯
C:> javac TestBuilder.java
C:> java TestBuilder
//運行結果
Check house building progress:
OneStoryHouse 2 bedrooms, 2.5 baths, 2-car garage, 1500 sqft
foundation is done
frame is done
Exterior is done
Interior is under going
TwoStoryHouse 4 bedrooms, 4 baths, 3-car garage, 5000 sqft
foundation is done
frame is under construction
Exterior is waiting to start
Interior is not started yet
要更進一步優化上面這個例子的話,每一個doXXX的方法都可以設計成一個類。同樣的功能類可以只設計一次,然後被其他類使用,比如,門,窗,廚房等等。
另外,如果要寫一個匹薩餅的程序,每一個成分都可以設計成一個類。一個匹薩餅至少包含了幾種成分。不同的匹薩餅有不同的成分。這樣,我們就可以將Builder的設計模式用上了。