code_junkie

How many constructor arguments is too many?

Nov 10th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. public class CustomerBuilder {
  2. private String surname;
  3. private String firstName;
  4. private String ssn;
  5. public static CustomerBuilder customer() {
  6. return new CustomerBuilder();
  7. }
  8. public CustomerBuilder withSurname(String surname) {
  9. this.surname = surname;
  10. return this;
  11. }
  12. public CustomerBuilder withLastName(String lastName) {
  13. this.lastName = lastName;
  14. return this;
  15. }
  16. public CustomerBuilder withSSN(String ssn) {
  17. this.ssn = ssn;
  18. return this;
  19. }
  20.  
  21. // client doesn't get to instantiate Customer directly
  22. public Customer build() {
  23. if (surname == null || firstName == null || ssn == null) {
  24. throw new NullPointerException("Incomplete customer definition");
  25. }
  26. /* option 1: would have package private setters */
  27. Customer target = new Customer();
  28. target.setSurname(this.surname);
  29. target.setFirstName(this.firstName);
  30. target.setSSN(this.ssn);
  31. return target;
  32.  
  33. /* option 2:
  34.  
  35. I actually like the approach marcio suggests, where we pass the
  36. builder in the constructor - prevents the need for all of the
  37. setters
  38.  
  39. Customer target = new Customer(this);
  40. return target;
  41. */
  42. }
  43. }
  44.  
  45.  
  46. import static com.acme.CustomerBuilder.customer;
  47.  
  48. public class Client {
  49. public void doSomething() {
  50. Customer customer = customer()
  51. .withSurname("Smith")
  52. .withFirstName("Fred")
  53. .withSSN("123XS1")
  54. .build();
  55. }
  56. }
  57.  
  58. public class NutritionFacts {
  59. private final int servingSize;
  60. private final int servings;
  61. private final int calories;
  62. private final int fat;
  63. private final int sodium;
  64. private final int carbohydrate;
  65.  
  66. public static class Builder {
  67. // required parameters
  68. private final int servingSize;
  69. private final int servings;
  70.  
  71. // optional parameters
  72. private int calories = 0;
  73. private int fat = 0;
  74. private int carbohydrate = 0;
  75. private int sodium = 0;
  76.  
  77. public Builder(int servingSize, int servings) {
  78. this.servingSize = servingSize;
  79. this.servings = servings;
  80. }
  81.  
  82. public Builder calories(int val)
  83. { calories = val; return this; }
  84. public Builder fat(int val)
  85. { fat = val; return this; }
  86. public Builder carbohydrate(int val)
  87. { carbohydrate = val; return this; }
  88. public Builder sodium(int val)
  89. { sodium = val; return this; }
  90.  
  91. public NutritionFacts build() {
  92. return new NutritionFacts(this);
  93. }
  94. }
  95.  
  96. private NutritionFacts(Builder builder) {
  97. servingSize = builder.servingSize;
  98. servings = builder.servings;
  99. calories = builder.calories;
  100. fat = builder.fat;
  101. soduim = builder.sodium;
  102. carbohydrate = builder.carbohydrate;
  103. }
  104. }
  105.  
  106. NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
  107. calories(100).sodium(35).carbohydrate(27).build();
  108.  
  109. setOuterBounds(x, y, width, height);
  110. setInnerBounds(x + 2, y + 2, width - 4, height - 4);
  111.  
  112. setOuterBounds(bounds);
  113. setInnerBounds(bounds.expand(-2));
  114.  
  115. public static void setEmail(String newEmail){
  116. this.email = newEmail;
  117. }
  118.  
  119. public static String getEmail(){
  120. return this.email;
  121. }
  122.  
  123. ContactInfo cinfos = new ContactInfo[] {
  124. new ContactInfo("home", "+123456789", "123 ABC Avenue"),
  125. new ContactInfo("biz", "+987654321", "789 ZYX Avenue")
  126. };
  127.  
  128. Customer c = new Customer("john", "doe", cinfos);
Add Comment
Please, Sign In to add comment