Effective Java Second Edition---Builder Pattern

Source: Internet
Author: User

If you have multiple parameters in a class's constructor or static factory, the builder mode is a good choice when designing this class, especially if most of the parameters are optional.

Client code using the builder pattern is easier to read and write than using the traditional overloaded constructor pattern, and the constructor is more secure than javabeens.

The code is as follows:

1 /**2 * Created by Zhanga.fnst on 2016/3/14.3  */4  Public classnutritionfacts {5     Private Final intservingsize;6     Private Final intservings;7     Private Final intcalories;8     Private Final intflat;9     Private Final intsodium;Ten     Private Final intcarbohydrate; One  A      Public  Static classbuilder{ -         //Required Parameters -         Private Final intservingsize; the         Private Final intservings; -  -         //Optional Parameters -         Private  intCalories = 0; +         Private  intFlat = 0; -         Private  intSodium = 0; +         Private  intCarbohydrate = 0; A  at          PublicBuilder (intServingsize,intservings) { -              This. servingsize =servingsize; -              This. servings =servings; -         } -  -          PublicBuilder Calories (intval) { inCalories =Val; -             return  This; to         } +  -          PublicBuilder Flat (intval) { theFlat =Val; *             return  This; $         }Panax Notoginseng  -          PublicBuilder Carbohydrate (intval) { theCarbohydrate =Val; +             return  This; A         } the  +          PublicBuilder Sodium (intval) { -Sodium =Val; $             return  This; $         } -  -          Publicnutritionfacts Build () { the             return NewNutritionfacts ( This); -         }Wuyi     } the  -     Privatenutritionfacts (Builder builder) { WuServingsize =builder.servingsize; -Servings =builder.servings; AboutCalories =builder.calories; $Flat =Builder.flat; -Sodium =Builder.sodium; -Carbohydrate =builder.carbohydrate; -     } A}
View Code

Main method:

1 /** 2 * Created by Zhanga.fnst on 2016/3/14. 3  */ 4  Public class Testmain {5     New Nutritionfacts.builder (240,8). 6             Calories (+). Sodium (+). Carbohydrate (). build (); 7 }

Effective Java Second Edition---Builder Pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.