Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Usage
- */
- Test t = Test.create()
- .withMember(42)
- .withString("hello")
- .withString("world")
- .build();
- /**
- * Implementation
- */
- public class Test {
- private final int member;
- private final List<String> strings;
- public static Test.Builder create() {
- return new Test.Builder();
- }
- private Test(Test.Builder builder) {
- this.member = builder.member;
- this.strings = builder.strings;
- }
- /*
- * ...
- * methods/getters
- * ...
- */
- private static class Builder {
- int member;
- List<String> strings;
- public Builder() {
- strings = new ArrayList<>();
- }
- public Builder withMember(int m) {
- this.member = m;
- return this;
- }
- public Builder withString(String s) {
- this.strings.add(s);
- return this;
- }
- public Test build() {
- return new Test(this);
- }
- }
- }
Add Comment
Please, Sign In to add comment