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

          類中可以將按照Constraint, Process and Specification進(jìn)行重構(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) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA General

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

          導(dǎo)航

          統(tǒng)計(jì)

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 临泉县| 涟源市| 资中县| 周口市| 钟祥市| 壶关县| 改则县| 玉屏| 通榆县| 南宁市| 龙山县| 彭山县| 营口市| 西乌珠穆沁旗| 天水市| 磴口县| 汾阳市| 集安市| 奎屯市| 盐源县| 枞阳县| 武清区| 盐边县| 福鼎市| 洪洞县| 宁陕县| 博兴县| 武清区| 长岛县| 昭通市| 乾安县| 茶陵县| 出国| 新乡县| 来宾市| 华蓥市| 仲巴县| 云林县| 望都县| 阿拉善盟| 绥宁县|