About Java static factory methods, see which one is enough!

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43170213/article/details/100551529

1. Introduction: What is a static factory method

In Java, a class instance get the easiest way is to use the new keyword, to achieve the creation of an object by the constructor.
like this:

    Fragment fragment = new MyFragment();
    // or
    Date date = new Date();

But in the actual development, we often also see another way to get class instance:

 Fragment fragment = MyFragment.newIntance();
    // or 
 Calendar calendar = Calendar.getInstance();
    // or 
 Integer number = Integer.valueOf("3");

↑ like this: By not new, but rather a way to provide external static method itself instance, that is what we call a static factory method (Static factory method).

Knowledge Point: new exactly what to do?

In simple terms: When we use new to construct a new instance of the class actually told I needed a new JVM instance. JVM will automatically open up a space in memory, and then call the constructor to initialize member variables, eventually returns a reference to the caller.

Advantage of static factory method of
2.1 different static factory methods and constructors first advantage is that they have a name
due to the nature of language, Java constructor with the class name are the same. This leads to a problem is the constructor's name is not flexible enough, often can not accurately describe the return value, even worse when there are multiple overloaded constructor, if the parameter type, number and relatively similar, then it is very easy to make mistakes .

For example, the following piece of code:

Date date0 = new Date();
Date date1 = new Date(0L);
Date date2 = new Date("0");
Date date3 = new Date(1,2,1);
Date date4 = new Date(1,2,1,1,1);
Date date5 = new Date(1,2,1,1,1,1);

- Date class has a lot of overloaded functions, for developers, if not particularly familiar with, I am afraid is the need to hesitate, to find the right constructor. As for the other code readers, it is estimated that more needed to view documents in order to understand the meaning of each parameter.

(Of course, Date class in the current version of Java, leaving only one with no parameters and a constructor parameter, other have been marked as @Deprecated a)

If using a static factory method, it can play a more meaningful name to the method, such as in front of the valueOf, newInstance, getInstance , etc., for writing and reading the code can be clearer.

2.2 second advantage, do not always create a new object is called
This is very easy to understand, and sometimes external callers only need to get one instance, does not care whether the new instance; or if we want to provide external a when singleton - If the factory method, can be easily controlled in the inside, to prevent creating unnecessary objects, reduce overhead.
General plant and static linked singleton.
In a practical scenario, writing a single embodiment may also be implemented using mostly static factory method.

2.3 The third advantage, can return to the original return type of the subclass
2.4 fourth advantage, when creating an instance of a generic tape, the code can become simple
addition to
3.1 but may have a plurality of different parameters of the same name factory method
3.2 can reduce external exposure of the property
more than 3.3 layer control, easy to modify the unified

3 summary

Summary
Overall, I think, "consider using static factory methods instead of constructors' points which, in addition to the name, you can use these advantages other than grammatical level subclass and other, more significance in engineering, I I think the most important role it essentially is: can increase the provider class of control over their own class provided by.

As a developer, when we, as the caller, use the class provided by someone else, if you want to use the new keyword to create an instance of the class, if not particularly familiar with the class, then it must be especially careful - new reality it is handy, so that it is often abused, anytime, anywhere new is very risky, in addition may cause performance problems in terms of memory, but also often makes the code structure becomes chaotic.

And when we as a provider class can not control the behavior of a specific caller, but we can try to use some of the ways to increase their own control of the class, reducing the chance of the caller to make mistakes, which is more accountable to the code a concrete manifestation.

Guess you like

Origin blog.csdn.net/qq_43170213/article/details/100551529