From d00188ca6c3bcabd5da0d2dd614ef8b5e991e33b Mon Sep 17 00:00:00 2001 From: Alexandru Borza Date: Tue, 16 Apr 2024 23:16:09 +0300 Subject: [PATCH] BAEL-7615 - review (#16427) * 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 * BAEL-7615 - review --- .../builder/implementation/GenericPost.java | 34 +++++++++++++++++++ .../builder/implementation/LombokPost.java | 2 -- .../baeldung/builder/implementation/Post.java | 27 ++------------- .../BuilderImplementationUnitTest.java | 8 ++--- 4 files changed, 41 insertions(+), 30 deletions(-) create mode 100644 patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericPost.java diff --git a/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericPost.java b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericPost.java new file mode 100644 index 0000000000..b89e5ea320 --- /dev/null +++ b/patterns-modules/design-patterns-creational-2/src/main/java/com/baeldung/builder/implementation/GenericPost.java @@ -0,0 +1,34 @@ +package com.baeldung.builder.implementation; + +public class GenericPost { + + private String title; + + private String text; + + private String category; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } +} 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 index 76edf2390c..f2833c7783 100644 --- 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 @@ -2,11 +2,9 @@ package com.baeldung.builder.implementation; import lombok.Builder; import lombok.Getter; -import lombok.Setter; @Builder @Getter -@Setter public class LombokPost { private String title; 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 index 621618a0e6..4eb94dd44c 100644 --- 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 @@ -2,11 +2,11 @@ package com.baeldung.builder.implementation; public class Post { - private String title; + private final String title; - private String text; + private final String text; - private String category; + private final String category; Post(Builder builder) { this.title = builder.title; @@ -14,8 +14,6 @@ public class Post { this.category = builder.category; } - Post() {} - public String getTitle() { return title; } @@ -28,30 +26,11 @@ public class Post { 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; 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 index e05d4f476a..a827bd0a7f 100644 --- 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 @@ -23,10 +23,10 @@ public class BuilderImplementationUnitTest { @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") + GenericPost post = GenericBuilder.of(GenericPost::new) + .with(GenericPost::setTitle, "Java Builder Pattern") + .with(GenericPost::setText, "Explaining how to implement the Builder Pattern in Java") + .with(GenericPost::setCategory, "Programming") .build(); assertEquals("Java Builder Pattern", post.getTitle());