* [????? ?????????](#?????-?????????) * [????? ??????](#?????-??????) * [?????? ????????](#??????-????????) * [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?????????](#5-divergent-change?????????) * [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???I????](#9-subsititute-algorithn???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-????????????) # ????? ????????? ????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????? ????????????????????????????????????????????? **????????** ?????????????????????Movie??Rental ?? Customer??Rental ????????? Movie ????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a758c8b2-0ac7-438f-90c2-3923ffad6328.png) ???????????????????????? Customer ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9e5e3cc6-3107-4051-b584-8ff077638fe6.png) ???????? Movie ????????????????????? Movie ?????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a502516-5d34-4eef-8f39-916298a60035.png) ???? Price ???????????????????????? Movie ???? Price ?????????????????????????????? Price ?????????? Movie ??????????????????????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c02a83b8-a6b9-4d00-a509-6f0516beaf5e.png) ????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/95f4559c-3d2a-4176-b365-4fbc46c76cf1.png) ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/293b9326-02fc-4ad8-8c79-b4a7b5ba60d3.png) # ????? ?????? ?????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????? bug??????????? ??????????????????????????????????????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????? Java ???? @deprecation ?????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? # ?????? ???????? ## 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????????? ????????????????? ??????????????? Extract Class ????????????????? ## 6. Shotgun Surgery?????????? ???????????????? ??? 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 ????????I????? ## 10. Switch Statements??switch ??????? ## 11. Parallel Inheritance Hierarchies??????????? ??????????????????????????????????????????????? ????????????????????????????????????????????????????????????????????????? ## 12. Lazy Class???????? ????????????????????????????????? ## 13. Speculative Generality????????????? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ## 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??????????????? ???????????????????????????????????????????????? ?????????????????I????????????????????? ```java double basePrice = anOrder.basePrice(); return basePrice > 1000; ``` ```java return anOrder.basePrice() > 1000; ``` ## 4. Replace Temp with Query?????????????????? ??????????????????????????????????????????????????????????????????????????????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???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?????????????????? ?????????????I???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ## 4. Change Reference to value?????????????????? ?? Change Value to Reference ????????????????????????????????????????????????????????????????????????I????????????????? ???????????? equals() ?? hashCode() ???? ## 5. Replace Array with Object????????????? ????????????????????????????? ??????I?????????????????????????????????????????????????????????? ## 6. Duplicate Observed Data???????????????????? ??????????????? GUI ??????????????????????????? ??????????????????????????????? Oberver ??????????????????? GUI ??????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e024bd7e-fb4e-4239-9451-9a6227f50b00.jpg) ## 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 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??????????????? ?????????????????????????????????????????????????I????????????????????????? switch ??????????? Replace Conditional with Polymorphism ??? switch????????????? Replace Type Code with Subcalss ?? Replace Type Code with State/Strategy ????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/27c2e0b3-8f95-453d-bedc-6398a8566ce9.jpg) ## 14. Replace Type Code with Subcalsses????????????????? ?????????????????????????????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c41d3977-e0e7-4ee4-93e1-d84f1ae3e20e.jpg) ## 15. Replace Type Code with State/Strategy ???? State/Strategy ????????? ??????????????????????????????????????????????? ?? Replace Type Code with Subcalsses ???????? Replace Type Code with State/Strategy ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/81fd1d6f-a3b2-4160-9a0a-1f7cb50ba440.jpg) ## 16. Replace Subclass with Fields?????????????? ????????????????????????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f2e0cee9-ecdc-4a96-853f-d9f6a1ad6ad1.jpg) # ????? ?????????? ## 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 ????????????????????????? ????????????????????????????????????????????????????????????????????????@????????????????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"); } ``` ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8432c8-2552-457f-b117-1da36c697221.jpg) ## 7. Introduce Null Object??????Null???? ?? null ??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?? ???????????????????? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/08738dd0-ae8e-404a-ba78-a6b1b7d225b3.jpg) ## 10. Remove Setting Method?????????????? ???????????????????????????????????? ????????????????????????????????? final?? ## 11. Hide Method??????????? ??????????????????????????????? ????????????? private?? ## 12. Replace Constructor with Factory Method ?????????????????????? ??????????????????????????????????? ?????????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????????????????? ????????????????????????????????????????? ????????????????????????????????????????? ```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 ???????????? ?????????????????????????????????????????????????? ???????????????