package com.gjxaiou.builder;
public abstract class AbstractHouse {
//打地基
public abstract void buildBasic();
//砌墙
public abstract void buildWalls();
//封顶
public abstract void roofed();
/**
* 在构造方法中调用方法,使其在初始化时候有顺序执行
*/
public void build() {
buildBasic();
buildWalls();
roofed();
}
}
这里以 commonHouse 为例,实现上面的接口,其他实体类类型:
package com.gjxaiou.builder;
public class CommonHouse extends AbstractHouse {
@Override
public void buildBasic() {
// TODO Auto-generated method stub
System.out.println(" 普通房子打地基 ");
}
@Override
public void buildWalls() {
// TODO Auto-generated method stub
System.out.println(" 普通房子砌墙 ");
}
@Override
public void roofed() {
// TODO Auto-generated method stub
System.out.println(" 普通房子封顶 ");
}
}
测试方法:
package com.gjxaiou.builder;
public class Client {
public static void main(String[] args) {
CommonHouse commonHouse = new CommonHouse();
commonHouse.build();
}
}
建造指挥者:
package com.gjxaiou.builder.improve;
//指挥者,这里去指定制作流程,返回产品
public class HouseDirector {
HouseBuilder houseBuilder = null;
// 下面采用了两种方式传入 houseBuilder
//构造器传入 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 com.gjxaiou.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.gjxaiou.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.gjxaiou.builder.improve;
public class HighBuilding extends HouseBuilder {
@Override
public void buildBasic() {
System.out.println(" 高楼的打地基100米 ");
}
@Override
public void buildWalls() {
System.out.println(" 高楼的砌墙20cm ");
}
@Override
public void roofed() {
System.out.println(" 高楼的透明屋顶 ");
}
}
具体的建造产品:
package com.gjxaiou.builder.improve;
//产品->Product
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.gjxaiou.builder.improve;
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("输出流程");
System.out.println("--------------------------");
//盖高楼
HighBuilding highBuilding = new HighBuilding();
//重置建造者
houseDirector.setHouseBuilder(highBuilding);
//完成盖房子,返回产品(高楼)
houseDirector.constructHouse();
}
}
原文:https://www.cnblogs.com/qq438649499/p/12178288.html