UML图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2d16dc24135d0b4fc6308ddd83202ef1.png)
建造者模式主要包含四个角色
- Product:代表最终构建的对象,比如,汽车类。
- Builder:代表建造者的抽象基类(可以使用接口来代替)。它定义了构建 Product
的步骤,它的子类(或接口实现类)需要实现这些步骤。同时,它还需要包含一个用来返回最终对象的方法 getProduct()。 - ConcreteBuilder:代表 Builder 类的具体实现类。
- Director:代表需要建造最终对象的某种算法。这里通过使用构造函数 Construct(Builder builder) 来调用
Builder 的创建方法创建对象,等创建完成后,再通过 getProduct() 方法来获取最终的完整对象。
时序图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2fa0bc2b0a3dfab42999840ffb5781cf.png)
原型图对应的实现
public class Product {
private int partA;
private String partB;
private int partC;
public static Product builder(){
return new Product();
}
public int getPartA() {
return partA;
}
public Product partA(int partA) {
this.partA = partA;
return this;
}
public String getPartB() {
return partB;
}
public Product partB(String partB) {
this.partB = partB;
return this;
}
public int getPartC() {
return partC;
}
public Product partC(int partC) {
this.partC = partC;
return this;
}
public Product build(){
return this;
}
}
public interface Builder {
void buildA(int partA);
void buildB(String partB);
void buildC(int partC);
Product getResult();
}
public class ConcreteBuilder implements Builder {
private int partA;
private String partB;
private int partC;
@Override
public void buildA(int partA) {
this.partA = partA;
}
@Override
public void buildB(String partB) {
this.partB = partB;
}
@Override
public void buildC(int partC) {
this.partC = partC;
}
@Override
public Product getResult() {
return Product.builder().partA(partA).partB(partB).partC(partC).build();
}
}
public class Director {
public void consturct(Builder builder){
builder.buildA(1);
builder.buildB("testB");
builder.buildC(222);
}
}
public class Main {
public static void main(String[] args){
Builder builder = new ConcreteBuilder();
Director director = new Director();
director.consturct(builder);
System.out.println(builder.getResult());
}
}
示例1
import lombok.Data;
@Data
public class House {
private String basic;
private String wall;
private String roofed;
}
public interface HouseBuilder {
default House initHouse(){
return new House();
}
void buildBasic();
void buildWall();
void roofed();
House getResult() ;
}
public class CommonHouse implements HouseBuilder {
private final House house;
public CommonHouse() {
this.house = initHouse();
}
@Override
public void buildBasic() {
this.house.setBasic("普通房子打地基");
}
@Override
public void buildWall() {
this.house.setWall("普通房子砌墙");
}
@Override
public void roofed() {
this.house.setRoofed("普通房子封顶");
}
@Override
public House getResult() {
return house;
}
}
public class HighHouse implements HouseBuilder{
private House house;
public HighHouse() {
this.house = initHouse();
}
@Override
public void buildBasic() {
house.setBasic("高楼打地基");
}
@Override
public void buildWall() {
house.setWall("高楼砌墙");
}
@Override
public void roofed() {
house.setRoofed("高楼封顶" );
}
@Override
public House getResult() {
return house;
}
}
public class HouseDirector {
HouseBuilder houseBuilder = null;
public void setHouseBuilder(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
public void constructHouse(){
houseBuilder.buildBasic();
houseBuilder.buildWall();
houseBuilder.roofed();
}
}
public class Client {
public static void main(String[] args) {
HouseDirector director = new HouseDirector();
CommonHouse commonHouse = new CommonHouse();
director.setHouseBuilder(commonHouse);
director.constructHouse();
System.out.println(commonHouse.getResult());
HighHouse highHouse = new HighHouse();
director.setHouseBuilder(highHouse);
director.constructHouse();
System.out.println(highHouse.getResult());
}
}