代码实践

房子

package com.atguigu.builder.improve;

// 产品 => Product
public class House {
    private String baise;
    private String wall;
    private String roofed;

    public String getBaise() {
        return baise;
    }

    public void setBaise(String baise) {
        this.baise = baise;
    }

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

抽象建造者

package com.atguigu.builder.improve;

// 抽象的建造者
public abstract class HouseBuilder {

    protected House house = new House();

    // 将建造的流程写好,抽象的方法
    public abstract void buildBasic();
    public abstract void buildWalls();
    public abstract void roofed();


    /**
     * 建造房子好,将产品(房子)返回
     * @return
     */
    public House buildHouse() {
        return house;

    }
}

矮楼

package com.atguigu.builder.improve;

/**
 * 抽象
 */
public class CommonHouse extends HouseBuilder{
    @Override
    public void buildBasic() {
        System.out.println("普通的房子地基5米");
    }

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

    @Override
    public void roofed() {
        System.out.println("普通房子屋顶盖上了");
    }
}

高楼

package com.atguigu.builder.improve;


public class HighBuilding extends HouseBuilder{
    @Override
    public void buildBasic() {
        System.out.println("高楼的地基98米");
    }

    @Override
    public void buildWalls() {
        System.out.println("高楼的墙20cm");
    }

    @Override
    public void roofed() {
        System.out.println("高楼的屋顶透明的");
    }
}

HouseDirector

package com.atguigu.builder.improve;


// 指挥者,去指定制定流程,返回产品
public class HouseDirector {
    // 聚合进来
    HouseBuilder houseBuilder = null;


    // 构造器传入
    public HouseDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    // 通过setter方法传入
    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }

    // 如何处理建造房子的流程
    // 交给了指挥者
    public House constructHouse() {
        // 先打地基
        houseBuilder.buildBasic();
        // 在墙
        houseBuilder.buildWalls();
        // 顶
        houseBuilder.roofed();
        // 返回
        return houseBuilder.buildHouse();
    }
}

主方法(客户端)调用

package com.atguigu.builder.improve;


public class Clinet {
    public static void main(String[] args) {
        System.out.println();
        // 盖普通票房子
        CommonHouse commonHouse = new CommonHouse();
        // 准备创建房子的指挥者
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        // 完成盖房子,返回产品(房子
        House house = houseDirector.constructHouse();
        System.out.println(house);
        /* out
        普通的房子地基5米
        普通房子砌墙10cm
        普通房子屋顶盖上了
        com.atguigu.builder.improve.House@1540e19d
        * */
    }
}


results matching ""

    No results matching ""