Skip to content

Instantly share code, notes, and snippets.

@AdelinGhanaem
Created June 28, 2017 07:33
Show Gist options
  • Select an option

  • Save AdelinGhanaem/961c183be595f3e3708aa2156cf3a105 to your computer and use it in GitHub Desktop.

Select an option

Save AdelinGhanaem/961c183be595f3e3708aa2156cf3a105 to your computer and use it in GitHub Desktop.
public class Client {
public static void main(String[] args){
Person person = newPerson().setAge(10).setSalary(100).setEyesColor("Dark").build();
}
}
private int age;
private long tall;
private String eyesColor;
private String hairColor;
private long weight;
private String status;
private String address;
private List<Person> family;
private long salary;
private String job;
private List<String> hobbies;
private List<String> cars;
//Telescoping Constructors
public Person(int age) {
this.age = age;
}
public Person(int age, long tall) {
this.age = age;
this.tall = tall;
}
public Person(int age, long tall, String eyesColor) {
this.age = age;
this.tall = tall;
this.eyesColor = eyesColor;
}
public Person(int age, long tall, String eyesColor, String hairColor, long weight, String status, String address, List<Person> family, long salary, String job, List<String> hobbies, List<String> cars) {
this.age = age;
this.tall = tall;
this.eyesColor = eyesColor;
this.hairColor = hairColor;
this.weight = weight;
this.status = status;
this.address = address;
this.family = family;
this.salary = salary;
this.job = job;
this.hobbies = hobbies;
this.cars = cars;
}
}
public class PersonBuilder {
private int age;
private long tall;
private String eyesColor;
private String hairColor;
private long weight;
private String status;
private String address;
private List<Person> family;
private long salary;
private String job;
private List<String> hobbies;
private List<String> cars;
public static PersonBuilder newPerson() {
return new PersonBuilder();
}
public Person build() {
return new Person(age, tall, eyesColor, hairColor, weight, status, address, family, salary, job, hobbies, cars);
}
public PersonBuilder setAge(int age) {
this.age = age;
return this;
}
public PersonBuilder setTall(long tall) {
this.tall = tall;
return this;
}
public PersonBuilder setEyesColor(String eyesColor) {
this.eyesColor = eyesColor;
return this;
}
public PersonBuilder setHairColor(String hairColor) {
this.hairColor = hairColor;
return this;
}
public PersonBuilder setWeight(long weight) {
this.weight = weight;
return this;
}
public PersonBuilder setStatus(String status) {
this.status = status;
return this;
}
public PersonBuilder setAddress(String address) {
this.address = address;
return this;
}
public PersonBuilder setFamily(List<Person> family) {
this.family = family;
return this;
}
public PersonBuilder setSalary(long salary) {
this.salary = salary;
return this;
}
public PersonBuilder setJob(String job) {
this.job = job;
return this;
}
public PersonBuilder setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
return this;
}
public PersonBuilder setCars(List<String> cars) {
this.cars = cars;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment