有關類中的重構問題

          類中可以將按照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 劉錚 閱讀(201) 評論(0)  編輯  收藏 所屬分類: JAVA General

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 平阴县| 天镇县| 靖州| 东至县| 鹤庆县| 平利县| 攀枝花市| 彭州市| 和龙市| 昌宁县| 琼中| 贺州市| 桃江县| 峡江县| 石渠县| 股票| 永和县| 金乡县| 达日县| 九江县| 江安县| 琼海市| 普兰县| 来宾市| 弋阳县| 长岭县| 勃利县| 平谷区| 西宁市| 湖北省| 沂南县| 关岭| 高青县| 罗甸县| 青浦区| 格尔木市| 巴彦淖尔市| 抚顺县| 石门县| 金川县| 静海县|