From fd99c16e89159c1ae5acaff3d3a5577edfacef8b Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 1 Nov 2024 16:52:25 +0100 Subject: [PATCH] added builder example --- src/learn/simple_builder/Builder.java | 16 +++++++++++ src/learn/simple_builder/ConcreteBuilder.java | 18 ++++++++++++ src/learn/simple_builder/Director.java | 18 ++++++++++++ src/learn/simple_builder/Main.java | 12 ++++++++ src/learn/simple_builder/Product.java | 28 +++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 src/learn/simple_builder/Builder.java create mode 100644 src/learn/simple_builder/ConcreteBuilder.java create mode 100644 src/learn/simple_builder/Director.java create mode 100644 src/learn/simple_builder/Main.java create mode 100644 src/learn/simple_builder/Product.java diff --git a/src/learn/simple_builder/Builder.java b/src/learn/simple_builder/Builder.java new file mode 100644 index 0000000..21a5170 --- /dev/null +++ b/src/learn/simple_builder/Builder.java @@ -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(); +} diff --git a/src/learn/simple_builder/ConcreteBuilder.java b/src/learn/simple_builder/ConcreteBuilder.java new file mode 100644 index 0000000..770ee90 --- /dev/null +++ b/src/learn/simple_builder/ConcreteBuilder.java @@ -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"); + } +} diff --git a/src/learn/simple_builder/Director.java b/src/learn/simple_builder/Director.java new file mode 100644 index 0000000..cf8c74b --- /dev/null +++ b/src/learn/simple_builder/Director.java @@ -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(); + } +} diff --git a/src/learn/simple_builder/Main.java b/src/learn/simple_builder/Main.java new file mode 100644 index 0000000..b03667c --- /dev/null +++ b/src/learn/simple_builder/Main.java @@ -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); + } +} diff --git a/src/learn/simple_builder/Product.java b/src/learn/simple_builder/Product.java new file mode 100644 index 0000000..a99c068 --- /dev/null +++ b/src/learn/simple_builder/Product.java @@ -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; + } +}