<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

    Consider providing static factory methods instead of
    constructors 考慮以靜態工廠方法取代構造函數

    The normal way for a class to allow a client to obtain an instance is to provide a public
    constructor.

    A class can provide a public static factory method, which is simply
    a static method that returns an instance of the class

    such as
    public static Boolean valueOf(boolean b) {
    return (b ? Boolean.TRUE : Boolean.FALSE);
    }

    advantages
    1:One advantage of static factory methods is that, unlike constructors, they have names.
    一個類靜態工廠方法是有名字的.If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static factory with a well-chosen name can make a class easier to use and the resulting client code easier to read. 如BigInteger(int, int,Random)只知道new了一個BigInteger對象,而BigInteger.probablePrime()能說明返回的可能是一個素數對象.另外,如果一個構造函數僅僅由于參數順序不同而意思也不同.靜態工廠方法就更加有用了.它更能說明函數的意思.因為它有一個名字.
    the reason?are : If the parameters to a constructor do not, describe the object being
    returned, a static factory with a well-chosen name can make a class easier to use and the
    resulting client code easier to read.

    2:A second advantage of static factory methods is that, unlike constructors, they are not
    required to create a new object each time they're invoked.每次請求時,不需要重新創建一個對象----單例的意思.這可以在任何時刻控制這個對象,同時在比較的時候也不需要用equals而用= =就可以解決了.

    3:A third advantage of static factory methods is that, unlike constructors, they can return
    an object of any subtype of their return type.可以返回一個原返回類型的子類型對象.
    One application of this flexibility is that an API can return objects without making their
    classes public.Hiding implementation classes in this fashion can lead to a very compact API.這種靈活性的一個應用是,一個A P I可以返回一個對象,同時又不使該對象的類成為公有的。以這種方式把具體的實現類隱藏起來,可以得到一個非常簡潔的A P I。這項技術非常適合
    于基于接口的框架結構,因為在這樣的框架結構中,接口成為靜態工廠方法的自然返回類型
    sample:
    // Provider framework sketch
    public abstract class Foo {
    // Maps String key to corresponding Class object
    private static Map implementations = null;
    // Initializes implementations map the first time it's called
    private static synchronized void initMapIfNecessary() {
    if (implementations == null) {
    implementations = new HashMap();
    // Load implementation class names and keys from
    // Properties file, translate names into Class
    // objects using Class.forName and store mappings.
    ...
    }
    }
    public static Foo getInstance(String key) {
    initMapIfNecessary();
    Class c = (Class) implementations.get(key);
    if (c == null)
    return new DefaultFoo();
    try {
    return (Foo) c.newInstance();
    } catch (Exception e) {
    return new DefaultFoo();
    }
    }
    }

    公有的靜態工廠方法所返回的對象的類不僅可以是非公有的,而且該類可以隨著每次調用
    而發生變化,這取決于靜態工廠方法的參數值。只要是已聲明的返回類型的子類型,都是允
    許的。而且,為了增強軟件的可維護性,返回對象的類也可以隨著不同的發行版本而不同。
    disadvantages:

    1:The main disadvantage of static factory methods is that classes without public or protected constructors cannot be subclassed.
    類如果不含公有的或者受保護的構造函數,就不能被子類化。對于公有的靜態工廠所返回的非公有類,也同樣如此

    2:A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.


    In summary, static factory methods and public constructors both have their uses, and it pays to understand their relative merits. Avoid the reflex to provide constructors without first considering static factories because static factories are often more appropriate. If you've weighed the two options and nothing pushes you strongly in either direction, it's probably best to provide a constructor simply because it's the norm.




    posted on 2006-03-30 17:08 dodoma 閱讀(220) 評論(0)  編輯  收藏 所屬分類: java基礎
    主站蜘蛛池模板: 亚洲国产天堂在线观看| 免费A级毛片在线播放不收费| 亚洲自偷自偷在线制服 | 亚洲av无码专区亚洲av不卡| 蜜臀AV免费一区二区三区| 亚洲男人第一av网站| 精品免费tv久久久久久久| 亚洲成色WWW久久网站| 在线播放免费人成毛片乱码| 人人狠狠综合久久亚洲88| 在线观看免费无码专区| 亚洲国产美女在线观看| 97在线线免费观看视频在线观看| 亚洲www77777| 免费A级毛片无码A∨男男 | 精品国产呦系列在线观看免费| 亚洲色偷偷综合亚洲AVYP| 美女在线视频观看影院免费天天看 | 亚洲一卡2卡3卡4卡国产网站| 性做久久久久久免费观看| 国产精品亚洲综合天堂夜夜| 国产偷国产偷亚洲高清日韩| 日本在线看片免费| 久久精品国产亚洲AV久| 国产91久久久久久久免费| 成在人线av无码免费高潮水| 亚洲黄色免费网站| 麻豆国产精品入口免费观看| 免费无码午夜福利片69| 亚洲午夜未满十八勿入| 免费看片A级毛片免费看| 99久久免费国产精品热| 亚洲人成影院午夜网站| 亚洲国产精品成人久久蜜臀 | 久久九九久精品国产免费直播| 亚洲av永久无码精品秋霞电影影院| 我们的2018在线观看免费高清| 色妞www精品视频免费看| 亚洲AV午夜成人影院老师机影院 | 无码国产精品一区二区免费式影视 | 在线aⅴ亚洲中文字幕|