有關類中的重構問題

          類中可以將按照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)

          搜索

          最新評論

          主站蜘蛛池模板: 元朗区| 泰州市| 锡林郭勒盟| 突泉县| 缙云县| 壤塘县| 定远县| 万荣县| 玛多县| 灵石县| 余干县| 三河市| 额济纳旗| 桦川县| 珠海市| 盈江县| 定边县| 改则县| 延川县| 贺州市| 沂水县| 霍邱县| 永清县| 云浮市| 泰兴市| 三江| 孝感市| 威宁| 石屏县| 富阳市| 皮山县| 阳信县| 安西县| 沙坪坝区| 宁夏| 双牌县| 蕉岭县| 庆阳市| 西宁市| 疏附县| 富川|