Design Pattern_Java:Builder Pattern

Design Pattern:Builder Pattern

建造者模式 Builder Pattern

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

將一個複雜對象的構建與它的表示分離,是的一樣的構建過程能夠建立不一樣的表示。

抽象建造者角色(Builder):該角色用於規範產品的各個組成部分,並進行抽象,通常獨立於應用程序的邏輯。

public abstract class Builder {

//設置產品的不一樣部分,以得到不一樣的產品
public abstract void setPart1();
public abstract void setPart2();
public abstract void setPart3();

//建造產品
public abstract Product builderProduct();
}

具體建造者角色(Concrete Builder):該角色實現抽象建造者中定義的全部方法,而且返回一個組建好的產品實例。

public class ConcreteBuilder extends Builder{
private Product product=new Product();

//設置產品零件
@Override
public void setPart1() {
    //爲product安裝部件1
}

@Override
public void setPart2() {
    //爲product安裝部件2
}

@Override
public void setPart3() {
    //爲product安裝部件2
}

//建造一個產品
@Override
public Product builderProduct() {
    return product;
}

}

產品角色(Product):該角色是建造中的複雜對象,一個系統中會有多於一個的產品類,這些產品類並必定有共同的接口,徹底能夠是不想關聯的。

public class Product{
    //產品類的業務處理方法
}

導演者角色(Director):該角色負責安排已有模塊的順序,而後告訴Builder開始建造。

public class Director{
    private Builder builder=new ConcreteBuilder();

    //構造產品,負責調用各個零件建造方法
    public Product build(){
        builder.setPart1();
        builder.setPart2();
        builder.setPart3();

        return builder.builderProduct();
    }
}

建造者模式的實例

Computer.java

package com.DesignPattern.Creational.Builder;

public abstract class Computer {

    private String type;
    private String cpu;
    private String ram;
    private String hardDisk;
    private String monitor;
    private String os;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) {
        this.ram = ram;
    }

    public String getHardDisk() {
        return hardDisk;
    }

    public void setHardDisk(String hardDisk) {
        this.hardDisk = hardDisk;
    }

    public String getMonitor() {
        return monitor;
    }

    public void setMonitor(String monitor) {
        this.monitor = monitor;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

}

T410.java

package com.DesignPattern.Creational.Builder;

public class T410 extends Computer {

    private String graphicCard;

    public T410() {
        this.setType("ThinkPad T410i");
    }

    public String getGraphicCard() {
        return graphicCard;
    }

    public void setGraphicCard(String graphicCard) {
        this.graphicCard = graphicCard;
    }

    @Override
    public String toString() {
        return "T410 [graphicCard=" + graphicCard + ", getType()=" + getType()
                + ", getCpu()=" + getCpu() + ", getRam()=" + getRam()
                + ", getHardDisk()=" + getHardDisk() + ", getMonitor()="
                + getMonitor() + ", getOs()=" + getOs() + "]";
    }

}

X201.java

package com.DesignPattern.Creational.Builder;

public class X201 extends Computer {

    public X201() {
        this.setType("ThinkPad X201i");
    }

    @Override
    public String toString() {
        return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu()
                + ", getRam()=" + getRam() + ", getHardDisk()=" + getHardDisk()
                + ", getMonitor()=" + getMonitor() + ", getOs()=" + getOs()
                + "]";
    }

}

ComputerBuilder.java

package com.DesignPattern.Creational.Builder;

public interface ComputerBuilder {

    void buildCpu();
    void buildRam();
    void buildHardDisk();
    void buildGraphicCard();
    void buildMonitor();
    void buildOS();

    Computer getResult();
}

T410Builder.java

package com.DesignPattern.Creational.Builder;

public class T410Builder implements ComputerBuilder {

    private T410 computer = new T410();

    @Override
    public void buildCpu() {
        computer.setCpu("i5-450");
    }

    @Override
    public void buildRam() {
        computer.setRam("4GB 1333MHz");
    }

    @Override
    public void buildHardDisk() {
        computer.setHardDisk("500G 7200轉");
    }

    @Override
    public void buildGraphicCard() {
        computer.setGraphicCard("Nvidia NVS 3100M");
    }

    @Override
    public void buildMonitor() {
        computer.setMonitor("14英寸 1280*800");
    }

    @Override
    public void buildOS() {
        computer.setOs("Windows 7旗艦版");
    }

    @Override
    public Computer getResult() {
        return computer;
    }

}

X201Builder.java

package com.DesignPattern.Creational.Builder;

public class X201Builder implements ComputerBuilder {

    private X201 computer = new X201();

    @Override
    public void buildCpu() {
        computer.setCpu("i3-350");
    }

    @Override
    public void buildRam() {
        computer.setRam("2GB 1333MHz");
    }

    @Override
    public void buildHardDisk() {
        computer.setHardDisk("250G 5400轉");
    }

    @Override
    public void buildGraphicCard() {
        // 無顯卡
    }

    @Override
    public void buildMonitor() {
        computer.setMonitor("12英寸 1280*800");
    }

    @Override
    public void buildOS() {
        computer.setOs("Windows 7 Home版");
    }

    @Override
    public Computer getResult() {
        return computer;
    }
}

ComputerDirector.java

package com.DesignPattern.Creational.Builder;

public class ComputerDirector {

    ComputerBuilder builder;
    public T410 constructT410(){
        builder=new T410Builder();
        builder.buildCpu();
        builder.buildRam();
        builder.buildHardDisk();
        builder.buildGraphicCard();
        builder.buildMonitor();
        builder.buildOS();
        return (T410)builder.getResult();
    }
    public X201 constructX210(){
        builder=new X201Builder();
        builder.buildCpu();
        builder.buildRam();
        builder.buildHardDisk();
        builder.buildMonitor();
        builder.buildOS();
        return (X201)builder.getResult();
    }
}

ComputerTest.java

package com.DesignPattern.Creational.Builder;

public class ComputerTest {

    public static void main(String[] args){
        ComputerDirector director=new ComputerDirector();
        Computer t410=director.constructT410();
        System.out.println(t410);
        System.out.println("-------------------------------");
        Computer x201=director.constructX210();
        System.out.println(x201);
    }
}
相關文章
相關標籤/搜索