Last active
October 17, 2017 03:40
-
-
Save takaki/0eec88b99211fa5d06bf462626174067 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
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