Skip to content

Instantly share code, notes, and snippets.

@takaki
Last active October 17, 2017 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takaki/0eec88b99211fa5d06bf462626174067 to your computer and use it in GitHub Desktop.
Save takaki/0eec88b99211fa5d06bf462626174067 to your computer and use it in GitHub Desktop.
import java.util.Objects;
public class Hoge {
public static void main(String[] args) {
Hoge hoge = Hoge.builder().address("Nagoya").age(23).build("nanashi-san");
}
private String name;
private int age;
private String address;
private Hoge(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
private static class Builder {
private int age;
private String address;
private Builder() {
}
public Hoge build(String name) {
return new Hoge(Objects.requireNonNull(name), age, address);
}
public Builder address(String address) {
this.address = Objects.requireNonNull(address);
return this;
}
public Builder age(int age) {
if (age < 20) {
throw new IllegalArgumentException("age must be more than 20.");
}
this.age = age;
return this;
}
}
public static Builder builder() {
return new Builder();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment