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.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
@Setter
|
||||
public class LombokPost {
|
||||
|
||||
private String title;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue