Consider builder--effective Java reading notes when encountering multiple constructor parameters

Source: Internet
Author: User
Tags sodium

/** * If you have multiple parameters in a class 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 mode is easier to read and write than using the traditional overlay constructor pattern, and the builder is more secure than JavaBeans. * * @author Liu Xiangfeng * * * * Public  class nutritionfacts {    //All the parameters    Private Final intServingsize;Private Final intservings;Private Final intcalories;Private Final intFatPrivate Final intSodiumPrivate Final intCarbohydrate; Public Static  class Builder {        //Required parameters, final guarantees that it must be assigned a value        Private Final intServingsize;Private Final intservings;//Options available        Private intCalories =0;Private intFat =0;Private intSodium =0;Private intCarbohydrate =0;/** * Complete initialization of the required parameters in the constructor * * @param servingsize * @param servings */         Public Builder(intServingsize,intServings) { This. servingsize = servingsize; This. servings = servings; }/** * * @param val * Assign a value to an option * @return Builder object for chain adjustment Use the Assignment function * /         PublicBuildercalories(intval) {calories = Val;return  This; } PublicBuilderFat(intval) {fat = val;return  This; } PublicBuilderSodium(intval) {sodium = Val;return  This; } PublicBuilderCarbohydrate(intval) {carbohydrate = Val;return  This; }/** * Create a Get object method and pass yourself (Builder) as a parameter * * @return nutritionfacts Object */         PublicNutritionfactsBuild() {return NewNutritionfacts ( This); }    }/** * Privatisation constructor, guaranteed to operate on the same object * * @param Builder incoming constructor to assign value */    Private nutritionfacts(Builder builder)        {servingsize = builder.servingsize;        servings = builder.servings;        calories = builder.calories;        Fat = Builder.fat;        Sodium = Builder.sodium;    Carbohydrate = builder.carbohydrate; }/** * Test method * * @param args */     Public Static void Main(string[] args) {Nutritionfacts CocaCola =NewNutritionfacts.builder ( -,8). Calories ( -). Sodium ( *). Carbohydrate ( -). build (); }}

Consider builder--effective Java reading notes when encountering multiple constructor parameters

Related Article

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.