一个 Java 的小问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
bbbai
V2EX    程序员

一个 Java 的小问题

  •  
  •   bbbai 2018-09-27 17:53:07 +08:00 2896 次点击
    这是一个创建于 2652 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到有代码是如下这样写的,

    class A{

    private A(B b){...}

    public static A of(B b) { return new A(b); }

    }

    这样写的好处是什么呢? 如果这是一种好的风格或者手段的话,是否有一种获取途径,来了解好的风格或者手段?

    16 条回复    2018-09-28 09:20:49 +08:00
    yemoluo
        1
    yemoluo  
       2018-09-27 18:10:44 +08:00
    单例模式了解下,嗯,其实,好处不仅仅是单例
    elgae
        2
    elgae  
       2018-09-27 18:12:24 +08:00
    @GTim 这跟单例有一毛钱关系?
    yemoluo
        3
    yemoluo  
       2018-09-27 18:13:22 +08:00
    @elgae 说说你的看法
    linshuang
        4
    linshuang  
       2018-09-27 18:46:34 +08:00
    也是第一次看见,或许可以把 of 替换成一个句子来达到代码具备更强的语义的目的. 就模式而言,至少我没见过这种。
    例如 Parent p = Parent.withChildren(children)
    maninfog
        5
    maninfog  
       2018-09-27 18:52:38 +08:00 via Android
    @GTim 这个每次都是 new 和单例的确无关阿
    这种写法估计是为了利用静态方法名充当注释的作用,个人感觉。
    icris
        6
    icris  
       2018-09-27 18:54:57 +08:00   1
    参考示例:BigInteger.valueOf(1)
    lululau
        7
    lululau  
       2018-09-27 18:59:31 +08:00
    new A(b) VS A.of(b)
    lululau
        8
    lululau  
       2018-09-27 19:00:05 +08:00
    8 个字符 VS 7 个字符,效率提升了
    wbgbg
        9
    wbgbg  
       2018-09-27 19:02:26 +08:00   1
    Effective Java 第一条 考虑使用静态工厂方法替代构造方法
    这里的主要优点就是有方法名来表示含义
    sutra
        10
    sutra  
       2018-09-27 19:02:41 +08:00
    可以参考 Instant.ofXXX() 的实现,可以看出来它并不是每次都 new 一个对象出来,有些是直接用的共享的对象。
    CasualYours
        11
    CasualYours  
       2018-09-27 19:06:57 +08:00 via Android
    Optional 类就是这种写法,好处就是代码语义更直接吧。
    Cbdy
        12
    Cbdy  
       2018-09-27 19:08:13 +08:00   1
    Effective Java 第三版第二章第一节有详细介绍

    我简单摘录一下:

    ### 优点

    One advantage of static factory methods is that, unlike constructors, they have names.

    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.

    A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.

    A fourth advantage of static factories is that the class of the returned object can vary from call to call as a function of the input parameters.

    A fifth advantage of static factories is that the class of the returned object need not exist when the class containing the method is written.

    ### 缺点

    The main limitation of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

    A second shortcoming of static factory methods is that they are hard for programmers to find.

    ### 一些常见的静态构造方法

    **from**: A type-conversion method that takes a single parameter and returns a corresponding instance of this type, for example:

    ```java
    Date d = Date.from(instant);
    ```

    **of**: An aggregation method that takes multiple parameters and returns an instance of this type that incorporates them, for example:

    ```java
    Set<Rank> faceCards = EnumSet.of(JACK, QUEEN, KING);
    ```

    **valueOf**: A more verbose alternative to from and of, for example:

    ```java
    BigInteger prime = BigInteger.valueOf(Integer.MAX_VALUE);
    ```

    **instance** or **getInstance**: Returns an instance that is described by its parameters (if any) but cannot be said to have the same value, for example:

    StackWalker luke = StackWalker.getInstance(options);

    **create** or **newInstance**: Like instance or getInstance, except that the method guarantees that each call returns a new instance, forexample:

    ```java
    Object newArray = Array.newInstance(classObject, arrayLen);
    ```
    CasualYours
        13
    CasualYours  
       2018-09-27 19:09:39 +08:00 via Android
    @GTim 不是单机例,of 静态方法每次都返回新的实例。
    Raymon111111
        14
    Raymon111111  
       2018-09-27 19:29:57 +08:00
    这是工厂, 了解一下工厂模式即可.
    lowzoom
        15
    lowzoom  
       2018-09-28 01:05:47 +08:00
    class A 体现不出这个写法的精髓,要 interface A
    bk201
        16
    bk201  
       2018-09-28 09:20:49 +08:00
    我认为是方便链式调用
    类似 a.of(b).add(c).avg()
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2228 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 15:26 PVG 23:26 LAX 07:26 JFK 10:26
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86