Detailed example of Java static factory method

Detailed example of Java static factory method

What is a static factory method

For a class, the most common way to let users get an instance of itself is to provide a public constructor. Of course, here is another method - static factory method, a static method that returns an instance of a class. For example, a method of Boolean that converts the basic type Boolean into an encapsulated class, valueof:

Why use the static factory method

So why do we use static factory methods instead of constructors?

Because the static factory method has the following three characteristics - naming, environmental protection and multiple children. Let's talk about them one by one.

>Named static factory method

For constructors, there can be multiple constructors according to different input parameters, but the names of these constructors are the same. Users will be confused when calling. Which one should be called.

After using the static factory method, you can give the method different names according to the function of the method. Only the name is good. The user will know what the method name means and which method should be called at this time, which greatly improves the readability of the code.

>You don't have to create a new object every time you call

Using constructors, a new object is generated each time.

Static factory methods can repeatedly return pre created objects.

The above Boolean is a very good example. Both true and false variables are pre created and immutable final objects. Whoever needs to use them will return them to the past without worrying about being modified.

The following is the initialization code of true and false variables:

>Multiple subtypes can return objects of any subtype of the original return type

With constructors, you can only return one type of object; Using the static factory method, you can return any subtype object of the original return type as needed. Take the noneof method of enumset as an example:

For performance reasons, the specific type returned by this method is determined by the number of enumeration types. If it exceeds 64, it returns jumboenumset, otherwise it returns regularenumset. These two types are invisible to the user. The user only needs to know that it is enumset.

It is precisely because static factory methods have greater advantages than constructors. When creating classes, we should avoid providing public constructors as the first reaction, and give priority to static factory methods.

Common static factory method names

Some conventional names of static factory methods are attached:

Valueof / of -- type conversion. The returned instance and input parameter have the same value, such as Boolean valueOf()、EnumSet. Valueof () getInstance -- returns a pre created instance newinstance -- returns a new instance

Isn't the static factory method the factory pattern?

At this point, many people may think that this is not the factory model? A: not exactly. The static factory method described in this paper, like the factory mode, is used to replace the constructor. It has the above three advantages: naming, environmental protection and multi son.

However, the implementation methods and usage scenarios are different.

First, intuitively, in terms of code structure, the factory pattern we call usually needs an xxxfactory class to define factory methods; The static factory in this paper

Method, only one class is required, and the class itself provides the factory method of the production object.

Secondly, let's think about it. If a class provides static factory methods at the time of design, does it still need to use factory mode?

Yes, No.

That is, we need to use the factory pattern only when a class does not provide static factory methods.

Brain hole, if Apple has a strong parts factory, does it still need foxconn?

summary

The static factory method has three advantages - naming, environmental protection and multi son.

If a class provides static factory methods, there is no need to consider factory mode for this class.

When we create a class, we should avoid that the first reaction is to provide a public constructor and give priority to static factory methods.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>