More Related Content What's hot (17)
PDF
Devon 2011-b-5 ???? ??? ?? ???
Daum DNA ?
PDF
Legacy code refactoring video rental system
Jaehoon Oh ?
PPTX
??? ?? ?? By googletest 1? ?? ??? ???? money ??
Mickey SJ Lee ?
PDF
Java performance and trouble shooting
Anna Choi ?
Viewers also liked (8)
PPTX
Refactoring tutorial 1??[refactoring ??]
bbongcsu ?
Similar to TDD&Refactoring Day 01: Refactoring (20) PPTX
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim ?
PPTX
Refactoring Tutorial 1??[ Refactoring ??]
Bingu Shim ?
PPTX
[NDC12] ??? ??? ???? ? ??? ???? ????? ????? ??? - ???
ChangKyu Song ?
PPTX
????? ??? Agile skill set
?? ? ?
PPTX
??? - ??? ???? ?? ??
?? ? ?
PDF
?????????? ????? ?????: ?? ?? ?? ??
Jay Park ?
PPTX
Code complete chapter 19, 20 organize
hanstar17 ?
PPTX
???????? ???? (?? ??, Clean Code)
Jay Park ?
TDD&Refactoring Day 01: Refactoring1. Day01
? ?? ?? ??? ??
????
1
2. ????
??? ?????? ??? ????? ?? ???? ??? ? ??.
??? ??? ???? ??? ??? ??? ???? ??? ??? ?
?? ? ??.
??? ???? ??? ??? ??? ???? ?? ??? ??? ? ?
?.
2
?????
5. ??? Abstraction ???? ??
??? ??? ??? ???
???? ?? ?? ????
???? ????
???? ?? ??? ??
5
6. ?? Inheritance ???? ??
?? ???? ?? ?? ??
???? ???? ????
???? ??? ???? ?
? ???? ??
6
8. ??? Polymorphism ???? ??
polymorphism ??? ???? ?? ?? ?
¶àÐÎÐÔ ?? ?? ? ?? ?. ?? ?
?? ?? ??? ??? ??
??
TYPE
8
10. 2. ???????
1. ???? ? ? ??
2. ???????
3. ??? ??
4. ????? ??
5. ?? ?? ?? ??
10
????
12. ???? In IDEAL Project in the world
? ???? ???? ?? ??? ?? ??
? ???? ??? ??
? ???? ??? ???? ??? ???
? ??? ??? ???? ? ?? ??? ???
? ??? ??? ???? ?? ?? ?? ??
12
13. ???? In REAL Project in the world
? ???? ???? ??
¨C ???? ?? ??? ??? Skill? ??? ?? ??.
¨C ??? ??? ?? ????.
¨C ?? ??? ???? ???? ?? ??? ???,
¨C ??? ??? ??? ??? ??.
¨C ???? ???? ?? ???? ????
¨C ?? ??? ??? ???. ????? ? ???.
13
15. ???? In REAL Project in the world
? ??? ???? ??
¨C ?? ??? ?? ??? ??(??,??? ???? ??)
¨C ??? ?? ??? ?? ??? ??? ?? ?? ?? ??
¨C ???? ?? ?? ??? ?? ??
¨C Copy & Paste ? ?? ?? ??
¨C ??? ???? ??? ???? (??? ??)
15
16. Normal Dejavu Project in the world
? (???) ????? ?? ???? ?? ??
? ?? ????? ?? ??, ??, ??
? ?? ??? ?? ??? ????, ?? ??? ???? ??
? ??? ???? ? ??? ??? ??? ?? (Iterative)
? ?? ??
? ??? ?? ??, ??? ?? ??? ???
? ?? ?? ?? ??? ?? ? ???? ??? ??
? ??? ??? ?? ?? ????, ?? ??? ???? ??? ??
? (???) ????? ?? ???? ?? ??
? and Looping¡
16
19. ??????? ???????
? ????
¨C ??? ??? ?? ??? ??? ?? ????
¨C ???? ??,
¨C ???? ???
¨C ?????? ???? ??? ???? ?
19
20. Example #1 ???????
void f() {
void f() { ¡.
¡. computeScore();
// Compute score }
score = a * b + c;
score -= discount; void computeScore() {
} score = a * b + c;
score -= discount;
}
20
21. Example #2 ???????
public String name;
private String name;
public String getName(){
return name;
}
public String setName (String newName) {
name = newName;
}
21
22. Example #3 ???????
if (user == null)
plan = Plan.basic();
else
plan = user.getPlan();
plan = user.getPlan();
<<interface>>
User
+getPlan()
NullUser
+getPlan() return Plan.basic();
22
23. ? ????? ???? ???????
? ??? ?? ?? ??
? ??? ???? ?? ??? ??
? ??? ???? ??
? ? ? ??? ????? ? ? ???
???? ????? ?? ??? ?? ? ? ????
23
24. ???? ???????
? ????(Smells)
? ????(Catalogue)
? ???? ?? ???(Automated Unit Test)
24
25. ??? ???????
? ??? ???
? ????? ??
? ???? ????
25
26. ?? ????? ???? ???????
? The Rule of Three ¨C DRY(Don¡¯t Repeat Yourself)??
? ??? ??? ?
? ??? ?? ? ?
? ?? ??? ? ?
26
27. ??? ?? ???????
? ?????? ? ???? ?? ??? ?? ??? ???, ??
?? ????? ? ??? ????? ?? ??? ? ? ??.
? ? ??? ??? ????? ?? ??
¨C ??? ?? ??? ?? ???? ???? ???? ?? ????.
¨C ??? ??? ????.
27
28. ??? ????? 3? ?? ???????
? ??? ??? ??? ???
?? ????.
? ??? ???? ??? ??
??.
? ???? ?? ??? ???
? ??? ???.
28
29. ???? ?? ???????
? ????? ???? ??
29
31. ?? ??? Automated Unit Test ??? ??
? ¡°???? ??? ?? ??? ??? ??? ???? ??? ?
? ???.¡± ¨C ?? ???
? ?? ???? ???? ?? ??? ?????
= JUnit
31
32. ??? ?? ?? Test-Driven Development ??? ??
? ???? ?? ??? ???? ??? ?? ?? ??? ??
? ¡°????? ???? ?? ???? ??? ??? ?? ????.¡±
? ??? ??? ??? ??? ??? ?? ??
32
33. TDD ?? ??? ??
?? Ask
???? ?????? ???? ????. (FAIL)
?? Respond
???? ???? ??? ???? ??? ????. (PASS)
?? Refine
????? ????, ???? ?? ????, ??? ??
??? ?? ??? ????. (REFACTORING)
?? Repeat
?? ??? ?? ??? ?? ????.
33
35. ????? ?? ????? ??
? ?? ?? = ?? ?? + ?? ??
? ?? ?? = ?? ?? + ?? ?? + ??? ??
+ ?? ??
35
36. ?? ?? Upfront Design ????? ??
? ?? ??
¨C ? ?? ??? ??? ??? SW??? ?? ??? ??? ?? ??
¨C ???? ??? ??? ??? ??? ?? ????.
¨C ? ? ??? ??? ??? ????.
? ??? ??? ???? ??? ?? ???
¨C ??? ???? ???? ? ?? ??? ???? ????.
¨C ? ? ??? ??? ?? ??, ????? ?????? ? ?????.
¨C ??? ??? ??? ????? ?? ???? ?? ?????.
36
37. ????? ?? ????? ??
? ????? ?? ??? ????(upfront design)? ????
? ????? ????? ???? ????.
¨C ????? ?? ??? ???? ???. ??? ???? ?? ?? ?
?? ????? ??(reasonable)? ???? ???.
¨C ??? ???? ??? ???? ??? ?? ? ? ??? ??.
¨C ??? ???? ? ? ??? ??? ????? ??? ?? ?? ??
?, ? ??? ?? ??? ????.
37
39. ?? Intuition Smells
1997? 5? 11?
??? ???? ????? IBM? ??? ?? ??? ? ??(Deep
blue)? ?? ?? ??? ¡®?? ?????(Kasparov)¡¯?? 6 ??
??? ??? ???? ???.
¡º? ??¡»? 2m? ? ??? ?? 1.4 ??? ??? ????
??? ??? ??? ??? ??? ?? ?? 6?? IBM? ??
????? ?? 8? ?? 32?? ???? ????? 512?? ??
?? ?? ???? ??? 2?? ?? ??? ?? ?? ?? ????.
¡º?¡»? ??? ¡º??? ???¡»? ??? ?? ??? ??? ???
???? 1?? 2??? ??(ÐÐñR)? ????? ??? ? ? ??
??? ???, ?? ? ? ??? ????? ?? ?? ??? ???
?? ???? ??. ??? ? ?? ?? ???? ??? ???? ??
?? ??? 2?? ????? ??? ?? ??? ?? ???? ??
?? ?? ???? ??? ??? ?????. ?? ? ?? ??
??? ???? ???? ??? ?? ??? ?? ???? ??? ?
?? ?? ????? ???? ?????? ??? ???? ??.
???¡º?¡»? ???? ??? ???? ¡º???? ??? ???
?? ???¡»?? ? ? ??
?? 5???? ? ? ????? ??? 1? 3? 1?? ???.
- New York Times 1997.5
39
40. ?? ?? ?? ?? #1 Smells
¡°?? ??? ???, ???!¡± ¨C ???
¨C ????(Duplicate code)
¨C ? ???(Long Method)
¨C ??? ???(Large class)
¨C ? ???? ???(Long Parameter List)
¨C ??? ??(Divergent Change)
¨C ??? ??(Shotgun Surgery)
¨C ?? ??(Feature Envy)
¨C ??? ???(Data Clumps)
¨C ???? ?? ??(Primitive Obsession)
¨C ??? ??(Switch Statements)
¨C ?? ?? ??(Parallel Inheritance Hierarchies)
40
41. ?? ?? ?? ?? #2 Smells
¡°???? ??? ? ?? ??? ??? ??? ?
??. ??? ??? ? ?? ??? ????¡±
¨C ??? ???(Lazy Class) ¨C ?? ???
¨C ??? ???(Speculative Generality)
¨C ?? ??(Temporary Field)
¨C ??? ??(Message Chains)
¨C ???(Middle Man)
¨C ???? ????(Inappropriate Intimacy)
¨C ?????? ?? ?? ???(Alternative Classes with Different Interfaces)
¨C ???? ????? ???(Incomplete Library Class)
¨C ??? ???(Data Class)
¨C ??? ??(Refused Bequest)
¨C ??(Comments)
41
42. ? ??? Long Method Smells
? ???? ??? ?? ????? ?? ?? ???.
¨C ???? ??? ?? ??? ???? ? ??? ??.
¨C ??, ?? ???? ????
????? ??(Semantic Distance)? ??
? ??? 99%? ???? ??
¨C Extract Method
? ???? ????
¨C Decompose Conditional
42
43. ? ??? Long Method Smells
? Decompose Conditional? ??? ?? ?
43
44. ?? ?? Naming Smells
? addCourse( course );
? getNodesArrayList();
? makeIt();
? getData();
? sort();
? processItem();
??? ?? ???? ??? ? ?? ??? ??.
W44()
44
45. ?? ?? Naming Smells
? ???? ???? ?? ??? ???? ? ??.
maskClear2( plMoney )
???? ???? ??? ??? ???? ???? ??
System.out.println(LMoney.toCommaFormat(120000));
System.out.println(LMoney.toCommaFormat(120000, true)); // () ?? ??? ???? ??? ??
System.out.println(LMoney.toCommaFormat(070712093801.9283));
? ??(argument)? ??? ?? ?? ?? ??? ??? ?? ??
Money(String a) {
Money(String a, false); }
? ???? ?? ?? ??
/**
* basic ?? ????? ?? ?? Array
*/
private static final String basicA[] = { "", "?", "?", "?" };
/**
* base ?? ????? ?? ?? Array
*/
private static final String baseA[] = { "", "?", "?", "?", "?" };
45
46. ?? Comments Smells
? ?? ??? ???? ?? ??? ??????
¨C Extract Method
? ??? ????? ??? ??? ??? ??????
¨C Rename Method
? ???? ??? ???? ?? ??? ???? ?? ??? ?
?? ??? ?????
¨C Introduce Assertion
46
47. ?? Comments Smells
? ??? ????
¨C ??? ?? ???. ? Sweet Smells
¨C But, ??(what)? ??? ????? ???? ???.
¨C ??(why)? ???? ??? ???? ??.
¨C ??? ??? ?? ??? ???? ???? ???? ??? ??.
? ?? ??? ???? ?? ???? ??????
¨C Extract Method
47
48. ?? Comments Smells
? ?? ???
¨C ??? ?? ?? ??? ???? ?? (???)
¨C ??? ?? : ????? ??? ??? ?? ??? ???? ??
(???, ??? ??? ??)
¨C ???? : Todo, Release ?? ? ?? ?? ??? ???? ??
(???, ??? ?? ??? ??)
¨C ??? ?? : ? ?? ?? ??? ?? ?? ???? ??.
??? ??? ??? ? ??? ??? ?(??)
¨C ??? ?? ?? (??)
¨C ?????? ??? ? ?? ??: ????? ????, ??? ?? ?? ??(??)
- from code complete
48
49. ?? ¨C Example #1 Smells
/**
*
* laf.xml ??? ??? ?? ???? ??? ????.
*
* 1) test-mode
* ???? ??? ?? ???? ? ?? true? ????, ? ?? test-receivers ??? ????.
* ????? test-mode? false? ????.
*
* 2) spec
* spec ? ?? ??? ??? mailer server??? ??? ?? ?? ????.
* spec ? name? ???? ?? ?? default ?? ????,
* default ? ??? ? ? ??? ???? ???? ?? ???? spec ? name? example ? ?? ????.
* ??? ? ??, default?? ??? ???? ?? ??? ?? overwrite ??.
* ?, ???? ??? default? ?? ????? ? ??? spec? name? ???? ??? ? ?? ??? ??.
* spec ? LMail ??? ???? ????, ??? ?? ?? default spec? ????.
*
* 3) body-template
* html ??? ??? ???? ? ?? ????.
* html string ? ?? ???? send? ?? ???, ???? html ?? ??? ????? ? ??? ????.
* ?? ?? template html ??? ????? ????? html??? ????.
* mail-template ? spec? ????? LMail ??? ??? ????.
* ??? ?? ?? default mail-template? ????, sub-directory??? ????.
*
49
50. ?? ¨C Example #2 Smells
public static void writeMail(String[] args) {
// ??? ??? ?? ???? LDATA? ???? ??
LData ld = new LData("userInfo");
// ???? ??? ????.
// ? ???? userName ??? ???? ???? ?
ld.setString("userName", "Jubong, Park");
// ??? ??? ???. ??? ??? LData?? name ?? ????.
LMultiData lm = new LMultiData("friends");
lm.add("name", "Hyori, Kim");
lm.add("name", "Yeesul, Lee");
lm.add("name", "Dongwook, Park");
// [ ] ? ???? ?????, ??? ??? ???? ???? ??? ????? ??
StringBuffer imsi = new StringBuffer();
for(int i=0; i<lm.keySize("name"); i++) {
imsi.append("[");
imsi.append(lm.getString("name",i));
imsi.append("] ");
}
ld.setString("friends", imsi.toString());
50
51. ?? ¨C Example #2 Smells
public static void writeMail(String[] args) {
Mail mail = new Mail();
mail.setSender("Jubong, Park");
mail.addReceiver("Hyori, Kim");
mail.addReceiver("Yeesul, Lee");
mail.addReceiver("Dongwook, Park");
¡
¡
51
52. ?? ¨C Example #3 Smells
public static final int LowHandleLength = 400;
// ???? ?? MaxTilt? ??????? ???? ????.
public static final int MaxTilt = 200;
52
53. ??? ?? ?? Feature Envy Smells
? ??? ??? ?? ????? ?? ???? ?? ??? ??
???
¨C Move Method
? ?? ???? ???? ??? ???? ?? ???
¨C Extract Method ? Move Method
? ?? ???? ?? ??? ? ??? ???.
53
54. ? ???? ??? Long Parameter List Smells
? ????? ??? ??? ??
¨C ????? ?? ??. ? ??? ??? ?? ????? ???.
? ???? ???? ?????
¨C ???? ???.
¨C ?????? ????? ???? ????.
54
55. ? ???? ??? Long Parameter List Smells
? OOP? ???
? ??(Object)? ?? ??? ???? ??? ? ??.
? ???? ???? ? ? ? ??!
¨C Replace Parameter with Method
¨C Preserve Whole Object
¨C Introduce Parameter Object
55
56. ??? ?? Divergent Change Smells
? ? ???? ?? ??? ?? ?? ???? ??
¨C ?????? ???? ??? ??. ??? ??? ??? ???? ??
? ? ?? ????? ???? ??. ???, ??? ??? ?? ? ?
?? ??? ?? ?? ???? ????
? ?? ??? ?? ???? ?? ???? ??? ??
¨C Extract Method
56
57. ?? ?? Temporary Fields Smells
? ?? ??? ?? ?? ??? ?? ????? ? ? ??? ?
??? ??? ???.
¨C Replace Temp with Query
¨C Replace Parameter with Method
? ?? ?? ??? ??????
¨C Extract Class
57
58. ?? ?? Duplicate code Smells
? ? ?? ??? ??? ?? = ?? ??!!
¨C ?? ??? ??? ???!
? ??? ??? ?? ??? ??
¨C ?? ??? ??? ? Extract Method
¨C ?? ???? ????? ? Extract Method and Pull Up Field
¨C ?? ???? ??? ????? Extract Class
58
59. ??? ?? Switch statements Smells
? ? ??? ???? ????? ??? ?? ??!
¨C ???(switch) ??? ????? ??!
? ??? ??? ? ? ?? ??? ???? ???? ?? ??
?? ??? ??? ??? ???? ??.
¨C Extract Method and Move Method
¨C Replace Type Code with Subclasses ?? Replace Type Code with
State/Strategy
¨C Replace Conditional with Polymorphism
¨C Replace Parameter with Explicit Methods
¨C Introduce Null Object
59
60. ??? ?? Shotgun surgery Smells
? ??? ?? ?? ?? ?? ???? ??? ?? ??
¨C ???? ??? ??? ???? ?? ??? ??.
? ?????? ??/??? ???(one-to-one) ????? ?
??? ??.
¨C Move Method and Move Field
¨C Inline Class
60
61. ??? ??? Speculative Generality Smells
? ¡°??! ????? ??? ??? ??? ?! ?? ??? ???
?!¡±
¨C ???, ??? ? ? ???? ??? ???? ?? ??? ???.
? ??? ??? ???? ??! ??? ???? ??!
¨C ? ???? ?????? ? Collapse Hierarchy
¨C ???? ?? ??? ?????? ? Inline Class
¨C ?? ? ?? ??????? ? Remove Parameter
61
62. ???? ?? ?? Primitive Obsession Smells
? ?? ?? ???????? ???? ???? ?? ?? ??
?? ??.
? ?????? ??? ????!
¨C ??, ??, ????, ????, ????, ??, ?? ?? ?? ?? ?
?? ? ? ??.
¨C Replace Data Value with Object
¨C Replace Array with Object
62
63. 3. ???? ????
1. Extract Method 13. Replace Type Code with Subclasses
2. Rename Method 14. Replace Type Code with
State/Strategy
3. Introduce Assertion
15. Replace Conditional with
4. Move Method
Polymorphism
5. Decompose Conditional
16. Introduce Null Object
6. Move Field
17. Preserve Whole Object
7. Extract Class
18. Collapse Hierarchy
8. Inline Class
19. Remove Parameter
9. Replace Temp with Query
20. Introduce Parameter Object
10. Pull Up Field ? Pull Down Field
21. Replace Data Value with Object
11. Replace Parameter with Method
22. Replace Array with Object
12. Replace Type Code with Class
63
????
64. ???? ???? #1 ???? ????
¨C Extract Method http://www.refactoring.com/catalog/
¨C Decompose Conditional
¨C Rename Method
¨C Introduce Assertion
¨C Move Method
¨C Move Field
¨C Extract Class
¨C Inline Class
¨C Replace Temp with Query
¨C Pull Up Field
¨C Replace Parameter with Method
64
65. ???? ???? #2 ???? ????
¨C Replace Type Code with Class
¨C Replace Type Code with Subclasses
¨C Replace Type Code with State/Strategy
¨C Replace Conditional with Polymorphism
¨C Introduce Null Object
¨C Preserve Whole Object
¨C Collapse Hierarchy
¨C Remove Parameter
¨C Introduce Parameter Object
¨C Replace Data Value with Object
¨C Replace Array with Object
65
66. Extract Method ???? ????
?? ??? ???? ??? ?? ?
- ???? ???, ??? ??? ? ????, ???, ???? ????.
- ??? ? ??? ??, ??? ??? ??? ¡°???? ??(Semantic distance)¡±??.
66
67. Rename Method ???? ????
????? ??? ?? ??? ? ?? ?
Customer Customer
getinvcdtlmt() getInvoiceableCreditLimit()
- ???? ?? ??? ? ??? ?? ??? ??? ??.
- ?? ???? ??? ??? ????? ?? ????, ???? ????? ?? ???.
- ???? ??? ?????? ?? ???? ???? ??? ? ?? ?? ???? ????!
67
68. Introduce Assertion ???? ????
??? ??? ????? ?? ??? ???? ?? ?
double getExpenseLimit() {
// should have either expense limit or a primary project
return (_expenseLimit != NULL_EXPENSE) ?
_expenseLimit:
_primaryProject.getMemberExpenseLimit();
}
double getExpenseLimit() {
Assert.isTrue (_expenseLimit != NULL_EXPENSE || _primaryProject != null);
return (_expenseLimit != NULL_EXPENSE) ?
_expenseLimit:
_primaryProject.getMemberExpenseLimit();
}
- ????? ???? ??? ????? ?? ?? ??.
68
69. Move Method ???? ????
???? ??? ??? ????? ?? ???? ??? ? ?? ??? ?
- ???? ?? ?? ??? ??? ???, ?? ???? ???? ??? ??? ?? ??? ??? ???? ?
? ? ??? ??? ???.
- ?? ???? ??? ??? ?? ??? ??? ?? ????? ?? ????.
69
70. Decompose Conditional ???? ????
if-then-else?? ???? ???? ??? ?
if ( date.before (SUMMER_START) || date.after(SUMMER_END) )
charge = quantity * _winterRate + _winterServiceCharge;
else charge = quantity * _summerRate;
if ( notSummer(date) )
charge = winterCharge(quantity);
else charge = summerCharge (quantity);
70
71. Move Field ???? ????
??? ??? ??? ????? ?? ???? ?? ? ?? ?? ? ?
71
72. Extract Class ???? ????
? ?? ???? ?? ? ?? ??? ???? ?? ?? ??
- ????? ???? ??? ???? ??? ??, ??? ?? ?? ???? ?????.
- ?? ??, ?? ? ???, ???!
72
73. Inline Class ???? ????
???? ?? ?? ?? ?? ?
- ?? ???? ? ? ???? ??? ? ?? ????, ?? ?? ???? ?? ?? ?? ??? ? ?? ??.
73
74. Replace Temp with Query ???? ????
?? ??? ???? ???? ??? ????? ???? ?? ??
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98; if (basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
...
double basePrice() {
return _quantity * _itemPrice;
}
- ?? ??? ??? ????, ?? ????? ??? ???? ??? ??.
- ????? ???? ???? ?? ??? ????.
74
75. Pull Up Field ? Pull Down Field ???? ????
? ?? ?? ???? ?? ??? ?? ?? ??
- ??? ????.
75
76. Replace Parameter with Method ???? ????
???? ?? ??? ?? ???? ????? ?? ?? ??
int basePrice = _quantity * _itemPrice;
discountLevel = getDiscountLevel();
double finalPrice = discountedPrice (basePrice, discountLevel);
int basePrice = _quantity * _itemPrice;
double finalPrice = discountedPrice (basePrice);
- ????? ???? ?????? ?? ? ? ?? ????? ?? ??? ??? ?? ? ??? ??.
76
77. Replace Type Code with Class ???? ????
?? ???? ??? ??? ??? ?? ?? ?? ????? ??? ??
- ?? ??? ???? ??? ??? ??? ??? ???? ??.
- ?? ??? ???? ??? ?? ??? ??? ????? ??? ??? ? ? ??? ???.
77
78. Replace Type Code with Subclasses ???? ????
???? ??? ??? ?? ?? ????(immutable) ????? ?? ??
- ?? ?? ??? ???? ??? ??? ? ??, ??? ??? ???? ???? ???.
- ??? ??? ?? ??? ????? ???? ?? ???? ???? ??? ?? ????.
78
79. Replace Type Code with State/Strategy ???? ????
??? ??? ?? ?? ??? ??? ?? ?? ??? ?????? ?? ?
?? ??
- ??? ???? ?? ?? ??? ?? ??? ???? ?? ? ??.
- ????? ?? ?? ?? ??? ?? ? ??.
79
80. Replace Conditional with
???? ????
Polymorphism #1
??? ??? ?? ?? ??? ???? ???? ??? ?? ??
double getSpeed() {
switch (_type) {
case EUROPEAN:
return getBaseSpeed();
case AFRICAN:
return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
case NORWEIGIAN_BLUE:
return (_isNailed) ? 0 : getBaseSpeed(_voltage);
}
throw new RuntimeException ("Should be unreachable");
}
- ???? ? ??? ?????? ?? ????? ???? ??? ?? ???? abstract? ???.
80
82. Introduce Null Object ???? ????
???? ?(null) ??? ????? ?? ??
if (customer == null)
plan = BillingPlan.basic();
else
plan = customer.getPlan();
- null ?? null ??? ????.
- ???? ???, ??? ??? ?? ? ?? ??? ?? ?? ??? ???? ??? ?? ? ??? ????
???. ??? ? ??? ??? ??? ??? ????.
82
83. Preserve Whole Object ???? ????
?? ???? ?? ?? ?? ?? ?? ??? ?? ? ????? ?? ??
int low = daysTempRange().getLow();
int high = daysTempRange().getHigh();
withinPlan = plan.withinRange(low, high);
withinPlan = plan.withinRange(daysTempRange());
- ???? ??? ?? ?? ???? ??, ?? ????? ? ??? ?? ??? ????. ?? ???? ???
??? ???? ?? ???? ???? ??? ???? ??.
83
84. Collapse Hierarchy ???? ????
?? ???? ?? ???? ?? ??? ?? ??
- ????? ???? ?? ??? ?? ????.
84
85. Remove Parameter ???? ????
????? ? ?? ??? ??? ??? ?? ??
- ?? ?? ????? ?? ??? ?? ???? ????.
- ?, ???? ???? ???? ??, ???? ??? ? ? ???? ??.
85
86. Introduce Parameter Object ???? ????
?? ?? ??? ?????? ??? ?? ??
- ???? ??? ?? ???? ????.
- ??, ??? ??? ?? ??? ??? ??? ?? ?????? ????? ???? ??.
86
87. Replace Data Value with Object ???? ????
???? ???? ??? ??? ??? ??? ?? ??
- ?? ??, ????? ?? ???? ???(String)?? ?? ? ? ???, ???? ???? ?? ????, ?
? ??? ???? ?? ?? ??? ??? ????? ??? ????.
87
88. Replace Array with Object ???? ????
??? ?? ??? ?? ?? ??? ?? ??
String[] row = new String[3];
row [0] = "Liverpool";
row [1] = "15";
Performance row = new Performance();
row.setName("Liverpool");
row.setWins("15");
88