控制圈復雜度:9種重構技術_第1頁
控制圈復雜度:9種重構技術_第2頁
控制圈復雜度:9種重構技術_第3頁
控制圈復雜度:9種重構技術_第4頁
控制圈復雜度:9種重構技術_第5頁
已閱讀5頁,還剩8頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、控制圈復雜度的9種重構技術-Refactoring: Improving the Design of Existing Code控制圈復雜度:重構可以直接降低圈復雜度的9種重構技術(針對結構化編程):Composing Methods(重新組織你的函數)Extract Method(提煉函數)Substitute Algorithm(替換你的算法)Simplifying Conditional Expressions(簡化條件表達式)Decompose Conditional(分解條件式)Consolidate Conditional Expression(合并條件式)Consolidate

2、Duplicate Conditional Fragments(合并重復的條件片斷)Remove Control Flag(移除控制標記)Making Method Calls Simpler(簡化函數調用)Separate Query from Modifier(將查詢函數和修改函數分離)Parameterize Method(令函數攜帶參數)Replace Parameter with Explicit Methods(以明確函數取代參數)- Refactoring: Improving the Design of Existing Code針對面向對象編程:Replace Conditi

3、onal with Polymorphism(以多態取代條件式)Extract Method(提煉函數)void printOwing(double previousAmount) Enumeration e = _orders.elements(); double outstanding = previousAmount * 1.2; / print banner System.out.println (*); System.out.println (* Customer Owes *); System.out.println (*); / calculate outstanding whi

4、le (e.hasMoreElements() Order each = (Order) e.nextElement(); outstanding += each.getAmount(); /print details System.out.println (name: + _name); System.out.println (amount + outstanding);void printOwing(double previousAmount) printBanner(); double outstanding = getOutstanding(previousAmount * 1.2);

5、 printDetails(outstanding);void printBanner() / print banner System.out.println (*); System.out.println (* Customer Owes *);double getOutstanding(double initialValue) double result = initialValue; Enumeration e = _orders.elements(); while (e.hasMoreElements() Order each = (Order) e.nextElement(); re

6、sult += each.getAmount(); return result;void printDetails (double outstanding) System.out.println (name: + _name); System.out.println (amount + outstanding);將這段代碼放進一個獨立函數中,并讓函數名稱解釋該函數的用途你有一段代碼可以被組織在一起并獨立出來Substitute Algorithm(替換你的算法)String foundPerson(String people) for (int i = 0; i people.length;

7、i+) if (peoplei.equals (Don) return Don; if (peoplei.equals (John) return John; if (peoplei.equals (Kent) return Kent; return ;String foundPerson(String people) List candidates = Arrays.asList(new StringDon, John,Kent); for (int i=0; ipeople.length; i+) if (candidates.contains(peoplei) return people

8、i; return ;你想要把某個算法替換為另一個更清晰的算法將函數本體替換為另一個算法Decompose Conditional(分解條件式)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 (

9、quantity);你有一個復雜的條件語句從if、then、else三個段落中分別提煉出獨立函數Consolidate Conditional Expression(合并條件式)double disabilityAmount() if (_seniority 12) return 0; if (_isPartTime) return 0; / compute the disability amountdouble disabilityAmount() if (isNotEligableForDisability() return 0; / compute the disability amou

10、nt將這些判斷合并為一個條件式,并將這個條件式提煉成為一個獨立函數你有一系列條件判斷,都得到相同結果Consolidate Duplicate Conditional Fragments(合并重復的條件片斷)if (isSpecialDeal() total = price * 0.95; send(); else total = price * 0.98; send(); if (isSpecialDeal() total = price * 0.95;else total = price * 0.98;send();在條件式的每個分支上有著相同的一段代碼。將這段重復代碼搬移到條件式之外Re

11、move Control Flag(移除控制標記)void checkSecurity(String people) boolean found = false; for (int i = 0; i people.length; i+) if (! found) if (peoplei.equals (Don) sendAlert(); found = true; if (peoplei.equals (John) sendAlert(); found = true; void checkSecurity(String people) for (int i = 0; i 100) result

12、 += (Math.min (lastUsage(),200) - 100) * 0.05; ; if (lastUsage() 200) result += (lastUsage() - 200) * 0.07; ; return new Dollars (result);Dollars baseCharge() double result = usageInRange(0, 100) * 0.03; result += usageInRange (100,200) * 0.05; result += usageInRange (200, Integer.MAX_VALUE) * 0.07;

13、 return new Dollars (result);int usageInRange(int start, int end) if (lastUsage() start) return Math.min(lastUsage(),end) -start; else return 0;若干函數做了類似的工作,但在函數本體中卻包含了不同的值建立單一函數,以參數表達那些不同的值Replace Parameter with Explicit Methods(以明確函數取代參數)void setValue (String name, int value) if (name.equals(height

14、) _height = value; if (name.equals(width) _width = value; Assert.shouldNeverReachHere();void setHeight(int arg) _height = arg;void setWidth (int arg) _width = arg;函數實現完全取決于參數值而采取不同反應針對該參數的每一個可能值,建立一個獨立函數Replace Conditional with Polymorphism(以多態取代條件式)double getSpeed() switch (_type) case EUROPEAN: return getBaseSpeed(); case AFRICAN: return getBaseSpeed() - getLoadF

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論