Chapter 2: create and destroy objects. Item2: when encountering multiple constructors, consider using the builder.

Source: Internet
Author: User
Tags sodium

If a class contains a large number of optional parameters, you can use the following methods:

1. Overlapping constructor:

Package com. twoslow. cha2;/*** the overlapped constructor is feasible, but it is difficult to write client code when many parameters are involved. * @ Author sai **/public class item201 {private final int servingsize; private final int servings; private final int Calories; private final int fat; private final int sodium; private Final int Carbohydrate; Public item201 (INT servingsize, int servings) {This (servingsize, servings, 0);} public item201 (INT servingsize, int servings, int calories) {This (servingsize, servings, calories, 0);} public item201 (INT servingsize, int servings, int calories, int fat) {This (servingsize, servings, calories, fat, 0);} public item201 (INT servingsize, int servings, int calories, int fat, int carbohydrate) {This (servingsize, servings, calories, fat, carbohydrate, 0 );} public item201 (INT servingsize, int servings, int calories, int fat, int carbohydrate, int sodium) {This. servingsize = servingsize; this. servings = servings; this. calories = Calories; this. fat = fat; this. carbohydrate = Carbohydrate; this. sodium = sodium ;}}

2. The JavaBeans mode provides the setter method:

Package com. twoslow. cha2;/*** JavaBeans mode. Call the setter method to set each required parameter. * The constructor is divided into several calls. During the constructor, the JavaBean may be inconsistent. * @ Author sai **/public class item202 {private int servingsize =-1; private int servings =-1; private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; Public item202 () {} public void setservingsize (INT servingsize) {This. servingsize = servingsize;} public void setservings (INT servings) {This. servings = servings;} public void setcalories (INT calories) {This. calories = Calories;} public void setfat (INT fat) {This. fat = fat;} public void setsodium (INT sodium) {This. sodium = sodium;} public void setcarbohydrate (INT carbohydrate) {This. carbohydrate = Carbohydrate ;}}

3. Builder:

Package com. twoslow. cha2;/*** builder, not enough: 1. To create an object, you must explicitly create its builder. * 2. It is more lengthy than the overlapped constructor and can only be used when there are many parameters,> = 4. * @ author sai **/public class item203 {private final int servingsize; private final int servings; private final int Calories; private final int fat; private final int sodium; private Final int Carbohydrate; public static class builder {// builder private final int servingsize; private final int servings; private int calories = 0 can be constructed through the implementation of generic interfaces; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; Public Builder (INT servingsize, int servings) {This. servingsize = servingsize; this. servings = servings;} public builder calories (INT calories) {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 item203 build () {return New item203 (this) ;}} public item203 (Builder) {This. servingsize = builder. servingsize; this. servings = builder. servings; this. calories = builder. calories; this. fat = builder. fat; this. sodium = builder. sodium; this. carbohydrate = builder. carbohydrate ;}}

 

Chapter 2: create and destroy objects. Item2: when encountering multiple constructors, consider using the builder.

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.