From 977071357e594c1806d2c8f4c11e3b775683622d Mon Sep 17 00:00:00 2001 From: Alexandru Borza Date: Sat, 13 Apr 2024 23:50:11 +0300 Subject: [PATCH] BAEL-7615 - Implement the Builder Pattern in Java 8 (#16399) * BAEL-7255 - Implementing GraphQL Mutation without Returning Data * BAEL-7615 - Implement the Builder Pattern in Java 8 * BAEL-7615 - Implement the Builder Pattern in Java 8 --- .../design-patterns-creational-2/pom.xml | 7 +- .../implementation/GenericBuilder.java | 29 ++++++++ .../builder/implementation/LombokPost.java | 18 +++++ .../baeldung/builder/implementation/Post.java | 74 +++++++++++++++++++ .../BuilderImplementationUnitTest.java | 50 +++++++++++++ 5 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericBuilder.java create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/LombokPost.java create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/Post.java create mode 100644 patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/builder/implementation/BuilderImplementationUnitTest.java diff --git a/patterns-modules/design-patterns-creational-2/pom.xml b/patterns-modules/design-patterns-creational-2/pom.xml index 30e052e6db..bfb8f3ff7c 100644 --- a/patterns-modules/design-patterns-creational-2/pom.xml +++ b/patterns-modules/design-patterns-creational-2/pom.xml @@ -19,10 +19,15 @@ ${mockito-inline.version} test + + org.projectlombok + lombok + 1.18.32 + 5.2.0 - \ No newline at end of file + diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericBuilder.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericBuilder.java new file mode 100644 index 0000000000..6159f92189 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericBuilder.java @@ -0,0 +1,29 @@ +package com.baeldung.builder.implementation; + +import java.util.function.BiConsumer; +import java.util.function.Supplier; + +public class GenericBuilder { + + private final Supplier supplier; + + private GenericBuilder(Supplier supplier) { + this.supplier = supplier; + } + + public static GenericBuilder of(Supplier supplier) { + return new GenericBuilder<>(supplier); + } + + public

GenericBuilder with(BiConsumer consumer, P value) { + return new GenericBuilder<>(() -> { + T object = supplier.get(); + consumer.accept(object, value); + return object; + }); + } + + public T build() { + return supplier.get(); + } +} diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/LombokPost.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/LombokPost.java new file mode 100644 index 0000000000..76edf2390c --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/LombokPost.java @@ -0,0 +1,18 @@ +package com.baeldung.builder.implementation; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Builder +@Getter +@Setter +public class LombokPost { + + private String title; + + private String text; + + private String category; + +} diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/Post.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/Post.java new file mode 100644 index 0000000000..621618a0e6 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/Post.java @@ -0,0 +1,74 @@ +package com.baeldung.builder.implementation; + +public class Post { + + private String title; + + private String text; + + private String category; + + Post(Builder builder) { + this.title = builder.title; + this.text = builder.text; + this.category = builder.category; + } + + Post() {} + + public String getTitle() { + return title; + } + + public String getText() { + return text; + } + + public String getCategory() { + return category; + } + + public void setTitle(String title) { + this.title = title; + } + + public void setText(String text) { + this.text = text; + } + + public void setCategory(String category) { + this.category = category; + } + + @Override + public String toString() { + return "Post{" + "title='" + title + '\'' + ", text='" + text + '\'' + ", category='" + category + '\'' + '}'; + } + + public static class Builder { + private String title; + private String text; + private String category; + + public Builder() {} + + public Builder title(String title) { + this.title = title; + return this; + } + + public Builder text(String text) { + this.text = text; + return this; + } + + public Builder category(String category) { + this.category = category; + return this; + } + + public Post build() { + return new Post(this); + } + } +} diff --git a/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/builder/implementation/BuilderImplementationUnitTest.java b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/builder/implementation/BuilderImplementationUnitTest.java new file mode 100644 index 0000000000..e05d4f476a --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/test/java/com/baeldung/builder/implementation/BuilderImplementationUnitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.builder.implementation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class BuilderImplementationUnitTest { + + @Test + void givenClassicBuilder_whenBuild_thenReturnObject() { + + Post post = new Post.Builder() + .title("Java Builder Pattern") + .text("Explaining how to implement the Builder Pattern in Java") + .category("Programming") + .build(); + + assertEquals("Java Builder Pattern", post.getTitle()); + assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText()); + assertEquals("Programming", post.getCategory()); + } + + @Test + void givenGenericBuilder_whenBuild_thenReturnObject() { + + Post post = GenericBuilder.of(Post::new) + .with(Post::setTitle, "Java Builder Pattern") + .with(Post::setText, "Explaining how to implement the Builder Pattern in Java") + .with(Post::setCategory, "Programming") + .build(); + + assertEquals("Java Builder Pattern", post.getTitle()); + assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText()); + assertEquals("Programming", post.getCategory()); + } + + @Test + void givenLombokBuilder_whenBuild_thenReturnObject() { + + LombokPost post = LombokPost.builder() + .title("Java Builder Pattern") + .text("Explaining how to implement the Builder Pattern in Java") + .category("Programming") + .build(); + + assertEquals("Java Builder Pattern", post.getTitle()); + assertEquals("Explaining how to implement the Builder Pattern in Java", post.getText()); + assertEquals("Programming", post.getCategory()); + } +}