Effective Java 01 Consider static factory methods instead of constructors

 Advantage

  • Unlike constructors, they have names. (BigInteger.probablePrime vs BigInteger(int, int, Random)
  • They are not required to create a new object each time they're invoked(Flyweight pattern). This ensures that a.equals(b) if and only if a = = b. Then the client can use = = instead of equals method which gets better performance to Equals method.
  • It can return an object of any subtype of their return type.(This gives you great flexibility in choosing the class of the returned object. )

    /********************sample code ******************/

    // Service provider framework sketch

    // Service interface

    public interface Service {

    ... // Service-specific methods go here

    }

    // Service provider interface

    public interface Provider {

    Service newService();

    }

    // Noninstantiable class for service registration and access

    public class Services {

    private Services() {

    } // Prevents instantiation (Item 4)

    // Maps service names to services

    private static final Map<String, Provider> providers = new ConcurrentHashMap<String, Provider>();

    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    // Provider registration API

    public static void registerDefaultProvider(Provider p) {

    registerProvider(DEFAULT_PROVIDER_NAME, p);

    }

    public static void registerProvider(String name, Provider p) {

    providers.put(name, p);

    }

    // Service access API

    public static Service newInstance() {

    return newInstance(DEFAULT_PROVIDER_NAME);

    }

    public static Service newInstance(String name) {

    Provider p = providers.get(name);

    if (p == null)

    throw new IllegalArgumentException(

    "No provider registered with name: " + name);

    return p.newService();

    }

    }

  • Reduce the verbosity of creating parameterized type instances.

 

Disadvantages
  • The class without public or protected constructors cannot be subclassed.
  • They are not readily distinguishable from other static methods.

 

Here are some common names for static factory methods:

• valueOf—Returns an instance that has, loosely speaking, the same value as its

parameters. Such static factories are effectively type-conversion methods.

• of—A concise alternative to valueOf, popularized by EnumSet(Item 32).

• getInstance—Returns an instance that is described by the parameters but

cannot be said to have the same value. In the case of a singleton, getInstance

takes no parameters and returns the sole instance.

• newInstance—LikegetInstance, except that newInstanceguarantees that

each instance returned is distinct from all others.

• getType—Like getInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

• newType—Like newInstance, but used when the factory method is in a different class. Type indicates the type of object returned by the factory method.

转载于:https://www.cnblogs.com/haokaibo/p/consider-static-factory-methods-instead-of-constructors.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值