When an object has more than one member variable and the object needs to be initialized when it is created, it is inconvenient to use the constructor directly to initialize it. For example
Public classBuilderdemo {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate;}
It is common practice to provide an overlapping constructor, first providing a constructor with only the necessary parameters, and then increasing the constructor that contains the optional arguments until all optional parameters are included. For example
PublicBuilderdemo (intServingsize,intservings) { This. servingsize =servingsize; This. servings =servings; } PublicBuilderdemo (intServingsize,intServings,intcalories) { This. servingsize =servingsize; This. servings =servings; This. Calories =calories; } PublicBuilderdemo (intServingsize,intServings,intCalories,intfat) { This. servingsize =servingsize; This. servings =servings; This. Calories =calories; This. Fat =fat; } PublicBuilderdemo (intServingsize,intServings,intCalories,intFatintsodium) { This. servingsize =servingsize; This. servings =servings; This. Calories =calories; This. Fat =fat; This. sodium =sodium; } PublicBuilderdemo (intServingsize,intServings,intCalories,intFatintSodiumintcarbohydrate) { This. servingsize =servingsize; This. servings =servings; This. Calories =calories; This. Fat =fat; This. sodium =sodium; This. Carbohydrate =carbohydrate; }
If only the following optional parameters are required, the preceding optional arguments must be provided when calling. And the parameter is very long can cause the inconvenience of reading and revising.
Another approach is to provide a set method for each member variable that sets the content of the member variable after the object is created. However, because the construction process is divided into several invocations, the object may be in an inconsistent state during construction.
The best way to do this is to use the builder pattern to construct the entire object through an internal static class. The code is as follows:
Public classBuilderdemo {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate; Public Static classBuilder {Private Final intservingsize; Private Final intservings; // Private intCalories = 0; Private intFat = 0; Private intSodium = 0; Private intCarbohydrate = 0; // PublicBuilder (intServingsize,intservings) { This. servingsize =servingsize; This. servings =servings; } PublicBuilder Calories (intval) {Calories=Val; return This; } PublicBuilder Fat (intval) {Fat=Val; return This; } PublicBuilder Sodium (intval) {Sodium=Val; return This; } PublicBuilder Carbohydrate (intval) {Carbohydrate=Val; return This; } PublicBuilderdemo Build () {return NewBuilderdemo ( This); } } PrivateBuilderdemo (Builderdemo.builder Builder) {servingsize=builder.servingsize; Servings=builder.servings; Calories=builder.calories; Fat=Builder.fat; Sodium=Builder.sodium; Carbohydrate=builder.carbohydrate; } Public Static voidMain (string[] args) {Builderdemo demo=NewBuilder (calories). Carbohydrate (43). build (); }}
Such code is easy to write and read. Furthermore, constraints can be imposed on their parameters, and the build method can examine these constraints, copy the parameters from the builder to the object, and test them in the object domain.
Insufficient builder mode: 1. You need to create the builder before creating the object, which is not applicable in a very performance-oriented situation. 2.Builder is more verbose than the overlapping constructor pattern, so it is used only when there are many parameters, such as more than 4.
"Effective Java" reading notes (a): Replace constructors with multiple parameters with builder mode