Article 2: when encountering multiple construction parameters, consider using the Builder

Source: Internet
Author: User
Tags sodium
Public class _ 2_2_nutritionfacts {private final int servingsize; private final int serving; private final int Calories; private final int fat; private final int sodium; private final int Carbohydrate; private _ 2_2_nutritionfacts (Builder) {This. servingsize = builder. servingsize; this. serving = builder. serving; this. calories = builder. calories; this. fat = builder. fat; this. sodium = builder. sodium; this. carbohydrate = builder. carbohydrate;} The establishment of the public static class builder {// builder, which has the same domain as the original class. The objects built through the Builder are assigned to the original object private final int servingsize; // static internal classes cannot reference external class members or methods private final int serving; private int Calories; private int fat; private int sodium; private int Carbohydrate; Public Builder (INT servingsize, int serving) {This. servingsize = servingsize; this. serving = serving;} public builder calories (INT calories) {// a builder is returned for each field modification. Finally, this builder is used to create the object 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;} public _ 2_2_nutritionfacts bicould () {return New _ 2_2_nutritionfacts (this) ;}} public static void main (string ARGs []) {_ 2_2_nutritionfacts food = new _ 2_2_nutritionfacts.builder (20,100 ). calories (100 ). carbohydrate (100 ). fat (1, 100 ). sodium (100 ). biwould () ;}/ ** builder mode: * The Builder is used to create an object. This mode is suitable for constructing a Builder with many parameters, its attributes have the same members as the objects to be constructed. In the builder, each member in the builder defines a modification method, and return this builder public builder fat (INT fat) {This. fat = fat; return this;} then define a builder method and return the public _ 2_2_nutritionfacts build () {retunr new _ 2_2_nutritionfacts (this )} because we need to create a constructor with builder parameters in the build method, we need to define a constructor. Because it is only used internally, private _ 2_2_nutritionfacts (Builder) should be used) {} then assign all the members in the builder to the _ 2_2_nutritionfacts member, and then call New _ 2_2_nutritionfacts.builder () when generating the object (). A (3 ). B (23 ). build ();*/

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.