赞
踩
package builder.entity; import java.util.ArrayList; import java.util.List; /** * 数据模型-建筑物类 **/ public class Building { /** * 用list来模拟建筑物组件的组装 * 以List承载各组件,模拟复杂对象中各组件的顺序组装 */ private List<String> buildingComponents = new ArrayList<>(); // 地基 public void setBasement(String basement) { this.buildingComponents.add(basement); } // 墙体 public void setWall(String wall) { this.buildingComponents.add(wall); } // 屋顶 public void setRoof(String roof) { this.buildingComponents.add(roof); } /** * 重写了toString() * 按从大到小的组件索引【顺序】组装各组件,后组装的组件应先展示出来,如屋顶应该首先输出 * @return */ @Override public String toString() { String buildingStr = ""; for (int i = buildingComponents.size() -1; i >= 0; i--) { buildingStr += buildingComponents.get(i); } return buildingStr; } }
package builder; import builder.entity.Building; /** * 施工方接口: 规定了3个施工标准, 具备三大组件的建造能力 * 建造地基 * 建造墙体 * 建造屋顶 * 获取建筑物,以供产品的交付 **/ public interface Builder { /** * 建造地基 */ public void buildBasement(); /** * 建造墙体 */ public void buildWall(); /** * 建造屋顶 */ public void buildRoof(); /** * 获取建筑物,以供产品的交付 * @return */ public Building getBuilding(); }
package builder.impl; import builder.Builder; import entity.Building; /** * 别墅施工方类 **/ public class HouseBuilder implements Builder { private Building house; public HouseBuilder() { house = new Building(); } @Override public void buildBasement() { System.out.println("挖土方,部署管道、线缆,水泥加固,搭建围墙、花园。"); house.setBasement("╬╬╬╬╬╬╬╬\n"); } @Override public void buildWall() { System.out.println("搭建木质框架,石膏板封墙并粉饰内外墙。"); house.setWall("|田|田 田|\n"); } @Override public void buildRoof() { System.out.println("建造木质屋顶、阁楼,安装烟囱,做好防水。"); house.setRoof("╱◥███◣\n"); } @Override public Building getBuilding() { return house; } }
package builder.impl; import builder.Builder; import builder.entity.Building; /** * 多层公寓施工方类 **/ public class ApartmentBuilder implements Builder { private Building apartment; public ApartmentBuilder() { apartment = new Building(); } @Override public void buildBasement() { System.out.println("深挖地基,修建地下车库,部署管道、线缆、风道。"); apartment.setBasement("╚═══════════╝\n"); } @Override public void buildWall() { System.out.println("搭建多层建筑框架,建造电梯井,钢筋混凝土浇灌。"); // 此处假设固定8层 for (int i = 0; i < 8; i++) { apartment.setWall("║ □ □ □ □ ║\n"); } } @Override public void buildRoof() { System.out.println("封顶,部署通风井,做防水层,保温层。"); apartment.setRoof("╔═══════════╗\n"); } @Override public Building getBuilding() { return apartment; } }
package builder; import builder.entity.Building; /** * 工程总监类: 指导施工,并把控整个施工流程 **/ public class Director { private Builder builder; public Director(Builder builder) { this.builder = builder; } public void setBuilder(Builder builder) { this.builder = builder; } public Building direct() { System.out.println("=====工程项目启动====="); // 第一步,打好地基 builder.buildBasement(); // 第二步,建造框架、墙体 builder.buildWall(); // 第三步,封顶 builder.buildRoof(); System.out.println("=====工程项目竣工====="); return builder.getBuilding(); } }
package builder; import builder.impl.ApartmentBuilder; import builder.impl.HouseBuilder; /** * 开发商客户端类: 组建了别墅施工队并安排=给工程总监进行管理 **/ public class Client { public static void main(String[] args) { //组建别墅施工队 Director director = new Director(new HouseBuilder()); System.out.println(director.direct()); //替换施工队,建造公寓 director.setBuilder(new ApartmentBuilder()); System.out.println(director.direct()); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。