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
This commit is contained in:
Alexandru Borza 2024-04-16 23:16:09 +03:00 committed by GitHub
parent 560a71d47c
commit d00188ca6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 30 deletions

View File

@ -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;
}
}

View File

@ -2,11 +2,9 @@ package com.baeldung.builder.implementation;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
@Builder @Builder
@Getter @Getter
@Setter
public class LombokPost { public class LombokPost {
private String title; private String title;

View File

@ -2,11 +2,11 @@ package com.baeldung.builder.implementation;
public class Post { 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) { Post(Builder builder) {
this.title = builder.title; this.title = builder.title;
@ -14,8 +14,6 @@ public class Post {
this.category = builder.category; this.category = builder.category;
} }
Post() {}
public String getTitle() { public String getTitle() {
return title; return title;
} }
@ -28,30 +26,11 @@ public class Post {
return category; 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 { public static class Builder {
private String title; private String title;
private String text; private String text;
private String category; private String category;
public Builder() {}
public Builder title(String title) { public Builder title(String title) {
this.title = title; this.title = title;
return this; return this;

View File

@ -23,10 +23,10 @@ public class BuilderImplementationUnitTest {
@Test @Test
void givenGenericBuilder_whenBuild_thenReturnObject() { void givenGenericBuilder_whenBuild_thenReturnObject() {
Post post = GenericBuilder.of(Post::new) GenericPost post = GenericBuilder.of(GenericPost::new)
.with(Post::setTitle, "Java Builder Pattern") .with(GenericPost::setTitle, "Java Builder Pattern")
.with(Post::setText, "Explaining how to implement the Builder Pattern in Java") .with(GenericPost::setText, "Explaining how to implement the Builder Pattern in Java")
.with(Post::setCategory, "Programming") .with(GenericPost::setCategory, "Programming")
.build(); .build();
assertEquals("Java Builder Pattern", post.getTitle()); assertEquals("Java Builder Pattern", post.getTitle());