First: Consider replacing the constructor with a static factory method
Advantages of using the static factory approach:
- A static factory method has a name, and a method with the appropriate name is easier to read.
Class users with multiple constructors often do not know which to use, and may consider providing multiple, appropriately named, Static factory methods.
- No need to create a new object each time it is called, compared to the constructor
immutable Classes can either pre-create a good instance or cache a well-built instance to avoid duplicating the object. This method is similar to flyweight mode. This technique can greatly improve performance if the program frequently requests the creation of the same objects, and the cost of creating objects is high.
- Can return any subtype of the original return type
There is more flexibility in choosing the classes that return objects. For example: An API can provide an object, but the actual class of the object can be hidden and the user does not need to know its specific type. The static factory method returns the class to which the object belongs, and can not be present when writing a class that contains the static factory method. This flexible, static factory approach forms the basis for the service provider Framework .
Simple example:
1 Public Interface Service {2 // service-specific methods Go here 3 }
Public Interface Provider { public Service newservice ();}
1 ImportJava.util.Map;2 ImportJava.util.concurrent.ConcurrentHashMap;3 4 /**5 * Noninstantiable class for service registration and access6 */7 Public classServices {8 PrivateServices () {}//Prevent instantiation9 Ten Private Static Finalmap<string, provider> providers =NewConcurrenthashmap<>(); One Private Static FinalString default_provider_name = "<def>"; A - Public Static voidRegisterdefaultprovider (Provider p) { - Registerprovider (Default_provider_name, p); the } - - Public Static voidRegisterprovider (String name, Provider p) { - providers.put (name, p); + } - + //Service Access API A Public StaticService newinstance () { at returnnewinstance (default_provider_name); - } - - Public StaticService newinstance (String name) { -Provider p =providers.get (name); - in if(NULL==p) { - Throw NewIllegalArgumentException ("No provider registered with Name:" +name); to } + - returnP.newservice (); the } * $}
Make your code concise when you create an instance of a parameterized type
Disadvantages:
Main disadvantage: Classes cannot be overridden if they do not contain a public or protected constructor.
The second disadvantage is that there is actually no difference from other static methods
It is not explicitly identified in the API documentation like a constructor, so it is very difficult to find out how to instantiate a class for a class that provides a static method instead of a constructor. You can compensate for this disadvantage by focusing on static factories in class or interface annotations and following standard naming conventions.
Customary name of the static factory method:
Less strictly speaking, the instance returned by the method has the same value as the parameter, so the static factory method is actually a type conversion method.
A more concise alternative to valueof, used in Enumset and popular.
The returned instance is described by the parameters of the method, but cannot be said to have the same value as the parameter.
Like getinstance, but newinstance can ensure that every instance returned is different from all other instances
Like getinstance, but used when the factory method is in a different class. Type represents the object types returned by the factory method
Like newinstance, but used when the factory method is in a different class.
In short, static factory methods and constructors are useful and need to understand their strengths. Static factories are usually more appropriate, and the first response is to provide a public constructor without first considering the static factory method.
[Effective Java] creating and destroying objects