<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### Replace Method with Method Object(以函數對象取代函數) 你有一個大型函數,其中對局部變量的使用,使你無法釆用 Extract Method。 將這個函數放進一個單獨對象中,如此一來局部變量就成了對象內的值域(field) 然后你可以在同一個對象中將這個大型函數分解為數個小型函數。 ~~~ class Order... double price() { double primaryBasePrice; double secondaryBasePrice; double tertiaryBasePrice; // long computation; ... } ~~~ ![](https://box.kancloud.cn/2016-08-15_57b1b56bc94e7.gif) **動機(Motivation)** 我在本書中不斷向讀者強調小型函數的優美動人。只要將相對獨立的代碼從大型函數中提煉出來,就可以大大提高代碼的可讀性。 但是,局部變量的存在會增加函數分解難度。如果一個函數之中局部變量泛濫成災, 那么想分解這個函數是非常困難的。Replace Temp with Query 可以助你減輕這一負擔,但有時候你會發現根本無法拆解一個需要拆解的函數。這種情況下,你應該把手深深地伸入你的工具箱(好酒沉甕底呢),祭出函數對象(method object )[Beck]這件法寶。 Replace Method with Method Object 會將所有局部變量都變成函數對象(method object)的值域(field)。然后你就可以對這個新對象使用 Extract Method 創造出新函數,從而將原本的大型函數拆解變短。 **作法(Mechanics)** 我厚著臉皮從Kent Beck [Beck]那里偷來了下列作法: - 建立一個新class,根據「待被處理之函數」的用途,為這個class命名。 - 在新class中建立一個final值域,用以保存原先大型函數所駐對象。我們將這個值域稱為「源對象」。同時,針對原(舊)函數的每個臨時變量和每個參 數,在新中建立一個個對應的值域保存之。 - 在新class中建立一個構造函數(constructor),接收源對象及原函數的所有參數作為參數。 - 在新class中建立一個compute()函數。 - 將原(舊)函數的代碼拷貝到compute()函數中。如果需要調用源對象的任何函數,請以「源對象」值域調用。 - 編譯。 - 將舊函數的函數本體替換為這樣一條語句:「創建上述新的一個新對象, 而后調用其中的compute()函數」。 現在進行到很有趣的部分了。由于所有局部變量現在都成了值域,所以你可以任意分解這個大型函數,不必傳遞任何參數。 **范例(Example)** 如果要給這一重構手法找個合適例子,需要很長的篇幅。所以我以一個不需要長篇幅(那也就是說可能不十分完美)的例子展示這項重構。請不要問這個函數的邏輯是什么,這完全是我且戰且走的產品。 ~~~ Class Account int gamma (int inputVal, int quantity, int yearToDate) { int importantValue1 = (inputVal * quantity) + delta(); int importantValue2 = (inputVal * yearToDate) + 100; if ((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; } ~~~ 為了把這個函數變成一個函數對象(method object),我首先需要聲明一個新class。在此新class中我應該提供一個final值域用以保存原先對象(源對象);對于函數的每一個參數和每一個臨時變量,也以一個個值域逐一保存。 ~~~ class Gamma... private final Account _account; private int inputVal; private int quantity; private int yearToDate; private int importantValue1; private int importantValue2; private int importantValue3; ~~~ 按慣例,我通常會以下劃線作為值域名稱的前綴。但為了保持小步前進,我暫時先保留這些值域的原名。 接下來,加入一個構造函數: ~~~ Gamma (Account source, int inputValArg, int quantityArg, int yearToDateArg) { _account = source; inputVal = inputValArg; quantity = quantityArg; yearToDate = yearToDateArg; } ~~~ 現在可以把原本的函數搬到compute()了。函數中任何調用Account class的地方,我都必須改而使用_account值域: ~~~ int compute () { importantValue1 = (inputVal * quantity) + _account.delta(); importantValue2 = (inputVal * yearToDate) + 100; if ((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; } ~~~ 然后,我修改舊函數,讓它將它的工作轉發〔委托,delegate)給剛完成的這個函 數對象(method object): ~~~ int gamma (int inputVal, int quantity, int yearToDate) { return new Gamma(this, inputVal, quantity, yearToDate).compute(); } ~~~ 這就是本項重構的基本原則。它帶來的好處是:現在我可以輕松地對compute()函數采取 Extract Method,不必擔心引數(argument)傳遞。 ~~~ int compute () { importantValue1 = (inputVal * quantity) + _account.delta(); importantValue2 = (inputVal * yearToDate) + 100; importantThing(); int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; } void importantThing() { if ((yearToDate - importantValue1) > 100) importantValue2 -= 20; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看