980 lines
31 KiB
Markdown
980 lines
31 KiB
Markdown
<!-- GFM-TOC -->
|
||
* [????? ?????????](#?????-?????????)
|
||
* [????? ??????](#?????-??????)
|
||
* [?????? ??????¦Æ??](#??????-??????¦Æ??)
|
||
* [1. Duplicated Code?????????](#1-duplicated-code?????????)
|
||
* [2. Long Method????????????](#2-long-method????????????)
|
||
* [3. Large Class?????????](#3-large-class?????????)
|
||
* [4. Long Parameter List????????????§µ?](#4-long-parameter-list????????????§å?)
|
||
* [5. Divergent Change???????<3F>£??](#5-divergent-change???????<3F>£??)
|
||
* [6. Shotgun Surgery??????????](#6-shotgun-surgery??????????)
|
||
* [7. Feature Envy?????????](#7-feature-envy?????????)
|
||
* [8. Data Clumps???????????](#8-data-clumps???????????)
|
||
* [9. Primitive Obsession??????????????](#9-primitive-obsession??????????????)
|
||
* [10. Switch Statements??switch ???????](#10-switch-statements??switch-???????)
|
||
* [11. Parallel Inheritance Hierarchies????§Þ???????](#11-parallel-inheritance-hierarchies????§Þ???????)
|
||
* [12. Lazy Class????????](#12-lazy-class????????)
|
||
* [13. Speculative Generality????????¦Ä?????](#13-speculative-generality????????¦Ä?????)
|
||
* [14. Temporary Field???????????????¦²?](#14-temporary-field???????????????¦Ò?)
|
||
* [15. Message Chains?????????????????](#15-message-chains?????????????????)
|
||
* [16. Middle Man???§Þ????](#16-middle-man???§Þ????)
|
||
* [17. Inappropriate Intimacy??????????](#17-inappropriate-intimacy??????????)
|
||
* [18. Alernative Classes with Different Interfaces?????????????](#18-alernative-classes-with-different-interfaces?????????????)
|
||
* [19. Incomplete Library Class?????????????](#19-incomplete-library-class?????????????)
|
||
* [20. Data Class?????????????](#20-data-class?????????????)
|
||
* [21. Refused Bequest???????????????](#21-refused-bequest???????????????)
|
||
* [22. Comments???????????](#22-comments???????????)
|
||
* [?????? ???????????](#??????-???????????)
|
||
* [?????? ????§Ò?](#??????-????§Ò?)
|
||
* [?????? ???????????](#??????-???????????)
|
||
* [1. Extract Method????????????](#1-extract-method????????????)
|
||
* [2. Inline Method????????????](#2-inline-method????????????)
|
||
* [3. Inline Temp???????????????](#3-inline-temp???????????????)
|
||
* [4. Replace Temp with Query??????????????????](#4-replace-temp-with-query??????????????????)
|
||
* [5. Introduce Explaining Variable??????????????](#5-introduce-explaining-variable??????????????)
|
||
* [6. Split Temporary Variable??????????????](#6-split-temporary-variable??????????????)
|
||
* [7. Remove Assigments to Parameters????????????????](#7-remove-assigments-to-parameters????????????????)
|
||
* [8. Replace Method with Method Object????????????????????](#8-replace-method-with-method-object????????????????????)
|
||
* [9. Subsititute Algorithn???<3F>I????](#9-subsititute-algorithn???<3F>I????)
|
||
* [?????? ???????????????](#??????-???????????????)
|
||
* [1. Move Method???????????](#1-move-method???????????)
|
||
* [2. Move Field????????¦²?](#2-move-field????????¦Ò?)
|
||
* [3. Extract Class????????](#3-extract-class????????)
|
||
* [4. Inline Class??????????????](#4-inline-class??????????????)
|
||
* [5. Hide Delegate?????????§Û??????](#5-hide-delegate?????????§Û??????)
|
||
* [6. Remove Middle Man??????§Þ????](#6-remove-middle-man??????§Þ????)
|
||
* [7. Introduce Foreign Method??????????????](#7-introduce-foreign-method??????????????)
|
||
* [8. Introduce Local Extension?????????????](#8-introduce-local-extension?????????????)
|
||
* [????? ???????????](#?????-???????????)
|
||
* [1. Self Encapsulate Field????????¦²?](#1-self-encapsulate-field????????¦Ò?)
|
||
* [2. Replace Data Value with Object?????????????????](#2-replace-data-value-with-object?????????????????)
|
||
* [3. Change Value to Reference??????????????????](#3-change-value-to-reference??????????????????)
|
||
* [4. Change Reference to value??????????????????](#4-change-reference-to-value??????????????????)
|
||
* [5. Replace Array with Object?????????????ï‚](#5-replace-array-with-object?????????????ï‚)
|
||
* [6. Duplicate Observed Data????????????????????](#6-duplicate-observed-data????????????????????)
|
||
* [7. Change Unidirectional Association to Bidirectional??????????????????????](#7-change-unidirectional-association-to-bidirectional??????????????????????)
|
||
* [8. Change Bidirectional Association to Unidirectional??????????????????????](#8-change-bidirectional-association-to-unidirectional??????????????????????)
|
||
* [9. Replace Magic Number with Symbolic Constant???????Æü????????????](#9-replace-magic-number-with-symbolic-constant???????Æü????????????)
|
||
* [10. Encapsulate Field???????¦²?](#10-encapsulate-field???????¦Ò?)
|
||
* [11. Encapsulate Collection??????????](#11-encapsulate-collection??????????)
|
||
* [12. Replace Record with Data Class??????????????????](#12-replace-record-with-data-class??????????????????)
|
||
* [13. Replace Type Code with Class???????????????](#13-replace-type-code-with-class???????????????)
|
||
* [14. Replace Type Code with Subcalsses?????????????????](#14-replace-type-code-with-subcalsses?????????????????)
|
||
* [15. Replace Type Code with State/Strategy ???? State/Strategy ?????????](#15-replace-type-code-with-statestrategy-????-statestrategy-?????????)
|
||
* [16. Replace Subclass with Fields??????????????](#16-replace-subclass-with-fields??????????????)
|
||
* [????? ??????????](#?????-??????????)
|
||
* [1. Decompose Conditional???????????????](#1-decompose-conditional???????????????)
|
||
* [2. Consolidate Conditional Expression???????????????](#2-consolidate-conditional-expression???????????????)
|
||
* [3. Consolidate Duplicate Conditional Fragments ????????????????¦²?](#3-consolidate-duplicate-conditional-fragments-????????????????¦Ò?)
|
||
* [4. Remove Control Flag????????????](#4-remove-control-flag????????????)
|
||
* [5. Replace Nested Conditional with Guard Clauses ?????????????????????????](#5-replace-nested-conditional-with-guard-clauses-?????????????????????????)
|
||
* [6. Replace Conditional with Polymorphism ???????????????????](#6-replace-conditional-with-polymorphism-???????????????????)
|
||
* [7. Introduce Null Object??????Null????](#7-introduce-null-object??????null????)
|
||
* [8. Introduce Assertion??????????](#8-introduce-assertion??????????)
|
||
* [????? ?????????](#?????-?????????)
|
||
* [1. Rename Method????????????](#1-rename-method????????????)
|
||
* [2. Add Parameter??????????](#2-add-parameter??????????)
|
||
* [3. Remove Parameter???????????](#3-remove-parameter???????????)
|
||
* [4. Separate Query from Modifier???????????????????????](#4-separate-query-from-modifier???????????????????????)
|
||
* [5. Parameterize Method??????§Á????????](#5-parameterize-method??????§ñ????????)
|
||
* [6. Replace Parameter with Explicit Methods????????????????????](#6-replace-parameter-with-explicit-methods????????????????????)
|
||
* [7. Preserve Whole Object???????????????](#7-preserve-whole-object???????????????)
|
||
* [8. Replace Parameter with Methods????????????????](#8-replace-parameter-with-methods????????????????)
|
||
* [9. Introduce Parameter Object?????????????](#9-introduce-parameter-object?????????????)
|
||
* [10. Remove Setting Method??????????????](#10-remove-setting-method??????????????)
|
||
* [11. Hide Method???????????](#11-hide-method???????????)
|
||
* [12. Replace Constructor with Factory Method ??????????????????????](#12-replace-constructor-with-factory-method-??????????????????????)
|
||
* [13. Encapsulate Downcast?????????????](#13-encapsulate-downcast?????????????)
|
||
* [14. Replace Error Code with Exception ???????????????](#14-replace-error-code-with-exception-???????????????)
|
||
* [15. Replace Exception with Test??????????????](#15-replace-exception-with-test??????????????)
|
||
* [?????? ??????????](#??????-??????????)
|
||
* [1. Pull Up Field??????????](#1-pull-up-field??????????)
|
||
* [2. Pull Up Method???????????](#2-pull-up-method???????????)
|
||
* [3. Pull Up Constructor Body?????????????????](#3-pull-up-constructor-body?????????????????)
|
||
* [4. Push Down Method???????????](#4-push-down-method???????????)
|
||
* [5. Push Down Field??????????](#5-push-down-field??????????)
|
||
* [6. Extract Subclass??????????](#6-extract-subclass??????????)
|
||
* [7. Extract Superclass??????????](#7-extract-superclass??????????)
|
||
* [8. Extract Interface??????????](#8-extract-interface??????????)
|
||
* [9. Collapse Hierarchy?????????????](#9-collapse-hierarchy?????????????)
|
||
* [10. Form Template Method????????Žï????](#10-form-template-method????????Žï????)
|
||
* [11. Replace Inheritance with Delegation ????????????§µ?](#11-replace-inheritance-with-delegation-????????????§å?)
|
||
* [12. Replace Delegation with Inheritance ???????????§µ?](#12-replace-delegation-with-inheritance-???????????§å?)
|
||
<!-- GFM-TOC -->
|
||
|
||
# ????? ?????????
|
||
|
||
?????????????????????????????????????????????????????????????????????
|
||
|
||
???????????????????????????????????????????
|
||
|
||
????????§³?????????????????????????????????????
|
||
|
||
**????????**
|
||
|
||
?????????¨®????????????Movie??Rental ?? Customer??Rental ????????? Movie ?????????
|
||
|
||

|
||
|
||
??????????????§Ö?????????? Customer ???§µ???<3F>£?????????????????????§Ú???????????§á????????<3F>£?§µ??????????????????????????????????????????????????????????????????????????§Þ????????????
|
||
|
||

|
||
|
||
???????? Movie ????????????????????? Movie ?????????????????????????§³?
|
||
|
||

|
||
|
||
???? Price ???????????????????????? Movie ?§Þ??? Price ?????????????????????????????? Price ?????§µ????? Movie ?????????????????????????????????????????<3F>£??
|
||
|
||

|
||
|
||
?????????????????
|
||
|
||

|
||
|
||

|
||
|
||
# ????? ??????
|
||
|
||
??????????????????????????????????????????????????????????????????????????????
|
||
|
||
??????????????????????????????????????? bug???????????
|
||
|
||
???¦Ç????????????????????????????????????????????????????????????????????????????
|
||
|
||
????????????????????§Ö?????????????????????????????????????????????????????????????????????????????<3F>£????????????????????????????????¦Ë?¨°???????????????????????
|
||
|
||
???????????????????????????????????????? Java ???? @deprecation ????????????¨¢????????§Ò????????????????????????¾ˆ??????
|
||
|
||
?????§Õ?????????????????§Õ?????????????????§Ö???????????????????????????????????????§Õ??????????????
|
||
|
||
?????????????????????????????§Ü??<3F>£????????????????????????????????????????????????????????????????????????????<3F>£??????????????????????<3F>£???????????????????
|
||
|
||
?????????????????????????????????????§Õ?????????????????????????????????????????????????????????????????????????????????§³????????????????
|
||
|
||
# ?????? ??????¦Æ??
|
||
|
||
## 1. Duplicated Code?????????
|
||
|
||
?????????????????????????????? Extract Method ????????????
|
||
|
||
???????????????????????????????? Extract Method?????????????????? Pull Up Method ??????
|
||
|
||
???????????????? Extract Method ????????????????????????? Form Template Method ??????Ùã?????????
|
||
|
||
???????????????????????????????? Extract Class ????????????????????????????§³?
|
||
|
||
## 2. Long Method????????????
|
||
|
||
??????????????????????????????????????
|
||
|
||
??????????????????????????¦Ä????????????????????§Õ???????????????§³?
|
||
|
||
Extract Method ??????????????????????????????????? Replace Temp with Query ?????????????Introduce Parameter Object ?? Preserve Whole Object ???????????????§Ò??????
|
||
|
||
?????????????????????????????§³?
|
||
|
||
## 3. Large Class?????????
|
||
|
||
?????????????????ï…?????? Extract Class ?? Extract Subclass??
|
||
|
||
???????????????????????????? Extract Interface ???????¡Â??????????????
|
||
|
||
## 4. Long Parameter List????????????§µ?
|
||
|
||
## 5. Divergent Change???????<3F>£??
|
||
|
||
????????????<3F>£?????
|
||
|
||
??????????<3F>£????? Extract Class ????????????????§³?
|
||
|
||
## 6. Shotgun Surgery??????????
|
||
|
||
????<3F>£????????????
|
||
|
||
??? Move Method ?? Move Field ?????????????????????????§³?
|
||
|
||
## 7. Feature Envy?????????
|
||
|
||
?????????????????????????????????????????????????????????????
|
||
|
||
??? Move Method ??????????????????????????? Feature Envy?????? Extract Method ??????????????
|
||
|
||
## 8. Data Clumps???????????
|
||
|
||
??§»??????????????????????????????????¦±????????????????????? Extract Class ????????????
|
||
|
||
## 9. Primitive Obsession??????????????
|
||
|
||
???????????????????????????? Replace Data Value with Object ????????<3F>I?????
|
||
|
||
## 10. Switch Statements??switch ???????
|
||
|
||
## 11. Parallel Inheritance Hierarchies????§Þ???????
|
||
|
||
???????????????????????????????????????????????
|
||
|
||
????????????§»?????????????????????????????????????????????????????????????
|
||
|
||
## 12. Lazy Class????????
|
||
|
||
?????????????????????????????????
|
||
|
||
## 13. Speculative Generality????????¦Ä?????
|
||
|
||
??§»??????????????¦Ä???????????<3F>£?????????????????????????????????????¦Ä?????????????????????????????????????????????????????????
|
||
|
||
## 14. Temporary Field???????????????¦²?
|
||
|
||
?????¦Í??????????????Ñk?????????????????????????????????????????????????¦±?
|
||
|
||
????????¦Ê?????????????????? Extract Class ????????????????§³?
|
||
|
||
## 15. Message Chains?????????????????
|
||
|
||
??????????????????????????????????????????????...????????????????????????????¦Æ?????????????????????????
|
||
|
||
???¨²????????¨²?????????????????????
|
||
|
||
## 16. Middle Man???§Þ????
|
||
|
||
?§Þ??????????§Ú?????????????????????§Û???????????§Ú?????????????????????§µ???? Remove Middle Man?????????????????
|
||
|
||
## 17. Inappropriate Intimacy??????????
|
||
|
||
??????????????????????????????? private ????
|
||
|
||
## 18. Alernative Classes with Different Interfaces?????????????
|
||
|
||
## 19. Incomplete Library Class?????????????
|
||
|
||
?????????????????????????????????????????????§»????????????????????????????????????????????????? Introduce Foreign Method???????????????????????? Introduce Local Extension??
|
||
|
||
## 20. Data Class?????????????
|
||
|
||
???????§»??????¦±?
|
||
|
||
????????????????????????????? Data Class ?§³?
|
||
|
||
## 21. Refused Bequest???????????????
|
||
|
||
?????§Ô???????§Ü??????????????????????????
|
||
|
||
??????????????????????????????????? Push Down Method ?? Push Down Field ????????????
|
||
|
||
## 22. Comments???????????
|
||
|
||
??? Extract Method ?????????????????????¨²?????????????????????
|
||
|
||
# ?????? ???????????
|
||
|
||
Java ??????? Junit ???§Ö???????
|
||
|
||
????????????????????????????????????????????????§³?
|
||
|
||
??????§Ó??????????????????
|
||
|
||
# ?????? ????§Ò?
|
||
|
||
§³???????????????
|
||
|
||
# ?????? ???????????
|
||
|
||
## 1. Extract Method????????????
|
||
|
||
????¦Ä?????????????????§µ????¨²??????????¨²??????????
|
||
|
||
## 2. Inline Method????????????
|
||
|
||
?????????????????????????????
|
||
|
||
??????????????????ÈÉ???????¨²?????
|
||
|
||
## 3. Inline Temp???????????????
|
||
|
||
?????????????????????????¦²???????????????????????
|
||
|
||
?????§Ø????????????<3F>I?????????????????????
|
||
|
||
```java
|
||
double basePrice = anOrder.basePrice();
|
||
return basePrice > 1000;
|
||
```
|
||
|
||
```java
|
||
return anOrder.basePrice() > 1000;
|
||
```
|
||
|
||
## 4. Replace Temp with Query??????????????????
|
||
|
||
????????????????????????????????????????????????????????§µ??????§Ø????????????????<3F>I????????????¨¢?Replace Temp with Query ?????? Extract Method ????????????????Ñk???????????????????????????
|
||
|
||
```java
|
||
double basePrice = quantity * itemPrice;
|
||
if(basePrice > 1000)
|
||
return basePrice * 0.95;
|
||
else
|
||
return basePrice * 0.98;
|
||
```
|
||
|
||
```java
|
||
if(basePrice() > 1000)
|
||
return basePrice() * 0.95;
|
||
else
|
||
return basePrice() * 0.98;
|
||
|
||
// ...
|
||
double basePrice(){
|
||
return quantity * itemPrice;
|
||
}
|
||
```
|
||
|
||
## 5. Introduce Explaining Variable??????????????
|
||
|
||
??????????????????????????????????????????????????????????????????
|
||
|
||
```java
|
||
if((platform.toUpperCase().indexOf("MAC") > -1) &&
|
||
(browser.toUpperCase().indexOf("IE") > -1) &&
|
||
wasInitialized() && resize > 0) {
|
||
// do something
|
||
}
|
||
```
|
||
|
||
```java
|
||
final boolean isMacOS = platform.toUpperCase().indexOf("MAC") > -1;
|
||
final boolean isIEBrower = browser.toUpperCase().indexOf("IE") > -1;
|
||
final boolean wasResized = resize > 0;
|
||
|
||
if(isMacOS && isIEBrower && wasInitialized() && wasResized) {
|
||
// do something
|
||
}
|
||
```
|
||
|
||
## 6. Split Temporary Variable??????????????
|
||
|
||
?????????????????????¦²?????????????????????????????????????
|
||
|
||
?????¦È???????????????????????????????????????????§Ö???????¦±?
|
||
|
||
## 7. Remove Assigments to Parameters????????????????
|
||
|
||
?????????????????¨°?????????
|
||
|
||
```java
|
||
int discount (int inputVal, int quentity, int yearToDate){
|
||
if (inputVal > 50) inputVal -= 2;
|
||
```
|
||
|
||
```java
|
||
int discount (int inputVal, int quentity, int yearToDate){
|
||
int result = inputVal;
|
||
if (inputVal > 50) result -= 2;
|
||
```
|
||
|
||
## 8. Replace Method with Method Object????????????????????
|
||
|
||
?????????????????? Extract Method ????????????????????¨²?????§Ú¨°?????
|
||
|
||
????????????????????????§µ???????????????????????????¦±??????????????????§ß??????????????????§³???????
|
||
|
||
## 9. Subsititute Algorithn???<3F>I????
|
||
|
||
# ?????? ???????????????
|
||
|
||
## 1. Move Method???????????
|
||
|
||
???§Ö???????????????????§Ú??????????¨²?????????????¨¢?
|
||
|
||
??????????????????????§³?
|
||
|
||
## 2. Move Field????????¦²?
|
||
|
||
???§Ö??????¦Á???????????????????????????????????????????????????????????????§³?
|
||
|
||
## 3. Extract Class????????
|
||
|
||
???????????????????????????
|
||
|
||
??????????????????????¦Ê?????????????????
|
||
|
||
## 4. Inline Class??????????????
|
||
|
||
?? Extract Class ????
|
||
|
||
## 5. Hide Delegate?????????§Û??????
|
||
|
||
????????????????????§Û????
|
||
|
||
```java
|
||
class Person{
|
||
Department department;
|
||
|
||
public Department getDepartment(){
|
||
return department;
|
||
}
|
||
}
|
||
|
||
class Department{
|
||
private Person manager;
|
||
|
||
public Person getManager(){
|
||
return manager;
|
||
}
|
||
}
|
||
```
|
||
|
||
?????????????????????????????? Department ?????????????????? Department ????????
|
||
|
||
```java
|
||
Person manager = john.getDepartment().getManager();
|
||
```
|
||
|
||
???? Peron ???????????????????????§Û????
|
||
|
||
```java
|
||
public Person getManager(){
|
||
return department.getManager();
|
||
}
|
||
```
|
||
|
||
## 6. Remove Middle Man??????§Þ????
|
||
|
||
?? Hide Delegate ??????????????????§Ü????????????????????
|
||
|
||
Hide Delegate ?§Ü???????????????????????????????????????????????????????????????????????§Ü?????????????§Ö?????????????????????????????????§Þ??????
|
||
|
||
## 7. Introduce Foreign Method??????????????
|
||
|
||
????????????????????????????????????????
|
||
|
||
???????????§ß???????????????????????????????????????????????????????????????
|
||
|
||
## 8. Introduce Local Extension?????????????
|
||
|
||
?? Introduce Foreign Method ???????????? Introduce Local Extension ??????????????????????????????????????????????????????????????????????????
|
||
|
||
# ????? ???????????
|
||
|
||
## 1. Self Encapsulate Field????????¦²?
|
||
|
||
???¦Í?????/???????????????§»????????????¦±???§Ö???????????????????¦²????????????§ß????????¦Ç??????????????????????????????????????????¦Å???????????
|
||
|
||
## 2. Replace Data Value with Object?????????????????
|
||
|
||
?????????????????¨¹?????????????????????????????????§µ??§»?????????????§»????????????????????´Â??????????????????????´Â?????????????????????????????????????????
|
||
|
||
## 3. Change Value to Reference??????????????????
|
||
|
||
?????????????<3F>I????????????????????????????????¦·???????????????????????????????????§Ò???????????????????????????§Ò?????????????????????????????§Ò??§Ö??????????????????????????§Ò??§µ???????????
|
||
|
||
## 4. Change Reference to value??????????????????
|
||
|
||
?? Change Value to Reference ??????????§Ú??????????????????????????????????????????????????????????????<3F>I?????????????????
|
||
|
||
???????????? equals() ?? hashCode() ????
|
||
|
||
## 5. Replace Array with Object?????????????ï‚
|
||
|
||
????????ï…???§Ö??????????????????
|
||
|
||
??????<3F>I???ï…?????????§Ö??????????????????????????????????????????????
|
||
|
||
## 6. Duplicate Observed Data????????????????????
|
||
|
||
?§»?????????????? GUI ????§µ??????????????????§»?????
|
||
|
||
???????????????????????§µ???????? Oberver ??????????????????? GUI ???????????????
|
||
|
||

|
||
|
||
## 7. Change Unidirectional Association to Bidirectional??????????????????????
|
||
|
||
??????????????????????????????????????
|
||
|
||
???????????????? Order ???? Customer??Order ?????? Customer??Customer ???????? Order ?????????§Ø??????î•
|
||
|
||
```java
|
||
class Order{
|
||
private Customer customer;
|
||
public void setCustomer(Customer customer){
|
||
if(this.customer != null)
|
||
this.customer.removeOrder(this);
|
||
this.customer = customer;
|
||
this.customer.add(this);
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
class Curstomer{
|
||
private Set<Order> orders = new HashSet<>();
|
||
public void removeOrder(Order order){
|
||
orders.remove(order);
|
||
}
|
||
public void addOrder(Order order){
|
||
orders.add(order);
|
||
}
|
||
}
|
||
```
|
||
|
||
??????????? Curstomer ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
|
||
|
||
## 8. Change Bidirectional Association to Unidirectional??????????????????????
|
||
|
||
?? Change Unidirectional Association to Bidirectiona ?????????
|
||
|
||
?????????????????????????????????????????????????¨À??????????????????????????????????????§µ???????????????????????????
|
||
|
||
## 9. Replace Magic Number with Symbolic Constant???????Æü????????????
|
||
|
||
???????????????????????????????????????Æü????¦Ë?????????
|
||
|
||
## 10. Encapsulate Field???????¦²?
|
||
|
||
public ????????? private???????????????????
|
||
|
||
## 11. Encapsulate Collection??????????
|
||
|
||
????????????????????????????????????????/??????????????????????????????????????????????????????????????????????????
|
||
|
||
## 12. Replace Record with Data Class??????????????????
|
||
|
||
## 13. Replace Type Code with Class???????????????
|
||
|
||
?????????????????????????????????????????????????<3F>I????????????????????????? switch ????§µ??????? Replace Conditional with Polymorphism ??? switch????????????? Replace Type Code with Subcalss ?? Replace Type Code with State/Strategy ?????????
|
||
|
||

|
||
|
||
## 14. Replace Type Code with Subcalsses?????????????????
|
||
|
||
??????????????????????????????????????????????????
|
||
|
||

|
||
|
||
## 15. Replace Type Code with State/Strategy ???? State/Strategy ?????????
|
||
|
||
???????????????????????????????????????????????
|
||
|
||
?? Replace Type Code with Subcalsses ???????? Replace Type Code with State/Strategy ?????????????????????????§Ö???????????????????????????????????????????????§Ö????????????????????????????????????????????????????????????????????????????????????????????????????????
|
||
|
||

|
||
|
||
## 16. Replace Subclass with Fields??????????????
|
||
|
||
?????????¦·????????????????????????????
|
||
|
||

|
||
|
||
# ????? ??????????
|
||
|
||
## 1. Decompose Conditional???????????????
|
||
|
||
???????????????????????? if??then??else ?????????§Ù??????????????????
|
||
|
||
```java
|
||
if(data.befor(SUMMER_START) || data.after(SUMMER_END))
|
||
charge = quantity * winterRate + winterServiceCharge;
|
||
else charge = quantity * summerRate;
|
||
```
|
||
|
||
```java
|
||
if(notSummer(date))
|
||
charge = winterCharge(quantity);
|
||
else charge = summerCharge(quantity);
|
||
```
|
||
|
||
## 2. Consolidate Conditional Expression???????????????
|
||
|
||
????????????????????????????
|
||
|
||
????§»???????????????????????????????????????????????????????
|
||
|
||
```java
|
||
double disabilityAmount(){
|
||
if (seniority < 2) return 0;
|
||
if (monthsDisabled > 12 ) return 0;
|
||
if (isPartTime) return 0;
|
||
// ...
|
||
}
|
||
```
|
||
```java
|
||
double disabilityAmount(){
|
||
if (isNotEligibleForDisability()) return 0;
|
||
// ...
|
||
}
|
||
```
|
||
|
||
## 3. Consolidate Duplicate Conditional Fragments ????????????????¦²?
|
||
|
||
???????????????????????????????¦Ä???
|
||
|
||
???????????????????????????
|
||
|
||
```java
|
||
if (isSpecialDeal()){
|
||
total = price * 0.95;
|
||
send();
|
||
} else {
|
||
total = price * 0.98;
|
||
send();
|
||
}
|
||
```
|
||
|
||
```java
|
||
if (isSpecialDeal()) {
|
||
total = price * 0.95;
|
||
} else {
|
||
total = price * 0.98;
|
||
}
|
||
send();
|
||
```
|
||
|
||
## 4. Remove Control Flag????????????
|
||
|
||
?????§Ó????????§µ???????????§³?????????????¨¢?
|
||
|
||
?? break?? ??? return ???????????????
|
||
|
||
## 5. Replace Nested Conditional with Guard Clauses ?????????????????????????
|
||
|
||
?????????????<3F>‹????????????????????????????????????????????§Ù???????????????ø@????????????????guard clauses????
|
||
|
||
???????????????????????????????????????§Ù??????????????????????????????????????????????????????????????????????????????????????????????????????????????
|
||
|
||
```java
|
||
double getPayAmount() {
|
||
double result;
|
||
if (isDead) result = deadAmount();
|
||
else {
|
||
if (isSeparated) result = separatedAmount();
|
||
else {
|
||
if (isRetired) result = retiredAmount();
|
||
else result = normalPayAmount();
|
||
};
|
||
}
|
||
return result;
|
||
};
|
||
```
|
||
|
||
```java
|
||
double getPayAmount() {
|
||
if (isDead) return deadAmount();
|
||
if (isSeparated) return separatedAmount();
|
||
if (isRetired) return retiredAmount();
|
||
return normalPayAmount();
|
||
};
|
||
```
|
||
|
||
## 6. Replace Conditional with Polymorphism ???????????????????
|
||
|
||
???????????????????????????????????§Õ?????§µ??????????????????????????????? Replace Type Code with Subclass ?? Replace Type Code with State/Strategy ????????§ß????
|
||
|
||
```java
|
||
double getSpeed() {
|
||
switch (type) {
|
||
case EUROPEAN:
|
||
return getBaseSpeed();
|
||
case AFRICAN:
|
||
return getBaseSpeed()- getLoadFactor()* numberOfCoconuts;
|
||
case NORWEGIAN_BLUE:
|
||
return isNailed ? 0 : getBaseSpeed(voltage);
|
||
}
|
||
throw new RuntimeException("Should be unreachable");
|
||
}
|
||
```
|
||
|
||

|
||
|
||
## 7. Introduce Null Object??????Null????
|
||
|
||
?? null ??<3F>I? null ?????????????????????????????????????????????§³?
|
||
|
||
```java
|
||
if (customer == null) plan = BillingPlan.basic();
|
||
else plan = customer.getPlan();
|
||
```
|
||
|
||
## 8. Introduce Assertion??????????
|
||
|
||
?????????????????ÒÂ???????????????????§µ?????????§Ó????§Ø????
|
||
|
||
```java
|
||
double getExpenseLimit() {
|
||
// should have either expense limit or a primary project
|
||
return (expenseLimit != NULL_EXPENSE) ? expenseLimit : primaryProject.getMemberExpenseLimit();
|
||
}
|
||
```
|
||
|
||
```java
|
||
double getExpenseLimit() {
|
||
Assert.isTrue (expenseLimit != NULL_EXPENSE || primaryProject != null);
|
||
return (expenseLimit != NULL_EXPENSE) ? expenseLimit : primaryProject.getMemberExpenseLimit();
|
||
}
|
||
```
|
||
|
||
# ????? ?????????
|
||
|
||
## 1. Rename Method????????????
|
||
|
||
??????????????????????
|
||
|
||
## 2. Add Parameter??????????
|
||
|
||
???????????????????????????
|
||
|
||
## 3. Remove Parameter???????????
|
||
|
||
?? Add Parameter ???????????????????????????
|
||
|
||
## 4. Separate Query from Modifier???????????????????????
|
||
|
||
?????????????????????????????????
|
||
|
||
??????????????????????????????????????????????????¦Ê??§Ù???????????????????§á??????????¨¢?
|
||
|
||
```java
|
||
getTotalOutstandingAndSetReadyForSummaries();
|
||
```
|
||
|
||
```java
|
||
getTotalOutstanding();
|
||
setReadyForSummaries();
|
||
```
|
||
|
||
## 5. Parameterize Method??????§Á????????
|
||
|
||
????????????????????????????????????????????????
|
||
|
||
???????????????????????§»????????
|
||
|
||
```java
|
||
fivePercentRaise();
|
||
tenPercentRaise();
|
||
```
|
||
```java
|
||
raise(percentage);
|
||
```
|
||
|
||
## 6. Replace Parameter with Explicit Methods????????????????????
|
||
|
||
????????????????????????????????????
|
||
|
||
???¨°?????????????????????????????????
|
||
|
||
```java
|
||
void setValue(String name, int value){
|
||
if (name.equals("height")){
|
||
height = value;
|
||
return;
|
||
}
|
||
if (name.equals("width")){
|
||
width = value;
|
||
return;
|
||
}
|
||
Assert.shouldNeverReachHere();
|
||
}
|
||
```
|
||
|
||
```java
|
||
void setHeight(int arg){
|
||
height = arg;
|
||
}
|
||
void setWidth(int arg){
|
||
width = arg;
|
||
}
|
||
```
|
||
|
||
## 7. Preserve Whole Object???????????????
|
||
|
||
?????????????????????????????????¦Ê???????????????
|
||
|
||
???????????????
|
||
|
||
```java
|
||
int low = daysTempRange().getLow();
|
||
int high = daysTempRange().getHigh();
|
||
withinPlan = plan.withinRange(low,high);
|
||
```
|
||
|
||
```java
|
||
withinPlan = plan.withinRange(daysTempRange());
|
||
```
|
||
|
||
## 8. Replace Parameter with Methods????????????????
|
||
|
||
?????????????????????????????????????????????????????????¨°??????????????????????????????
|
||
|
||
?¨°?????????????????????????????????????????
|
||
|
||
```java
|
||
int basePrice = _quantity * _itemPrice;
|
||
discountLevel = getDiscountLevel();
|
||
double finalPrice = discountedPrice (basePrice, discountLevel);
|
||
```
|
||
|
||
```java
|
||
int basePrice = _quantity * _itemPrice;
|
||
double finalPrice = discountedPrice (basePrice);
|
||
```
|
||
|
||
## 9. Introduce Parameter Object?????????????
|
||
|
||
?§»???????????????????????§»???????? Data Clumps??
|
||
|
||
??????????????§»??????
|
||
|
||

|
||
|
||
## 10. Remove Setting Method??????????????
|
||
|
||
???§Ö?????????????????????????????????
|
||
|
||
???????¦Å?????????????????????????? final??
|
||
|
||
## 11. Hide Method???????????
|
||
|
||
?????????????????§Ò??????¦Ê????????
|
||
|
||
????????????? private??
|
||
|
||
## 12. Replace Constructor with Factory Method ??????????????????????
|
||
|
||
???????????????????????????????????
|
||
|
||
?????????<3F>I???????????
|
||
|
||
## 13. Encapsulate Downcast?????????????
|
||
|
||
????????????????????????????????????????downcast????
|
||
|
||
????????????????????§³?
|
||
|
||
```java
|
||
Object lastReading(){
|
||
return readings.lastElement();
|
||
}
|
||
```
|
||
```java
|
||
Reading lastReading(){
|
||
return (Reading)readings.lastElement();
|
||
}
|
||
```
|
||
|
||
## 14. Replace Error Code with Exception ???????????????
|
||
|
||
???????????????????????????????????????
|
||
|
||
??????????????????????????????????????????
|
||
|
||
## 15. Replace Exception with Test??????????????
|
||
|
||
?????????????????????????????????????????
|
||
|
||
????????????????¨²???????????î•
|
||
|
||
```java
|
||
double getValueForPeriod(int periodNumber) {
|
||
try {
|
||
return values[periodNumber];
|
||
} catch (ArrayIndexOutOfBoundsException e) {
|
||
return 0;
|
||
}
|
||
}
|
||
```
|
||
```java
|
||
double getValueForPeriod(int periodNumber) {
|
||
if (periodNumber >= values.length) return 0;
|
||
return values[periodNumber];
|
||
```
|
||
|
||
# ?????? ??????????
|
||
|
||
## 1. Pull Up Field??????????
|
||
|
||
??????????????????¦±?
|
||
|
||
???????????????
|
||
|
||
## 2. Pull Up Method???????????
|
||
|
||
??§»????????????????§Ó???????????????
|
||
|
||
???¨²???????????
|
||
|
||
## 3. Pull Up Constructor Body?????????????????
|
||
|
||
?????????????????§»???????????????<3F>‹?????????
|
||
|
||
??????????????????????????????????§Ö???????
|
||
|
||
```java
|
||
class Manager extends Employee...
|
||
|
||
public Manager(String name, String id, int grade) {
|
||
this.name = name;
|
||
this.id = id;
|
||
this.grade = grade;
|
||
}
|
||
```
|
||
|
||
```java
|
||
public Manager(String name, String id, int grade) {
|
||
super(name, id);
|
||
this.grade = grade;
|
||
}
|
||
```
|
||
|
||
## 4. Push Down Method???????????
|
||
|
||
?????§Ö??????????????????§Û??
|
||
|
||
??????????????????§»???????
|
||
|
||
## 5. Push Down Field??????????
|
||
|
||
?????§Ö???????????????????????
|
||
|
||
????????????????????§»???????
|
||
|
||
## 6. Extract Subclass??????????
|
||
|
||
???§Ö??§»????????§»????????
|
||
|
||
????????????????????????????????????????§³?
|
||
|
||
## 7. Extract Superclass??????????
|
||
|
||
?????????????????
|
||
|
||
???????????????????????????????????
|
||
|
||
## 8. Extract Interface??????????
|
||
|
||
??????????????§Ö??????????????????????§Ó????????
|
||
|
||
???????????????????????????§³?
|
||
|
||
## 9. Collapse Hierarchy?????????????
|
||
|
||
?????????????????????
|
||
|
||
??????????‰^
|
||
|
||
## 10. Form Template Method????????Žï????
|
||
|
||
?????§»??????????????§»???????????????????????????????????????????????????
|
||
|
||
????§»??????????????????§µ??????????????????????????????????????????????????????????????(??Ùã????)
|
||
|
||
## 11. Replace Inheritance with Delegation ????????????§µ?
|
||
|
||
??????????¨®??????§Ö?????????????????????§Ø??????????
|
||
|
||
???????????????????????›‰???????????????????????§Ô??????????????????§Û????
|
||
|
||
## 12. Replace Delegation with Inheritance ???????????§µ?
|
||
|
||
??????????????????§Û???????????????????§Õ????????§Ü?????
|
||
|
||
???????????????
|