夜光序言:
台上的人演绎的是台下人的寂寞悲喜,而台下的人看到的是台上人的云散萍聚。
正文:
以道御术 / 以术识道
package 建造者模式.builder;
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("普通房子封顶");
}
}
package 建造者模式.builder;
/*
*
* @title AbstractHouse
* @describe 夜光:首先是一个抽象类
* @author hy
* @version
* @date 2019/12/2
*
*/
public abstract class AbstractHouse {
//打地基
public abstract void buildBasic(); //抽象方法
//砌墙
public abstract void buildWalls();
//封顶
public abstract void roofed();
//上面这三个方法其实是有顺序的
//我们依次调用
public void build(){
//依次调用
buildBasic();
buildWalls();
roofed();
}
}
package 建造者模式.improve1;
//写一个类,继承HouseBuilder,并且实现三个方法
public class CommonHouse extends HouseBuilder
{
@Override
public void buildBasic() {
System.out.println("普通房子打地基11m");
}
@Override
public void buildWalls() {
System.out.println("普通房子砌墙10cm");
}
@Override
public void roofed() {
System.out.println("普通房子屋顶");
}
}
package 建造者模式.improve1;
//指挥者,这里去指定制作流程,返回产品
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.buildWalls();
houseBuilder.roofed();
return houseBuilder.buildHouse();
}
}
package 建造者模式.improve1;
public class Client {
public static void main(String[] args) {
//盖普通房子
CommonHouse commonHouse = new CommonHouse();
//准备创建房子的指挥者
HouseDirector houseDirector = new HouseDirector(commonHouse);
//完成盖房子,返回产品(房子)
House house = houseDirector.constructHouse();
}
}
package 建造者模式.improve1;
public class Client {
public static void main(String[] args) {
//盖普通房子
CommonHouse commonHouse = new CommonHouse();
//准备创建房子的指挥者
HouseDirector houseDirector = new HouseDirector(commonHouse);
//完成盖房子,返回产品(房子)
House house = houseDirector.constructHouse();
System.out.println("------------------------------------------------");
//盖高楼
HighBuilding highBuilding = new HighBuilding();
//重置建造者
houseDirector.setHouseBuilder(highBuilding);
//完成盖房子,返回产品(高楼)
houseDirector.constructHouse();
}
}