有關(guān)類中的重構(gòu)問題

          類中可以將按照Constraint, Process and Specification進行重構(gòu)

          例如:
          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年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 漯河市| 义乌市| 正安县| 佛冈县| 西乌珠穆沁旗| 辽阳县| 法库县| 昔阳县| 澎湖县| 库伦旗| 八宿县| 惠东县| 齐河县| 新晃| 永福县| 衡山县| 砀山县| 论坛| 东阿县| 大港区| 新密市| 竹北市| 扶风县| 东乡| 内丘县| 虹口区| 龙川县| 牟定县| 平度市| 巴塘县| 邯郸市| 瓦房店市| 安乡县| 莱阳市| 南平市| 阿尔山市| 元氏县| 嵩明县| 清丰县| 五河县| 台湾省|