PackageBuildermanypara;//JavaBeans pattern-allows inconsistency,mandates mutability Public classNutritionfactsbean {//Parameters initialized to default values (if any)//Required Private intServingsize =-1; Private intServings =-1; //option Private intCalories = 0; Private intFat = 0; Private intSodium = 0; Private intCarbohydrate = 0; PublicNutritionfactsbean (intServingsize,intservings) { Super(); This. servingsize =servingsize; This. servings =servings; } Public intgetservingsize () {returnservingsize; } Public voidSetservingsize (intservingsize) { This. servingsize =servingsize; } Public intgetservings () {returnservings; } Public voidSetservings (intservings) { This. servings =servings; } Public intgetcalories () {returncalories; } Public voidSetcalories (intcalories) { This. Calories =calories; } Public intGetfat () {returnfat; } Public voidSetfat (intfat) { This. Fat =fat; } Public intGetsodium () {returnsodium; } Public voidSetsodium (intsodium) { This. sodium =sodium; } Public intgetcarbohydrate () {returncarbohydrate; } Public voidSetcarbohydrate (intcarbohydrate) { This. Carbohydrate =carbohydrate; } Public Static voidMain (string[] args) {Nutritionfactsbean Nutritionfactsbean=NewNutritionfactsbean (240, 8); Nutritionfactsbean.setfat (8); }}
A JavaBean May is in the inconsistent state partway through its construction.
The JavaBean pattern precludes the possibility of making a class immutable.
PackageBuildermanypara;//Builder Pattern Public classNutritionfactsbuilder {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate; Public Static classBuilderImplementsBuilders<nutritionfactsbuilder> { //Required Parameters Private Final intservingsize; Private Final intservings; //Optional parameters-initialized to default values Private intCalories = 0; Private intFat = 0; Private intSodium = 0; Private intCarbohydrate = 0; PublicBuilder (intServingsize,intservings) { Super(); This. servingsize =servingsize; This. servings =servings; } PublicBuilder Calories (intcalories) { This. Calories =calories; return This; } PublicBuilder Fat (intfat) { This. Fat =fat; return This; } PublicBuilder Sodium (intsodium) { This. sodium =sodium; return This; } PublicBuilder Carbohydrate (intcarbohydrate) { This. Carbohydrate =carbohydrate; return This; } PublicNutritionfactsbuilder Build () {return NewNutritionfactsbuilder ( This); } } PrivateNutritionfactsbuilder (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) {Nutritionfactsbuilder nutritionfacts=NewNutritionfactsbuilder.builder (240, 8). Calories (). Sodium (35). build (); }}
Package Buildermanypara; // A Builder for objects of type Public Interface Builders<t> { public T Build ();}
In summary,the Builder pattern was a good choice when designing classes whose constructors or static factories would Ore than a handful of parameters,especially if most of those parameters is optional. Client code is much easier to read and write with builders than with the traditional telescoping constructor Pattern,and b Uilders is much safer than JavaBeans.
Effective Java English Second Edition reading note Item 2:consider a builder when faced with many constructor parameters.