Item 1: Consider Static Factory

2018-10-08  本文已影响0人  Aunki

1 Example

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

2 ?

    Fragment fragment = new MyFragment();
    // or
    Date date = new Date();
    Fragment fragment = MyFragment.newIntance();
    // or 
    Calendar calendar = Calendar.getInstance();
    // or 
    Integer number = Integer.valueOf("3");

3 Advantage

3.1 比构造器多名字

valueOfnewInstancegetInstance

3.2 不用被调用时都创建新对象

3.3 返回原返回类型的子类

上一篇 下一篇

猜你喜欢

热点阅读