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
This commit is contained in:
Alexandru Borza 2024-04-13 23:50:11 +03:00 committed by GitHub
parent e975895695
commit 977071357e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 177 additions and 1 deletions

View File

@ -19,10 +19,15 @@
<version>${mockito-inline.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</dependency>
</dependencies>
<properties>
<mockito-inline.version>5.2.0</mockito-inline.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,29 @@
package com.baeldung.builder.implementation;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class GenericBuilder<T> {
private final Supplier<T> supplier;
private GenericBuilder(Supplier<T> supplier) {
this.supplier = supplier;
}
public static <T> GenericBuilder<T> of(Supplier<T> supplier) {
return new GenericBuilder<>(supplier);
}
public <P> GenericBuilder<T> with(BiConsumer<T, P> consumer, P value) {
return new GenericBuilder<>(() -> {
T object = supplier.get();
consumer.accept(object, value);
return object;
});
}
public T build() {
return supplier.get();
}
}

View File

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

View File

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

View File

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