代码实践
房子
package com.atguigu.builder.improve;
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();
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;
}
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);
}
}