Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vojtechruz
Last active July 20, 2020 22:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vojtechruz/3162a7e5525d242e50935e93149208a1 to your computer and use it in GitHub Desktop.
Save vojtechruz/3162a7e5525d242e50935e93149208a1 to your computer and use it in GitHub Desktop.
PersonBuilder builder = new PersonBuilder();
Person bob = builder.firstName("Bob")
.lastName("Builder")
.age(33)
.description("Man, I love building stuff!")
.build();
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;
}
}
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