Guest User

Untitled

a guest
Feb 25th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. /**
  2. * Usage
  3. */
  4.  
  5. Test t = Test.create()
  6. .withMember(42)
  7. .withString("hello")
  8. .withString("world")
  9. .build();
  10.  
  11. /**
  12. * Implementation
  13. */
  14. public class Test {
  15.  
  16. private final int member;
  17. private final List<String> strings;
  18.  
  19. public static Test.Builder create() {
  20. return new Test.Builder();
  21. }
  22.  
  23. private Test(Test.Builder builder) {
  24. this.member = builder.member;
  25. this.strings = builder.strings;
  26. }
  27.  
  28.  
  29. /*
  30. * ...
  31. * methods/getters
  32. * ...
  33. */
  34.  
  35. private static class Builder {
  36. int member;
  37. List<String> strings;
  38.  
  39.  
  40. public Builder() {
  41. strings = new ArrayList<>();
  42. }
  43.  
  44. public Builder withMember(int m) {
  45. this.member = m;
  46. return this;
  47. }
  48.  
  49. public Builder withString(String s) {
  50. this.strings.add(s);
  51. return this;
  52. }
  53.  
  54. public Test build() {
  55. return new Test(this);
  56. }
  57. }
  58. }
Add Comment
Please, Sign In to add comment