added builder example
This commit is contained in:
parent
8f38c89d7c
commit
fd99c16e89
16
src/learn/simple_builder/Builder.java
Normal file
16
src/learn/simple_builder/Builder.java
Normal 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();
|
||||||
|
}
|
18
src/learn/simple_builder/ConcreteBuilder.java
Normal file
18
src/learn/simple_builder/ConcreteBuilder.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
18
src/learn/simple_builder/Director.java
Normal file
18
src/learn/simple_builder/Director.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
12
src/learn/simple_builder/Main.java
Normal file
12
src/learn/simple_builder/Main.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
28
src/learn/simple_builder/Product.java
Normal file
28
src/learn/simple_builder/Product.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user