Eclipse第四講:重構(gòu)----實(shí)戰(zhàn)重構(gòu)

          1.方法抽取:Extract Method
          將calcCost中參數(shù)校驗(yàn)部分抽取成方法verify.
          選定代碼--->右鍵Refactor-->Extract Method
          2.重命名:Rename
          (1)重命名類名Test1為CostCalculator
          (2)重命名User為UserInfo
          選定類名-->右鍵Refactor-->Rename
          3.內(nèi)聯(lián):Inlining
          內(nèi)聯(lián)方法add
          內(nèi)聯(lián)原因:調(diào)用方法越多,運(yùn)行速度越慢
          選定方法名("add")-->右鍵Refactor-->Inline
          4.常量抽取:Extract Constant
          原因:避免手誤
          將"normal","vip"抽取成常量
          選定字符串-->右鍵Refactor-->Extract Constant
          5.抽取局部變量:Extract Local Voriable
          將user,userType抽取成變量
          原因:減少代碼長(zhǎng)度,增加代碼可讀性
          選定要抽取的變量(user.userName)-->右鍵Refactor-->Extract Local Variable
          6.包裝字段:Encapsulate Field
          重構(gòu)user類,用set,get包裝
          主菜單-->Refactor-->Encapsulate Field
          7.抽取接口:Extract Interface
          選定方法名(calcCost)-->右鍵Refactor-->Extract Interface-->Name:ICcstCalculator
          重構(gòu)后的代碼為:
          (一)UserInfo.java
          package com.comnew.demo;

          public class UserInfo {
             private String userName;
             private String userType;
             private int amount;
          public void setUserName(String userName) {
           this.userName = userName;
          }
          public String getUserName() {
           return userName;
          }
          public void setUserType(String userType) {
           this.userType = userType;
          }
          public String getUserType() {
           return userType;
          }
          public void setAmount(int amount) {
           this.amount = amount;
          }
          public int getAmount() {
           return amount;
          }
          }
          (二)CostCalculator.java
          package com.comnew.demo;

          public class CostCalculator implements ICostCalculator {
              private static final int VIPPRICE = 0;
           private static final int NORMALPRICE = 10;
           private static final String VIP = "vip";
           private static final String NORMAL = "normal";

           /* (non-Javadoc)
            * @see com.comnew.demo.ICostCalculator#calcCost(com.comnew.demo.UserInfo)
            */
           public int calcCost(UserInfo user)
                {
                 verify(user);
                
                
                 int ret=0;
                 String userType = user.getUserType();
            if(userType.equals(NORMAL))
                 {
                  ret=user.getAmount()+NORMALPRICE;
                 }
                 else if(userType.equals(VIP))
                 {
                  ret=user.getAmount()+VIPPRICE;
                 }
                 else
                 {
                  throw new IllegalArgumentException("useType的類型錯(cuò)誤!必須未如下幾種:"+NORMAL+","+VIP);
                 }
                 return ret;
                }

           private void verify(UserInfo user) {
            String useType = user.getUserType();
            if(useType==null)
                 {
                  throw new IllegalArgumentException("userType不能為空!");
                 }
                 if(user.getAmount()<=0)
                 {
                  throw new IllegalArgumentException("amount必須大于0!");
                 }
           }

           public static void main(String[] args)
           {
            String userName="tom";
            String userType=NORMAL;
            int amount=20;
            UserInfo user=new UserInfo();
            user.setUserName(userName);
            user.setUserType(userType);
            user.setAmount(amount);
            System.out.println(new CostCalculator().calcCost(user));
           }
          }
          (三)ICostCalculator.java
          package com.comnew.demo;

          public interface ICostCalculator {

           public abstract int calcCost(UserInfo user);

          }

          posted on 2007-04-25 08:37 糖果 閱讀(472) 評(píng)論(0)  編輯  收藏 所屬分類: 畢業(yè)設(shè)計(jì)

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          畢業(yè)設(shè)計(jì)~~加油~~

          常用鏈接

          留言簿(11)

          隨筆分類

          隨筆檔案

          戰(zhàn)友^-^

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 菏泽市| 乌拉特前旗| 阿尔山市| 石棉县| 梧州市| 开原市| 莱西市| 区。| 石家庄市| 寿宁县| 宜都市| 习水县| 获嘉县| 垫江县| 桐柏县| 闽侯县| 阿巴嘎旗| 贺兰县| 惠水县| 新乡县| 呼玛县| 正宁县| 阳谷县| 金秀| 南乐县| 教育| 襄城县| 岚皋县| 抚顺县| 皋兰县| 铁力市| 濮阳县| 宝兴县| 深水埗区| 麻城市| 桂林市| 大邑县| 称多县| 玉龙| 迁安市| 张掖市|