Effective Java (2) when you encounter multiple constructor parameters, consider using a builder

Source: Internet
Author: User
Tags constructor

I. BACKGROUND

For classes that have multiple optional parameters, what do we usually pass the arguments to? There are three options available:

①. Overlapping builder pattern

②. JavaBeans mode

③. Builder Builder Mode

Here we analyze the advantages and disadvantages of the above three methods.

Second, overlapping builder mode

There are only the necessary parameters in the first constructor in the overlapping constructor pattern, the second constructor has an optional argument, the third constructor has two optional parameters, and so on, and so on, the last constructor contains all the optional parameters. This scheme is feasible, but has a large defect.

Disadvantage: When there are many optional parameters, the client code is difficult to write, and difficult to read, if the client accidentally reversed the order of the two parameters, the compiler will not complain, but the program in the Run-time error behavior.

/* Constructor mode/public class NutritionFacts1 {private int a1;///Must be private int A2;//Must be private in T A3; optional private int A4;  
    Optional public NutritionFacts1 (int a1, int A2) {This (A1, a2, 0);  
    NutritionFacts1 (int a1, int a2, int a3) {This (A1, A2, 0, 0);  
        Public NutritionFacts1 (int a1, int a2, int a3, int a4) {this.a1 = A1;  
        THIS.A2 = A2;  
        this.a3 = A3;  
    this.a4 = A4;  
    public int getA1 () {return A1;  
    The public void setA1 (int a1) {this.a1 = A1;  
    public int getA2 () {return a2;  
    The public void setA2 (int a2) {this.a2 = A2;  
    public int getA3 () {return a3;  
    The public void setA3 (int a3) {this.a3 = A3;  
    public int getA4 () {return A4;  
  }    
    public void setA4 (int a4) {this.a4 = A4;  
    Public String toString () {return a1 + "-" + A2 + "-" + A3 + "-" + A4;  
        public static void Main (string[] args) {NutritionFacts1 nf = new NutritionFacts1 (1, 2, 3, 4);  
    SYSTEM.OUT.PRINTLN (NF); }  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.