In the meantime, you can reduce this problem by drawing attention to static factories in class or interface documentation and by adhering to common naming conventions. Here are some common names for static factory methods. This list is far from exhaustive: • from—A type-conversion method that takes a single parameter and returns a corresponding instance of this type, for example: 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: Click here to view code image Set<Rank> faceCards =
In the meantime, you can reduce this problem by drawing attention to static factories in class or interface documentation and by adhering to common naming conventions. Here are some common names for static factory methods. This list is far from exhaustive: • from—A type-conversion method that takes a single parameter and returns a corresponding instance of this type, for example: 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: Click here to view code image Set<Rank> faceCards = EnumSet.of(JACK, QUEEN, KING); • valueOf—A more verbose alternative to from and of, for example: Click here to view code image 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: Click here to view code image StackWalker luke = StackWalker.getInstance(options); • create or newInstance—Like instance or getInstance, except that the method guarantees that each call returns a new instance, for example: Click here to view code image Object newArray = Array.newInstance(classObject, arrayLen); • getType—Like getInstance, but used if the factory method is in a different class. Type is the type of object returned by the factory method, for example: Click here to view code image FileStore fs = Files.getFileStore(path); • newType—Like newInstance, b...
...more
This highlight has been truncated due to consecutive passage length restrictions.
Good naming recommendations for common factory method patterns:
• from(<obj to convert>) / valueOf
• of(<list of objects to compose into obj>)
• instance(<opts>)
• create(<opts>) [alt. to instance that guarantees distinct instance from previous invocation, whereas instance may share singletons/oligarchs(?)]
• get*Type*()/*type*(). [similar to instance, but for when factory method is in diff class from obj/interface returned]
• new*Type*() [similar to create but for when factory method is in diff class from obj/interface returned]