Guest User

Untitled

a guest
Dec 11th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package com.sibomots;
  2.  
  3. // $0.02
  4.  
  5. // Cannonical Builder pattern usage:
  6. // BuildableThing myThing = new BuildableThing().setColor("red").setMass(10).setDensity(2.4f);
  7.  
  8. // Pedantic
  9. // BuildableThing myOtherThing = new BuildableThing.setColor("crap").setMass(100).setDensity(1.0f).build();
  10.  
  11. // The need to book-end the call with a .build() is not necessary in a Builder pattern. It signifies a bit
  12. // of the implementation to finally construct a new BuildableThing isn't really over until build() puts a bow on
  13. // the object instance.
  14.  
  15. // I prefer the Cannonical approach.
  16.  
  17. // A lot of the time, Builder pattern is used for when a Class has a tremendous number of
  18. // parameters that would make constructors really unweildlly.
  19.  
  20. public class BuildableThing {
  21.  
  22. // If the number of fields needed on the Constructor is more than 4, likely the best way to go
  23. // is to make it use a Builder pattern.
  24. // Eg:
  25. // BuildableThing myBestThing = new BuildableThing(); // OK, no builder needed.
  26. // BuildableThing myBestThing2 = new BuildableThing("green"); // no builder needed.
  27. // BuildableThing myBestThing3 = new BuildableThing("orange", 10); // no builder needed.
  28. // BuildableThing myBestThing4 = new BuildableThing("orange", 10, 5.4f); // no builder needed, getting close
  29. // BuildableThing myBestThing5 = new BuildableThing("orange", 10, 5.4f, 1); // builder needed, out of hand!
  30. //
  31. // Rationale:
  32. //
  33. // As the number of arguments increase, it's more difficult to keep straight which parameter goes in which
  34. // order and the code becomes difficult to read.
  35. // Plus, what if in making a BuildableThing, I want a color and a density, but not a mass. Or some other
  36. // combination of "attributes". In other words, a BuildableThing needs a set of values defined. How many?
  37. // If the answer is "we don't care" or "don't know", then Builder fits well.
  38. //
  39. // Which gets us back to the .build() method. If the required fields to "set" are indeed non trivially important
  40. // then build() can perform the check to see if they were set and throw/error out. That's one effect of that bookend
  41. // method.
  42.  
  43. private String color;
  44. private int mass;
  45. private float density;
  46. private int x;
  47. private int y;
  48. private int z;
  49. // etc..
  50.  
  51. public BuildableThing()
  52. {
  53. // initialize any member data, if necessary, right here.
  54. }
  55.  
  56. public BuildableThing setColor(String color)
  57. {
  58. this.color = color;
  59. return this;
  60. }
  61.  
  62. public BuildableThing setMass(int mass)
  63. {
  64. this.mass = mass;
  65. return this;
  66. }
  67.  
  68. public BuildableThing setDensity(float density)
  69. {
  70. this.density = density;
  71. return this;
  72. }
  73.  
  74. public BuildableThing setX(int x)
  75. {
  76. this.x = x;
  77. return this;
  78. }
  79. }
Add Comment
Please, Sign In to add comment