建造者模式

homle發表於2024-04-06

1. 蓋房子需求

  (1)建造房子需要打樁,砌牆,封頂;

  (2)房子有各種各樣,如普通房子,高樓。

2. 實現方式

  (1)傳統方式

/**
 * @Description :傳統方式
 * @date :2024/4/6 12:11
 */
public abstract class AbstractHouse {

    //打地基
    public abstract void buildBasic();

    //砌牆
    public abstract void buildWalls();

    //封頂
    public abstract void roofed();

    public void build(){
        buildBasic();
        buildWalls();
        roofed();
    }
}
public class CommonHouse extends AbstractHouse {

    @Override
    public void buildBasic() {
        System.out.println("普通房子打地基");
    }

    @Override
    public void buildWalls() {
        System.out.println("普通房子砌牆");
    }

    @Override
    public void roofed() {
        System.out.println("普通房子封頂");
    }
}
public class Client {

    public static void main(String[] args) {

        CommonHouse house = new CommonHouse();
        house.build();
    }
}

  優點:比較好理解,簡單;

  缺點:把產品(房子)和建立產品的過程(建房子的流程)封裝在一起,耦合性增強

  (2)建造者模式

  把複雜物件的建造過程抽象出來,是這個抽象過程的不同實現方法構造出不同的物件

/**
 * @Description :產品
 * @date :2024/4/6 12:17
 */
public class House {

    private String basic;

    private String wall;

    private String roofed;

    public String getBasic() {
        return basic;
    }

    public void setBasic(String basic) {
        this.basic = basic;
    }

    public String getWall() {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getRoofed() {
        return roofed;
    }

    public void setRoofed(String roofed) {
        this.roofed = roofed;
    }
}
/**
 * @Description :產品建立過程
 * @date :2024/4/6 12:22
 */
public abstract class HouseBuilder {

    protected House house = new House();

    //打地基
    public abstract void buildBasic();

    //砌牆
    public abstract void buildWalls();

    //封頂
    public abstract void roofed();

    public House buildHouse(){
        return house;
    }

}
public class CommonHouse extends HouseBuilder {

    @Override
    public void buildBasic() {
        System.out.println("普通房子打地基");
    }

    @Override
    public void buildWalls() {
        System.out.println("普通房子砌牆");
    }

    @Override
    public void roofed() {
        System.out.println("普通房子封頂");
    }
}
public class HighHouse extends HouseBuilder {

    @Override
    public void buildBasic() {
        System.out.println("高樓封頂");
    }

    @Override
    public void buildWalls() {
        System.out.println("高樓砌牆");
    }

    @Override
    public void roofed() {
        System.out.println("高樓屋頂");
    }
}
/**
 * @Description :指揮者決定建造哪種型別的房子
 * @date :2024/4/6 12:25
 */
public class HouseDirector {

    private HouseBuilder houseBuilder;

    public HouseDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    public House constructHouse(){
        houseBuilder.buildBasic();
        houseBuilder.buildWalls();
        houseBuilder.roofed();
        return houseBuilder.buildHouse();
    }
}
public class Client {

    public static void main(String[] args) {
        HouseDirector houseDirector = new HouseDirector(new CommonHouse());
        houseDirector.constructHouse();

        System.out.println("-----------------");

        HighHouse highHouse = new HighHouse();
        houseDirector.setHouseBuilder(highHouse);
        houseDirector.constructHouse();
    }
}

  優點:增加新的具體建造者無需修改原有類的程式碼,指揮者對抽象建造者程式設計,符合開閉原則;客戶端不必知道產品內部組成的細節,將產品本身與產品的建立過程解耦;

相關文章