Consider static factory methods instead of constructors.
A class may provide clients with static factory methods instead of, or in addition to, constructors. These are used to replace constructors with carefully chosen names. Can be similar to the Flyweight pattern. The returned class can be non-public, and can vary from invocation to invocation. This is not the same as the factory method pattern.
Constructors that differ only in the order of their parameter types must use static factory methods.
Supports a service provider framework, which contains three key required and one optional components:
- service interface, which providers implement
- provider registration API, which the system uses to register implementations
- service access API., which clients use to obtain an instance of a service.
- service provider interface, which providers implement to create instance of their service implementation.
Example TODO Markings to put into your code to suggest the usage of this item.
- TODO: make instance controlled
- TODO: make static factory method
- TODO: refactor into a service provider interface.
- TODO: service access API
- TODO: service provider interface
- they have names, unlike constructors. this helps describe the object being returned. Easier to read.
- they prevent the wrong constructor from being called by mistake
- people reading the code will know what the code does without referring to the class documentation.
- they are not required to create a new object each time they’re invoked.
- supports immutable classes to use preconstructed instances or to cache instances as they’re constructed and dispense them repeatedly to avoid creating unnecessary duplicate objects.
- can greatly improve performance if equivalent objects are requested often, especially if they are expensive to create.
- allows instance-controlled classes: strict control over what instances exist at any time.
- an object of any subtype of the class can be returned.
- allows the return of objects without making them public – supports interface-based frameworks a very compact API.
- reduces conceptual weight
- reduces the need to read additional class documentation for the returned classes.
- requires a client to refer to the returned object by its interface rather than its implementation class (item 52).
- supports service provider frameworks (e.g. JDBC API) – 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.
- service provider framework supports the service provider framework pattern.
- reduces the verbosity of creating parameterized type instances.
- classes without public or protected constructors cannot be subclassed.
- not readily distinguishable from other static methods. Can be reduced by drawing attention to static factory methods through the use of common names like “valueOf”, “of”, “getInstance”, “newInstance”, “getType”, “newType” (where Type is an arbitrary type).