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

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

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

    ivaneeo's blog

    自由的力量,自由的生活。

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
    范例(Examples)
    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() {
           int importantValue1 = (inputVal * quantity) + _account.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):
    int gamma(int inputVal, int quantity, int yearToDate) {
        return new Gamma(this, inputVal, quantity, yearToDate).compute();
    }
    這就是本項重構的基本原則。它帶來的好處是:現在我可以輕松地對compute()函數采取Extract Method(110),不必擔心引數(argument)傳遞。
    int compute() {
        int importantValue1 = (inputVal * quantity) + _account.delta();
           int importantValue2 = (inputVal * yearToDate) + 100;
           importantThing();
           int importantValue3 = importantValue2 * 7;
           //   and so on.
           return importantValue3 -2 * importantValue1;
    }
    void importantThing() {
        if((yearToDate - importantValue1) > 100)
           importantValue2 -= 20;
    }
    posted on 2005-08-29 16:49 ivaneeo 閱讀(256) 評論(0)  編輯  收藏 所屬分類: refactoring-從地獄中重生
    主站蜘蛛池模板: 中文在线观看免费网站| 成人妇女免费播放久久久| 黄瓜视频高清在线看免费下载 | 一级做a爱过程免费视| 日本免费人成黄页在线观看视频| 亚洲国产乱码最新视频| 免费a级毛片高清视频不卡| 99亚偷拍自图区亚洲| 国产网站在线免费观看| 又硬又粗又长又爽免费看| 中文字幕在亚洲第一在线| 国内精品免费在线观看| 亚洲精品高清国产麻豆专区| 国产精品成人观看视频免费 | 无套内射无矿码免费看黄| 亚洲毛片网址在线观看中文字幕 | 3d动漫精品啪啪一区二区免费| 久久精品国产亚洲AV嫖农村妇女 | 亚洲免费视频播放| 国产午夜亚洲精品| 亚洲 国产 图片| 18禁超污无遮挡无码免费网站 | 成人午夜免费福利视频| 精品国产日韩久久亚洲| 免费精品国产自产拍观看| 中国国产高清免费av片| 亚洲国产高清美女在线观看| 日本免费网站观看| 免费毛片在线看不用播放器| 亚洲精品国产成人中文| 国产成人无码a区在线观看视频免费| 一级毛片aa高清免费观看| 久久精品国产亚洲av麻豆色欲| 啦啦啦在线免费视频| 香蕉免费在线视频| 精品亚洲456在线播放| 久久久久噜噜噜亚洲熟女综合| 亚洲国产精品免费在线观看| 亚洲av永久无码一区二区三区| 亚洲精品无码AV人在线播放 | 国产在线精品观看免费观看|