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:
parent
560a71d47c
commit
d00188ca6c
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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());
|
||||||
|
|
Loading…
Reference in New Issue