<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>


    posts - 10,comments - 4,trackbacks - 0
    Avoid creating duplicate objects避免創建重復的對象
    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:
    除了重用非可變的對象之外,對于那些已知不會被修改的可變對象,你也可以重用它們。

    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;
    }
    }

    。一個適配器是指這樣一個對象:它把功能委
    托給后面的一個對象,從而為后面的對象提供一個可選的接口。由于適配器除了后面的對象之外,沒有其他的狀態信息,所以針對某個給定對象的特定適配器而言,它不需要創建多個適配器實例。

    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 閱讀(202) 評論(0)  編輯  收藏 所屬分類: java基礎
    主站蜘蛛池模板: 国产一级淫片免费播放电影| 97国产免费全部免费观看| 亚洲a∨无码男人的天堂| 久久亚洲精品中文字幕三区| 亚洲视频在线一区二区| 亚洲精品无码你懂的网站| 亚洲?V乱码久久精品蜜桃| 免费一级毛片在线观看| 亚洲视频在线免费| 亚洲AV人无码激艳猛片| 亚洲高清不卡视频| 亚洲精品中文字幕无码A片老| 亚洲国产午夜精品理论片在线播放| 亚洲AV无码成人网站在线观看| 日韩少妇内射免费播放| 久草视频在线免费看| 99久久免费国产精品特黄| 亚洲AV无码成人精品区大在线| 亚洲va久久久噜噜噜久久天堂 | 羞羞视频免费网站含羞草| 一二三四在线观看免费中文在线观看| 精品一区二区三区免费观看| 桃子视频在线观看高清免费视频| 性生交片免费无码看人| 久久久精品国产亚洲成人满18免费网站| 亚洲网址在线观看| 一级毛片aa高清免费观看| 99热在线精品免费全部my| 亚洲成av人片天堂网| mm1313亚洲国产精品无码试看| 老司机69精品成免费视频| 亚洲精品成人片在线观看精品字幕| 亚洲av无码有乱码在线观看| 国产成人亚洲精品狼色在线| 亚洲欧洲日产国产最新| 亚洲.国产.欧美一区二区三区| 无码人妻丰满熟妇区免费| 久久精品国产亚洲7777| 国产精品久久久久久亚洲小说| 97性无码区免费| 国产亚洲高清不卡在线观看|