added builder example

This commit is contained in:
Louis Heredero 2024-11-01 16:52:25 +01:00
parent 8f38c89d7c
commit fd99c16e89
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package learn.simple_builder;
public abstract class Builder {
protected Product product;
public void setProduct(Product product) {
this.product = product;
}
public Product getProduct() {
return product;
}
public abstract void buildPartA();
public abstract void buildPartB();
public abstract void buildPartC();
}

View File

@ -0,0 +1,18 @@
package learn.simple_builder;
public class ConcreteBuilder extends Builder {
@Override
public void buildPartA() {
product.setPartA("material 1");
}
@Override
public void buildPartB() {
product.setPartB("material 2");
}
@Override
public void buildPartC() {
product.setPartC("material 3");
}
}

View File

@ -0,0 +1,18 @@
package learn.simple_builder;
public class Director {
private Builder builder;
private Product product;
public void setBuilder(Builder builder) {
this.builder = builder;
product = new Product();
this.builder.setProduct(product);
}
public void construct() {
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
}
}

View File

@ -0,0 +1,12 @@
package learn.simple_builder;
public class Main {
public static void main(String[] args) {
Builder builder = new ConcreteBuilder();
Director director = new Director();
director.setBuilder(builder);
director.construct();
Product finalProduct = builder.getProduct();
System.out.println(finalProduct);
}
}

View File

@ -0,0 +1,28 @@
package learn.simple_builder;
public class Product {
private String partA;
private String partB;
private String partC;
@Override
public String toString() {
return "Product{" +
"partA='" + partA + '\'' +
", partB='" + partB + '\'' +
", partC='" + partC + '\'' +
'}';
}
public void setPartA(String partA) {
this.partA = partA;
}
public void setPartB(String partB) {
this.partB = partB;
}
public void setPartC(String partC) {
this.partC = partC;
}
}