构建者模式

定义: 从一个简单对象一步一步构造复杂对象
优点及使用:
  • 仅仅通过指定的类型和内容构建复杂对象,而且隐藏构造的实现细节
  • 解耦通过对象组件构造一个复杂对象的过程
  • 分离构造和显示的代码
  • 通过构造过程达到良好的控制
  • 相关模式有:
  •         抽象工厂:重点是在工厂模式(简单工厂或者复杂工厂),而构建者模式重点是由简单对象构建复杂对象
            复合模式:它经常被用来构建复杂对象


例子:
  为了建设一座房子,我们需要按照以下步骤:
    1.建设基础设施
    2.建设主体框架
    3.装修外部
    4.装修内部
让我们用一个抽象类HouseBuilder来定义这四个步骤。任何HouseBuilder的子类需要根据这四个步骤建设房子(也就是在子类中实现这四个方法),接着我们用WorkShop来规范这四个步骤的顺序(也就是必须完成前三步才能装修内部)。类TestBuilder用来测试这些类的协调性和检查构建过程。
import java.util.*;

class WorkShop{
   //规范构建过程顺序
  public void construnct(HouseBuilder hb){
      hb.buildFoundation();
      hb.buildFrame();
      hb.buildExterior();
      hb.buildInterior();
  }
}

//定义建房子的方法
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();
}

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");
    } 
}

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或者以上版本

}


也可以通过将每个方法设计成类来改良以上例子,相似的方法类可以一次设计处处使用,例如:窗户,门,厨房等等。

你可能感兴趣的:(设计模式,框架)