For a class, the most common approach is to provide a common constructor for the client to obtain an instance of its seniority. There is also a place to put your hair, but also for each programmer's toolbox. A class can provide a common static factory method, which is simply a static method that returns an instance of the class.
A class can provide its client (object) through a static Factory method class, rather than through a constructor. The benefits of doing so include:
1. The first advantage of the static factory method differs from the constructor is that they have a name. For example, the BigInteger returned by the constructor BigInteger (int,int,random) can be a prime number, If the static factory method named Biginteger.probableprime is represented, it is clearly clearer. When a class requires more than one constructor with the same signature, the static factory method is used instead of the constructor, and the name is chosen carefully to highlight the difference between them.
2. The second big advantage of the static factory method differs from the constructor is that it is not necessary to create a new object each time they are called. This specifically does not say, you can look at the singleton mode.
3. The third big advantage of static factory methods differs from constructors is that they can return objects of any subtype of the original return type. This technology is suitable for interface-based frameworks. When using this static method, it is a good practice to even ask the client to refer to the returned object through an interface, rather than referencing the returned object through its implementation class.
4. The fourth big advantage of the static factory approach is that they make the code more concise when creating instances of parameterized types.
Disadvantages:
The main disadvantage of the static factory approach is that the class cannot be overridden if it does not contain a shared or protected constructor.
The second disadvantage of static factory methods is that they are virtually indistinguishable from other static methods. In the API documentation, they are not explicitly identified as a constructor. Therefore, for classes that provide a static factory method instead of a constructor, it is very difficult to find out how to instantiate a class. However, you can compensate for this disadvantage by focusing on static factories in class or interface annotations and following the standard naming conventions. Here are some of the customary names:
ValueOf
Of
GetInstance
Newinstance
GetType
NewType
In short, both the static factory method and the common constructor are useful, and we need to understand their respective singing. Static factories are usually more appropriate, so the first response is to provide a public constructor without first considering a static factory.