using java builder to create an instance constructor instead
For the class, in order to allow the client to obtain an instance of his own,
The most traditional way is to provide a public constructor.
A class
Overload multiple constructors
Customers face multiple constructors This API can never remember which constructor with this,
And each call to the constructor is bound to create a new object,
If the program needs to reuse objects, the constructor can not avoid creating unnecessary objects.
Static factory method different constructors
The first big advantage: They have a name
Create a new object does not have to call them every time: The second big advantage
The third big advantage: they can return to the original return type of any sub-type of the object
The fourth big advantage: the class of the object returned change with each call occurs, depending on the parameter value of the static method
Here are some of the usual static factory method name
1) from --------------------- type conversion
Date d = Date.from(instant)
2) of ------------------------ polymerization process, having a plurality of parameters, return an instance of the type, combine them into
Set faceCard = EnumSet.of(JACK,QUEEN,KING)
3) valueOf ----------------- more complicated than the previous two methods BigInteger prime = BigInteger.valueOf (Integer.MAX_VALUE)
…
When a plurality of parameters constructor
Static factories and constructors have a common limitation:
They do not scale well to a large number of optional parameters.
For example, with a class to represent the outside of the food nutrition labels.
The constructor calls Normally you do not want to set parameters,
Just a few parameters can be accepted, but with the increase of parameters, soon lost control.
Briefly, overlapping configuration mode possible, but when there are more parameters, client code would look very bad.
javaBean appears stronger than the above-described overlap mode configuration, but will also cause problems multithreading unsafe
So, we played builder.
Using a static inner class Builder, members of the class definition of the external member of the same variable.
JavaBean similar mode setting each parameter
Finally, construct a new instance of the class with the external build method (external calls the private constructor class)
Main.java
public class Main
{
public static void main(String[] args)
{
// initialize a NutritionFacts example, the last call must build () method to call the constructor NutritionFacts class to instantiate an object of NutritionFacts
NutritionFacts cocaCola = new NutritionFacts.Builder(120,200).calories(1).carbohydrate(22).build();
// parameters and decide when called by the client, not necessarily to a fixed input parameters, improved write overloaded constructor give users a lot of trouble
// javaBean improve the lot of the problem overloaded constructor, but it also brought problems not thread-safe
NutritionFacts cocaCola1 = new NutritionFacts.Builder(223,233).build();
} Zhengzhou good gynecological hospital http://www.kd0371.com/
}
NutritionFacts.java
public class NutritionFacts
{
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
// private constructor method
private NutritionFacts(Builder build)
{
this.servingSize=build.servingSize;
this.servings=build.servings;
this.calories=build.calories;
this.fat=build.fat;
this.sodium=build.sodium;
this.carbohydrate=build.carbohydrate;
}
// builder mode (Builder static inner classes) can be accessed directly outside
public static class Builder
{
private final int servingSize;
private final int servings;
// Initialize defaults
private int calories=0;
private int fat=0;
private int sodium=0;
private int carbohydrate=0;
// constructor for initializing immutable (Final) parameters
public Builder(int servingSize,int servings)
{
this.servingSize=servingSize;
this.servings=servings;
}
// The following methods were used for each member initialization
public Builder calories(int calories)
{
this.calories=calories;
return this;
}
public Builder fat(int fat)
{
this.fat=fat;
return this;
}
public Builder sodium(int sodium)
{
this.sodium=sodium;
return this;
}
public Builder carbohydrate(int carbohydrate)
{
this.carbohydrate=carbohydrate;
return this;
}
Example // The last configuration NutritionFacts class, which is defined in the class Builder
public NutritionFacts build()
{
return new NutritionFacts(this);
}
}
}