posts - 10,comments - 4,trackbacks - 0
          Avoid creating duplicate objects避免創(chuàng)建重復(fù)的對(duì)象
          It is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable

          String s = new String("silly"); // DON'T DO THIS!

          String s = "No longer silly";//do this

          In addition to reusing immutable objects, you can also reuse mutable objects that you know will not be modified. Here is a slightly more subtle and much more common example of what not to do, involving mutable objects that are never modified once their values have been computed:
          除了重用非可變的對(duì)象之外,對(duì)于那些已知不會(huì)被修改的可變對(duì)象,你也可以重用它們。

          such as
          public class Person {
          private final Date birthDate;
          // Other fields omitted
          public Person(Date birthDate) {
          this.birthDate = birthDate;
          }
          // DON'T DO THIS!
          public boolean isBabyBoomer() {
          Calendar gmtCal =
          Calendar.getInstance(TimeZone.getTimeZone("GMT"));
          gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
          Date boomStart = gmtCal.getTime();
          gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
          Date boomEnd = gmtCal.getTime();
          return birthDate.compareTo(boomStart) >= 0 &&
          birthDate.compareTo(boomEnd) < 0;
          }
          }

          改良的版本.
          The isBabyBoomer method unnecessarily creates a new Calendar, TimeZone, and two Date instances each time it is invoked. The version that follows avoids this inefficiency with a static initializer:

          class Person {
          private final Date birthDate;
          public Person(Date birthDate) {
          this.birthDate = birthDate;
          }
          /**
          * The starting and ending dates of the baby boom.
          */
          private static final Date BOOM_START;
          private static final Date BOOM_END;
          static {
          Calendar gmtCal =
          Calendar.getInstance(TimeZone.getTimeZone("GMT"));
          gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
          BOOM_START = gmtCal.getTime();
          gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
          BOOM_END = gmtCal.getTime();
          }
          public boolean isBabyBoomer() {
          return birthDate.compareTo(BOOM_START) >= 0 &&
          birthDate.compareTo(BOOM_END) < 0;
          }
          }

          。一個(gè)適配器是指這樣一個(gè)對(duì)象:它把功能委
          托給后面的一個(gè)對(duì)象,從而為后面的對(duì)象提供一個(gè)可選的接口。由于適配器除了后面的對(duì)象之外,沒(méi)有其他的狀態(tài)信息,所以針對(duì)某個(gè)給定對(duì)象的特定適配器而言,它不需要?jiǎng)?chuàng)建多個(gè)適配器實(shí)例。

          This item should not be misconstrued to imply that object creation is expensive and should be avoided. On the contrary, the creation and reclamation of small objects whose constructors do little explicit work is cheap, especially on modern JVM implementations. Creating additional objects to enhance the clarity, simplicity, or power of a program is generally a good thing.
          Conversely, avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight. A prototypical example of an object that does justify an object pool is a database connection. The cost of establishing the connection is sufficiently high that it makes sense to reuse these objects. Generally speaking, however, maintaining your own object pools clutters up your code, increases memory footprint, and harms performance. Modern JVM implementations have highly optimized garbage collectors that easily outperform such object pools on lightweight objects.
          posted on 2006-03-31 15:19 dodoma 閱讀(206) 評(píng)論(0)  編輯  收藏 所屬分類: java基礎(chǔ)
          主站蜘蛛池模板: 葫芦岛市| 招远市| 平泉县| 陇川县| 林口县| 界首市| 眉山市| 称多县| 昌江| 桃江县| 华安县| 紫云| 滨海县| 清流县| 金门县| 黄浦区| 大渡口区| 巫山县| 德昌县| 衢州市| 满洲里市| 广德县| 灵石县| 民勤县| 新野县| 拜泉县| 前郭尔| 常德市| 平陆县| 禹州市| 明水县| 义马市| 三江| 定兴县| 潮安县| 祁连县| 龙泉市| 桓台县| 广元市| 定日县| 格尔木市|