<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    重構(gòu)

    一.什么是重構(gòu)?

    A series of small steps, each of which changes the program’s internal structure without changing its external behavior。

    重構(gòu)是一系列的小部驟,每一步只修改程序的內(nèi)部結(jié)構(gòu),但是不改變它的外部行為.

    二.為什么要重構(gòu)

    • To improve the software design
    • 為了提高軟件設計

    -Combat's  "it rot"

    抵抗代碼的腐化

            -Makes the program easier to change

    使程序更容易修改

    • To make the software easier to understand
    • 使軟件更容易理解

    -Write for people, not the compiler

    程序是寫給人看的,不是給編譯器看的.

    -Understand unfamiliar code

    理解不熟悉的代碼

    • To help find bugs
    • 幫助找到bug

    -Refactor while debugging to clarify the code

    在為了理清代碼而DEBUG時,進行重構(gòu).

    三.我們應該在什么時候重構(gòu)?

    • To add new functionality
    • 添加新的功能時

    -Refactor existing code until you understand it

    重構(gòu)現(xiàn)有的代碼直到你理解它們

    -Refactor the design to make it easy to add

    重構(gòu)設計使它容易添加新的功能

    • To find bugs
    • 在發(fā)現(xiàn)bug時

    -Refactor to understand the code

    重構(gòu)直到你理解代碼

    • For code reviews
    • 為了代碼評審

    -Rmmediate effect of code review

    -Allows for higher level suggestions

    Don’t set aside time for refactoring,include it in your normal activities

    在日常活動中進行重構(gòu),而不是另外找時間去重構(gòu).

    四.最后的思想

    • The one benefit of objects is that they make it easier to change.

    • 有一個好處就是使得對象更容易修改.

    • Refactoring allows you to improve the design after the code is written

    • 重構(gòu)允許你在代碼已經(jīng)寫完后改進自己的設計.

    • Up front design is still important,but not so critical

    • 事先的設計仍然是很重要的,但是并不那么關(guān)鍵了.

    五.例子中用到的的重構(gòu)條目:

    • A.抽取臨時變量:

    • B.使用查詢方法代替臨時變量:

    Find temp with a single assignment

    Extract Right Hand Side of assignment

    Replace all references of temp with new method

    Remove declaration and assignment of temp

    Compile and test

    • C.抽取方法:

    Create method named after intention of code

    Copy extracted code

    Look for local variables and parameters

    Turn into parameter

    Turn into return value

    Declare within method

    Compile

    Replace code fragment with call to new method

    Compile and test

    • D.移動方法:

    Declare method in target class

    Copy and fit code

    Set up a reference from the source object to the target

    Turn the original method into a delegating method

    -amountOf(Rental each) {return each.charge()}

    -Check for overriding methods

    Compile and test

    Find all users of the method

        -Adjust them to call method on target

    Remove original method

    Compile and test

    • E.使用狀態(tài)模式/策略模式代替type code,也就是去掉switch:

    Create a new state class for the type code

    Add subclasses of the state object, one for each type code

    Create an abstract query in the superclass to return the type code. Override in subclasses to return correct type code

    Compile

    Create field in old class for the state object

    Change the type code query to delegate to the state object

    Change the type code setting methods to assign an instance of the subclass

    Compile and test

    • F.使用多態(tài)代替switch語句

    Move switch to superclass of inheritance structure

    Copy one leg of case statement into subclass

    Compile and test

    Repeat for all other legs

    Replace case statement with abstract method

    • G.形成模版方法

    Take two methods with similar overall structure but varying pieces

    Use subclasses of current class, or create a strategy and move the methods to the strategy

    At each point of variation extract methods from each source with the the same signature but different body

    Declare signature of extracted method in superclass and place varying bodies in subclasses

    When all points of variation have been removed,move one source method to superclass and remove the other

         六.例子中提到的模式:

    策略模式

    模版方法模式

    狀態(tài)模式

    posted on 2008-12-17 11:53 JavaBegger 閱讀(78) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    <2008年12月>
    30123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    導航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人成网站在线播放vr| 国产中文在线亚洲精品官网| 巨波霸乳在线永久免费视频 | 国产成人亚洲精品青草天美| 老司机福利在线免费观看| 日韩免费观看视频| 亚洲精品久久无码av片俺去也| 无码毛片一区二区三区视频免费播放 | 亚洲av无码成h人动漫无遮挡| 成在线人免费无码高潮喷水| 久久精品国产亚洲Aⅴ蜜臀色欲| 一级一黄在线观看视频免费| 亚洲精品高清一二区久久| 久久久亚洲欧洲日产国码是AV| 亚洲狠狠婷婷综合久久| 中文字幕无码不卡免费视频| 四虎亚洲精品高清在线观看| 在线免费一区二区| 西西人体大胆免费视频| 久久久久久亚洲精品不卡| 99re8这里有精品热视频免费| 亚洲AV永久无码精品水牛影视| 91精品国产免费| 亚洲精品国产精品国自产网站| 成年午夜视频免费观看视频| 色欲色欲天天天www亚洲伊| 免费jlzzjlzz在线播放视频| 91成人免费福利网站在线| 91在线精品亚洲一区二区| 两个人的视频高清在线观看免费| 亚洲色欲色欲www在线播放 | 亚洲大片免费观看| 成年性羞羞视频免费观看无限| 免费一级毛suv好看的国产网站| 亚洲另类激情综合偷自拍图 | 日韩在线免费视频| 国产亚洲精品精品精品| 毛片免费观看的视频在线| 免费大片av手机看片| 亚洲影院在线观看| 国产一区在线观看免费|