Item 1: Consider static factory methods instead of constructors

阅读更多

1.  One advantage of static factory methods is that, unlike constructors, they have names. BigInteger(int, int, Random), which returns a BigInteger that is probably prime, would have been better expressed as a static factory method named BigInteger.probablePrime. In cases where a class seems to require multiple constructors with the same signature, replace the constructors with static factory methods and carefully chosen names to highlight their differences.

 

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. This allows immutable classes to use preconstructed instances, or to cache instances as they’re constructed, and dispense them repeatedly to avoid creating unnecessary duplicate objects.

 

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 implementing certain interface without making their implementation classes public. Interfaces can’t have static methods, so by convention, static factory methods for an interface named Type are put in a noninstantiable class named Types.(Collections have static methods returning unmodifiable , synchronized implementations of Collection without disclose their implementations) The class of the returned object can also vary from release to release for enhanced software maintainability and performance.

 

4.   The class java.util.EnumSet has no public constructors, only static factories. They return one of two implementations, depending on the size of the underlying enum type: if it has sixty-four or fewer elements, as most enum types do, the static factories return a RegularEnumSet instance, which is backed by a single long; if the enum type has sixty-five or more elements, the factories return a JumboEnumSet instance, backed by a long array.

 

5.   The class of the object returned by a static factory method need not even exist at the time the class containing the method is written. Such flexible static factory methods form the basis of service provider frameworks, such as the Java Database Connectivity API (JDBC).

 

6.   A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations. There are three essential components of a service provider framework: a service interface, which providers implement; a provider registration API, which the system uses to register implementations, giving clients access to them; and a service access API, which clients use to obtain an instance of the service. An optional fourth component is a service provider interface, which providers implement to create instances of their service implementation. In the absence of a service provider interface, implementations are registered by class name and instantiated reflectively.( In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDriver is the provider registration API, DriverManager.getConnection is the service access API, and Driver is the service provider interface.)

 

7.   A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances. By type inference , you could replace the wordy declaration: 

Map> m = new HashMap>();

 with the following succinct:  

Map> m = HashMap.newInstance();

 If HashMap have the following static method : 

public static  HashMap newInstance() {
    return new HashMap();
}

 However Java 7 has already support the following syntax: 

Map> m = new HashMap<>();

 

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

 

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

 

10.  common naming conventions for static factory methods:

    a)  valueOf—Returns an instance that has, loosely speaking, the same value as its parameters. Such static factories are effectively type-conversion methods.
    b)  of—A concise alternative to valueOf, popularized by EnumSet.
    c)  getInstance—Returns an instance that is described by the parameters but cannot be said to have the same value.
    d)  newInstance—Like getInstance, except that newInstance guarantees that each instance returned is distinct from all others.
    e)  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.
    f)  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.

你可能感兴趣的:(Constructor,Static,Factory,EnumSet)