Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. # Refactoring to Patterns
  2.  
  3. Are patterns a panacea to all your design ills? Of course not.
  4.  
  5. > Only hard workd and dedication can work magic.
  6.  
  7. Patterns help mostly when you already know the problem space. To help understand a problem space, refactor *towards* a pattern, even if you don't end up with some shinny example at the end.
  8.  
  9. ## Creation Refactorings
  10.  
  11. ### Replace Constructor Sprawl with *Creation Methods*
  12.  
  13. - Constructors on a class make it hard to decide which constructor to call during development
  14. - Communicates what kind of instances are available better than uber-constructors
  15.  
  16. **Before**
  17.  
  18. ```
  19. Loan(commitment, riskRating, maturity)
  20. Loan(commitment, riskRating, maturity, expiry)
  21. Loan(commitment, outstanding, riskRating, maturity, expiry)
  22. Loan(capitalStrategy, commitment, riskRating, maturity, expiry)
  23. Loan(capitalStrategy, commitment, outstanding, riskRating, maturity, expiry)
  24. ```
  25.  
  26. **After**
  27.  
  28. ```
  29. createTermLoan(commitment, riskRating, maturity)
  30. createTermLoan(capitalStrategy, commitment, riskRating, maturity)
  31. createRevolver(commitment, outstanding, riskRating, expiry)
  32. createRevolver(capitalStrategy, commitment, outstanding, riskRating, expiry)
  33. createRevolvingConvertible(comitment, outstanding, riskRating, maturity, expiry)
  34. createRevolvingConvertible(capitalStrategy, comitment, outstanding, riskRating, maturity, expiry)
  35. ```
  36.  
  37. ### Move creation helper classes into a *Factory*
  38.  
  39. - If it takes lots of different classes to create some other class, then they should be bundled up, with a bow.
  40. - Now the client only has to do one thing, say, let yourEx = new Chimera().
  41. - Negative: You'll have to add new methods when you want new kinds of instances
  42.  
  43. ### Encapsualte composite code with a *Builder*
  44.  
  45. - Simplifies the number of concepts and classes that a client has to understand
  46. - Reduces error-prone nature of composing something in many locations
  47. - Negative: It may hide too many details about the super object that it's creating
  48.  
  49. ## Simplification
  50.  
  51. ### Compose a *Smaller Method*
  52.  
  53. - If you can't rapidly understand a method's logic, then transform the logic into small, intention-revealing steps.
  54. - Allows you to start coding without knowing the details of how something is done within a method.
  55. - Negative: You can end up with a lot of small, private methods.
  56.  
  57. **Before**
  58.  
  59. ```
  60. public void add(Object element){
  61. if(!readOnly){
  62. int newSize = size + 1;
  63. if(newSize > elements.length){
  64. Object[] newElements = new Object[elements.length + 10];
  65. for(int i = 0; i < size; i++){
  66. newElements[i] = elements[i]
  67. }
  68. elements = newElements;
  69. }
  70. elements[size++] = element;
  71. }
  72. }
  73. ```
  74.  
  75. **After**
  76.  
  77. ```
  78. public void add(Object element){
  79. if(readOnly){
  80. return;
  81. }
  82. if(atCapacity()){
  83. grow();
  84. }
  85. addElement();
  86. }
  87. ```
  88.  
  89. ### Replace Conditional Logic with a *Strategy*
  90.  
  91. - When conditional logic controls variations of the same algorithm, encapsulate that algorithm into it's own class
  92. - Enables a different (maybe more performant) algorithm to be swapped out at runtime
  93. - Negative: Complicates the design and adds more concepts outside of the core problem
  94.  
  95. ### Add more class functionality with a *Decorator*
  96.  
  97. - Instead of adding new code to old classes, wrap the old class in a new "something" that adds the functionality.
  98. - Keeps old clients unaware of the changes
  99. - Negative: Unless you're programming to interfaces with strict composition, this can lead to too many unexpected classes
  100.  
  101. ### Replace State-Altering Conditionals with *State Classes*
  102.  
  103. - Centralize so that one class controls the details of state transitions.
  104. - If done right, then from the client, you can understand the semantics behind
  105. - Negative: If done wrong, then you'll have to manage nonsense concepts when passing control between classes
  106.  
  107. ## Generalization
  108.  
  109. ### Create a *Template Method*
  110.  
  111. - Two methods perform similar steps, in the same order, but the details are different.
  112. - Example: every iterator method *ever*
  113. - Removes duplicate code
  114.  
  115. ### Extract a *Superclass* and share methods
  116.  
  117. - When you end creating 3+ similar classes, it's time to let our powers combine
  118. - Eliminates duplicate code that's shared between multiple classes
  119.  
  120. ### Unify Interfaces with *Adapter*
  121.  
  122. - Commonly used when you need the two classes to talk to each other but their interfaces are just different enough that the compiler has a tantrum.
  123. - Can also be used to simplify the surface area of a class where you only need a subset of it's functionality.
  124. - Negative: Can inadvertently hide some nice-to-have functionality and end up re-creating it later.
  125.  
  126. ## End Notes
  127.  
  128. - Let the domain's specifics guide refactoring more than language specifics.
  129. - Refactorings work in tiny steps. They are evolutions, not revolutions.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement