What is the static factory method
Java static factory methods add public static to methods to make them public and static. This method returns an instance of the class, as if a factory had produced a product. That’s why it’s called the static factory method. There is an example of a static factory method in Boolea.java:
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
Copy the code
A Boolean instance is returned.
Compare static factory methods with constructors
2.1 name
Static factory methods can define self-descriptive method names based on the nature of the returned instance. For example, the BigInteger class defines a static factory method, probablePrime, that returns a prime of type BigInteger:
public static BigInteger probablePrime(int bitLength, Random rnd) {
if (bitLength < 2)
throw new ArithmeticException("bitLength < 2");
return (bitLength < SMALL_PRIME_THRESHOLD ?
smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
}
Copy the code
But if it is a constructor, it must be a class name, such as BigInteger in the above example. This makes it impossible to determine the nature of the returned BigInteger instance from its name.
2.2 the cache
Each call to the constructor creates a new object. If you need to create an object in advance and cache it for later reuse. The constructor approach does not meet this requirement. The static factory method does the trick. For example, the valueOf method in Boolea.java actually returns the implementation-created static properties TRUE and FALSE:
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
Copy the code
The following are statically initialized TRUE and FALSE:
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code true}.
*/
public static final Boolean TRUE = new Boolean(true);
/**
* The {@code Boolean} object corresponding to the primitive
* value {@code false}.
*/
public static final Boolean FALSE = new Boolean(false);
Copy the code
2.3 a subclass
Classes that have constructors can be inherited by subclasses. But classes that only have static factory methods do not. Class composition can be used to solve this problem.
2.4 summarize
To compare | Static factory method | The constructor |
---|---|---|
The name of the | You can customize the name as required | It can only be a class name |
The cache | Callable repeat object | Each call creates a new object |
A subclass | Cannot be inherited by subclasses | Can be inherited by subclasses |
3 static factory method naming method
keywords | instructions | Into the reference number | The sample |
---|---|---|---|
from | Type conversion, type A to type B. | 1 | public static Date from(Instant instant) |
of | Aggregate, do merge. | n | public static <E extends Enum> EnumSet of(E e1, E e2, E e3) |
instance | Returns an instance, either newly created or reusing an already created one. | n | public static Object instance(int length) |
create | Returns the newly created instance. | n | public static Object create(int length) |
type | The factory method is not in the class instance to be returned; type is the name of the class to be returned. | n | public static ArrayList list(Enumeration e) |
The final example, where the method definition is in Collections, is to return an instance of ArrayList, so it’s named List.
public static <T> ArrayList<T> list(Enumeration<T> e) {
ArrayList<T> l = new ArrayList<>();
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
Copy the code
It is recommended that static factory methods be used to instantiate classes in preference.
Joshua Loch. Effective Java Version 3 [M]. China Machine Press, 2018.p.4-8.