建造者模式(3)-盖房子

2020-12-27  本文已影响0人  Shaw_Young
package com.young.builder.improve;

/**
 * 产品->Product
 *
 * @author Shaw_Young
 * @date 2020/10/6 20:57
 */
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.young.builder.improve;

/**
 * 抽象建造者
 *
 * @author Shaw_Young
 * @date 2020/10/6 20:59
 */
public abstract class HouseBuilder {
    protected House house = new House();

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

    public abstract void buildWall();

    public abstract void roofed();

    //建造房子后,将产品(房子)返回
    public House buildHouse() {
        return house;
    }
}
package com.young.builder.improve;

/**
 * @author Shaw_Young
 * @date 2020/10/6 21:02
 */
public class CommonHouse extends HouseBuilder {
    @Override
    public void buildBasic() {
        System.out.println("普通房子打地基5米");
    }

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

    @Override
    public void roofed() {
        System.out.println("普通房子屋顶");
    }
}
package com.young.builder.improve;

/**
 * @author Shaw_Young
 * @date 2020/10/6 21:04
 */
public class HighBuilding extends HouseBuilder {

    @Override
    public void buildBasic() {
        System.out.println("高楼打地基100米");
    }

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

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

/**
 * 指挥者,这里去指定制作流程
 *
 * @author Shaw_Young
 * @date 2020/10/6 21:05
 */
public class HouseDirector {

    HouseBuilder houseBuilder = null;

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

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

    /**
     * 如何处理建造房子的流程,交给指挥者
     */
    public House constructHouse() {
        houseBuilder.buildBasic();
        houseBuilder.buildWall();
        houseBuilder.roofed();
        return houseBuilder.buildHouse();
    }

}
package com.young.builder.improve;

/**
 * @author Shaw_Young
 * @date 2020/10/6 21:10
 */
public class Client {

    public static void main(String[] args) {
        //盖普通房子
        CommonHouse commonHouse = new CommonHouse();
        //准备创建房子的指挥者
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        //完成盖房子,返回产品(房子)
        House house = houseDirector.constructHouse();
    }

}
上一篇下一篇

猜你喜欢

热点阅读