設計模式之建造者模式

意圖:將一個複雜對象的構建與它的表示分離,使得一樣的構建過程能夠建立不一樣的表示。java

適用性:當建立複雜對象的算法應該獨立於該對象的組成部分以及它們的裝配方式時。 當構造過程必須容許被構造的對象有不一樣的表示時。算法

建造者模式一般包括如下這幾個角色:
    一、Builder:給出一個抽象接口,規範建造者對於生產的產品的各個組成部分的建造。這個接口只是定一個規範,不涉及具體的建造,具體的建造讓繼承於它的子類(ConcreteBuilder)去實現。
    二、ConcreteBuilder:實現builder接口,針對不一樣的商業邏輯,具體化各對象部分的建造,最後返回一個建造好的產品。
    三、Director:導演,顧名思義,負責規範流程之用。在指導中不涉及產品的建立,只負責保證複雜對象各部分被建立或按某種順序建立。
    四、Product:複雜對象。ide

下面以建造一個車爲例子,如圖ui

經過director既可以建一個寶馬,也能建一個奧迪,控制流程在director職中,若是未來又要加一個奔馳,只用新增一個奔馳的builder便可,不用作過多改動就能很好的加進去。流程控制都在director中,舉例複雜的邏輯構建在builder中。示例代碼以下。this

/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id Car.java, v 0.1 2015-10-18 下午4:26 admin Exp $$
 */
public class Car {
    /**
     * 輪子
     */
    private String wheel;

    /**
     * 底盤
     */
    private String chassis;

    /**
     * 車窗
     */
    private String window;

    /*****
     * 其餘不少屬性。。。
     */
    private String other;

    /**
     * Getter method for property <tt>wheel</tt>.
     *
     * @return property value of wheel
     */

    public String getWheel() {
        return wheel;
    }

    /**
     * Setter method for property <tt>wheel</tt>.
     *
     * @param wheel value to be assigned to property wheel
     */
    public void setWheel(String wheel) {
        this.wheel = wheel;
    }

    /**
     * Getter method for property <tt>chassis</tt>.
     *
     * @return property value of chassis
     */

    public String getChassis() {
        return chassis;
    }

    /**
     * Setter method for property <tt>chassis</tt>.
     *
     * @param chassis value to be assigned to property chassis
     */
    public void setChassis(String chassis) {
        this.chassis = chassis;
    }

    /**
     * Getter method for property <tt>window</tt>.
     *
     * @return property value of window
     */

    public String getWindow() {
        return window;
    }

    /**
     * Setter method for property <tt>window</tt>.
     *
     * @param window value to be assigned to property window
     */
    public void setWindow(String window) {
        this.window = window;
    }

    /**
     * Getter method for property <tt>other</tt>.
     *
     * @return property value of other
     */

    public String getOther() {
        return other;
    }

    /**
     * Setter method for property <tt>other</tt>.
     *
     * @param other value to be assigned to property other
     */
    public void setOther(String other) {
        this.other = other;
    }

    @Override
    public String toString() {
        return "Car{" +
                "wheel='" + wheel + '\'' +
                ", chassis='" + chassis + '\'' +
                ", window='" + window + '\'' +
                ", other='" + other + '\'' +
                '}';
    }
}


/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id Builder.java, v 0.1 2015-10-18 下午4:29 admin Exp $$
 */
public interface Builder {
    /**
     * 建造輪子
     */
    public void buildWheel();

    /**
     * 建造底盤
     */
    public void buildChassis();

    /**
     * 建造窗戶
     */
    public void buildWindow();

    /**
     * 建造其餘部件
     */
    public void buildOther();

    /**
     * 返回構建的產品
     * @return
     */
    public Car getCar();

}


/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id AudiBuilder.java, v 0.1 2015-10-18 下午4:33 admin Exp $$
 */
public class AudiBuilder implements Builder {

    /**
     * 要構建的產品
     */
    private Car car;

    public AudiBuilder() {
        this.car = new Car();
    }

    @Override
    public void buildWheel() {
        car.setWheel("Audi wheel!");
    }

    @Override
    public void buildChassis() {
        car.setChassis("Audi chassis!");
    }

    @Override
    public void buildWindow() {
        car.setWindow("Audi window!");
    }

    @Override
    public void buildOther() {
        car.setOther("Audi other!");
    }

    /**
     * Getter method for property <tt>car</tt>.
     *
     * @return property value of car
     */

    public Car getCar() {
        return car;
    }

    /**
     * Setter method for property <tt>car</tt>.
     *
     * @param car value to be assigned to property car
     */
    public void setCar(Car car) {
        this.car = car;
    }
}


/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id BmwBuilder.java, v 0.1 2015-10-18 下午4:34 admin Exp $$
 */
public class BmwBuilder implements Builder {

    /**
     * 要構建的產品
     */
    private Car car;

    public BmwBuilder() {
        this.car = new Car();
    }

    @Override
    public void buildWheel() {
        car.setWheel("Bmw wheel!");
    }

    @Override
    public void buildChassis() {
        car.setChassis("Bmw chassis!");
    }

    @Override
    public void buildWindow() {
        car.setWindow("Bmw window!");
    }

    @Override
    public void buildOther() {
        car.setOther("Bmw other!");
    }

    /**
     * Getter method for property <tt>car</tt>.
     *
     * @return property value of car
     */

    public Car getCar() {
        return car;
    }

    /**
     * Setter method for property <tt>car</tt>.
     *
     * @param car value to be assigned to property car
     */
    public void setCar(Car car) {
        this.car = car;
    }
}


/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id Director.java, v 0.1 2015-10-18 下午4:39 admin Exp $$
 */
public class Director {
    private Builder builder;
    public void constroct(){
        builder.buildWindow();
        builder.buildChassis();
        builder.buildWheel();
        builder.buildOther();
    }
    /**
     * Getter method for property <tt>builder</tt>.
     *
     * @return property value of builder
     */

    public Builder getBuilder() {
        return builder;
    }

    /**
     * Setter method for property <tt>builder</tt>.
     *
     * @param builder value to be assigned to property builder
     */
    public void setBuilder(Builder builder) {
        this.builder = builder;
    }
}


/**
 * Open Free.
 * Code for Open Community, free to use!
 */
package designpattern.builder;

/**
 * @author admin
 * @version $Id Customer.java, v 0.1 2015-10-18 下午4:39 admin Exp $$
 */
public class Customer {
    public static void main(String[] args) {
        //建一個奧迪Audi
        Builder builder = new AudiBuilder();
        //流程控制師
        Director director = new Director();
        //注入構建audi的建造
        director.setBuilder(builder);
        director.constroct();
        Car car = builder.getCar();
        System.out.println(car);


        //建造一個寶馬
        builder = new BmwBuilder();
        //注入構建bmw的建造
        director.setBuilder(builder);
        director.constroct();
        car = builder.getCar();
        System.out.println(car);
    }
}
相關文章
相關標籤/搜索