建造者模式也叫做生成器模式
1.需要建房子:这一过程为打桩、砌墙、封顶
2.房子有各种各样的,比如普通房,高楼、别墅,各种房子的过程虽然一样,但是要求不相同。
3.请编写程序,完成需求。
package com.builder; public abstract class AbstractHouse { //打地基 public abstract void buildBasic(); //砌墙 public abstract void buildWalls(); //封顶 public abstract void roofed(); //依次调用者三个方法 public void build(){ buildBasic(); buildWalls(); roofed(); } }
package com.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 com.builder; public class HighHouse extends AbstractHouse { @Override public void buildBasic() { System.out.println("高楼房子打地基"); } @Override public void buildWalls() { System.out.println("高楼房子砌墙"); } @Override public void roofed() { System.out.println("高楼房子封顶"); } }
package com.builder; public class Client { public static void main(String[] args) { CommonHouse commonHouse = new CommonHouse(); commonHouse.build(); } }
运行结果:
普通房子打地基
普通房子砌墙
普通房子封顶
package com.builder.improve; //产品---->product public class House { private String base; private String wall; private String roofed; public String getBase() { return base; } public void setBase(String base) { this.base = base; } 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.builder.improve; //抽象的建造者 public abstract class HouseBuilder { protected House house=new House(); //将建造的流程写好,抽象的方法 public abstract void buildBase(); public abstract void buildWalls(); public abstract void buildRoofed(); //建造号房子之后,将产品(房子)返回 public House buildHouse(){ return house; } }
package com.builder.improve; /** * 下面的建造顺序我是可以根据自己的需要进行改动的 */ public class CommonHouse extends HouseBuilder { @Override public void buildBase() { System.out.println("普通房子打地基"); } @Override public void buildWalls() { System.out.println("普通房子砌墙"); } @Override public void buildRoofed() { System.out.println("普通房子封底"); } }
package com.builder.improve; public class HighBuilding extends HouseBuilder { @Override public void buildBase() { System.out.println("高楼打地基"); } @Override public void buildWalls() { System.out.println("高楼浇筑混凝土墙体"); } @Override public void buildRoofed() { System.out.println("高楼封底了"); } }
package com.builder.improve; /** * 创建指挥者 指挥具体的建造流程 * 当然,这里的指挥者是可以有多个的,指挥者可以自己制定建造房子的具体流程 * 但是使用之前的那种继承方式就不行了 */ 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.buildBase(); houseBuilder.buildWalls(); houseBuilder.buildRoofed(); return houseBuilder.buildHouse(); } }
package com.builder.improve; public class Client { public static void main(String[] args) { //盖普通房子 CommonHouse commonHouse = new CommonHouse(); HouseDirector houseDirector = new HouseDirector(commonHouse); House house = houseDirector.constructHouse(); } }
运行结果;
普通房子打地基
普通房子砌墙
普通房子封底