有關類中的重構問題

          類中可以將按照Constraint, Process and Specification進行重構

          例如:
          public class Bookshelf {
          private int capacity = 20;
          private Collection content;
          public void add(Book book) {
          if(content.size() + 1 <= capacity)
          {
          content.add(book);
          } else {
          throw new
          IllegalOperationException(
          “The bookshelf has reached
          its limit.”);
          }
          }
          }
          We can refactor the code, extracting the constraint in a separate
          method.
          public class Bookshelf {
          private int capacity = 20;
          private Collection content;
          public void add(Book book) {
          if(isSpaceAvailable()) {
          content.add(book);
          } else {
          throw new
          IllegalOperationException(
          “The bookshelf has reached
          its limit.”);
          }
          }
          private boolean isSpaceAvailable() {
          return content.size() < capacity;
          }
          }

          posted on 2007-10-18 12:07 劉錚 閱讀(200) 評論(0)  編輯  收藏 所屬分類: JAVA General

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 绩溪县| 沙雅县| 华池县| 奈曼旗| 玛纳斯县| 隆安县| 永顺县| 华蓥市| 崇信县| 西充县| 宁远县| 江阴市| 石家庄市| 正宁县| 江达县| 吴江市| 普安县| 丰镇市| 桐柏县| 丰台区| 渑池县| 昭觉县| 裕民县| 天镇县| 鹤岗市| 古丈县| 双峰县| 彝良县| 凤庆县| 会东县| 彰化县| 循化| 利川市| 昆明市| 德阳市| 咸丰县| 大安市| 房产| 赤壁市| 隆尧县| 鲁山县|