//1.定义产品
//2.抽象构造者,定义抽象方法
//3.具体建造者,生产具体的建造方法
//4.指挥者,负责生产具体的产品
package com.serlyf.builder;
//产品
public class House {
private String basic;
private String wall;
private String roofed;
public void setBasic(String basic) {
this.basic = basic;
}
public void setWall(String wall) {
this.wall = wall;
}
public void setRoofed(String roofed) {
this.roofed = roofed;
}
public String getBasic() {
return basic;
}
public String getWall() {
return wall;
}
public String getRoofed() {
return roofed;
}
}
public abstract class HouseBuilder {
protected House house=new House();
//将建造的流程写好,抽象方法
public abstract void buildBasic();
public abstract void buildWall();
public abstract void buildRoofed();
//建造房子
public House buildHose(){
return house;
}
}
public class CommonHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("普通房子Basic....");
}
@Override
public void buildWall() {
System.out.println("普通房子Wall....");
}
@Override
public void buildRoofed() {
System.out.println("普通房子Roofed....");
}
}
public class HighHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("高层房子Basic....");
}
@Override
public void buildWall() {
System.out.println("高层房子Wall....");
}
@Override
public void buildRoofed() {
System.out.println("高层房子Roofed....");
}
}
//指挥者
public class HouseDirecter {
HouseBuilder houseBuilder=null;
public HouseDirecter(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
public void setHouseBuilder(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
public House createHouse(){
houseBuilder.buildBasic();
houseBuilder.buildWall();
houseBuilder.buildRoofed();
return houseBuilder.buildHose();
}
}
public class BuilderDemo {
public static void main(String[] args) {
///需要创建普通房子
HighHouse highHouse=new HighHouse();
//指挥者
HouseDirecter houseDirecter=new HouseDirecter(highHouse);
//产出房子
House house=houseDirecter.createHouse();
}
}
标签:String,void,建造,System,模式,houseBuilder,Override,设计模式,public From: https://www.cnblogs.com/NIAN2011/p/16631747.html