Last active
July 20, 2020 22:10
-
-
Save vojtechruz/3162a7e5525d242e50935e93149208a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PersonBuilder builder = new PersonBuilder(); | |
Person bob = builder.firstName("Bob") | |
.lastName("Builder") | |
.age(33) | |
.description("Man, I love building stuff!") | |
.build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private final String firstName; | |
private final String lastName; | |
private final String description; | |
private final int age; | |
public Person(String firstName, String lastName, String description, int age) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.description = description; | |
this.age = age; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PersonBuilder { | |
private String firstName; | |
private String lastName; | |
private String description; | |
private int age; | |
public PersonBuilder() { | |
} | |
public PersonBuilder firstName(String val) { | |
firstName = val; | |
return this; | |
} | |
public PersonBuilder lastName(String val) { | |
lastName = val; | |
return this; | |
} | |
public PersonBuilder description(String val) { | |
description = val; | |
return this; | |
} | |
public PersonBuilder age(int val) { | |
age = val; | |
return this; | |
} | |
public Person build() { | |
return new Person(firstName, lastName, description, age) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment