<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 考慮以靜態(tài)工廠方法取代構(gòu)造函數(shù)

    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.
    一個(gè)類靜態(tài)工廠方法是有名字的.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了一個(gè)BigInteger對(duì)象,而BigInteger.probablePrime()能說(shuō)明返回的可能是一個(gè)素?cái)?shù)對(duì)象.另外,如果一個(gè)構(gòu)造函數(shù)僅僅由于參數(shù)順序不同而意思也不同.靜態(tài)工廠方法就更加有用了.它更能說(shuō)明函數(shù)的意思.因?yàn)樗幸粋€(gè)名字.
    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.每次請(qǐng)求時(shí),不需要重新創(chuàng)建一個(gè)對(duì)象----單例的意思.這可以在任何時(shí)刻控制這個(gè)對(duì)象,同時(shí)在比較的時(shí)候也不需要用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.可以返回一個(gè)原返回類型的子類型對(duì)象.
    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.這種靈活性的一個(gè)應(yīng)用是,一個(gè)A P I可以返回一個(gè)對(duì)象,同時(shí)又不使該對(duì)象的類成為公有的。以這種方式把具體的實(shí)現(xiàn)類隱藏起來(lái),可以得到一個(gè)非常簡(jiǎn)潔的A P I。這項(xiàng)技術(shù)非常適合
    于基于接口的框架結(jié)構(gòu),因?yàn)樵谶@樣的框架結(jié)構(gòu)中,接口成為靜態(tà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();
    }
    }
    }

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

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

    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) 評(píng)論(0)  編輯  收藏 所屬分類: java基礎(chǔ)
    主站蜘蛛池模板: 亚洲乱码中文字幕综合234| 久久久久国产精品免费看| 亚洲男同gay片| 亚洲成a人片77777群色| 久久久久亚洲av无码专区喷水| 日本亚洲视频在线 | 污污网站免费观看| 精品亚洲永久免费精品| 免费国产在线视频| 精品一区二区三区无码免费视频| 13一14周岁毛片免费| 又黄又爽又成人免费视频| aa级一级天堂片免费观看| 女人18特级一级毛片免费视频| 成年性午夜免费视频网站不卡| 日韩人妻无码免费视频一区二区三区 | 亚洲av综合av一区| 亚洲一区二区成人| 亚洲嫩草影院在线观看| 亚洲 欧洲 视频 伦小说| 精品亚洲成A人在线观看青青| 国产AV日韩A∨亚洲AV电影 | 亚洲宅男天堂a在线| 在线a亚洲老鸭窝天堂av高清| 老牛精品亚洲成av人片| 有色视频在线观看免费高清在线直播 | 在线精品亚洲一区二区三区| 亚洲AV无码一区二区三区DV| 亚洲免费闲人蜜桃| 亚洲av片在线观看| sss日本免费完整版在线观看| 久久免费观看国产精品88av| 我们的2018在线观看免费高清| 永久免费毛片手机版在线看| 亚洲一本大道无码av天堂| 亚洲av无码国产精品色午夜字幕 | 亚洲精品久久无码av片俺去也| 国产亚洲美女精品久久| 不卡视频免费在线观看| 最近2019中文字幕免费直播 | 成人免费黄色网址|