Consider using one javaBeans pattern in Effective Java when encountering multiple constructors. The fatal drawback of having one javaBeans pattern in a builder is inconsistencies and thread-safety issues associated with it. This article is based on Effective Java for a clear discussion;

1. Overlapping structure

  • In Java, constructor initialization is more basic, as programmers used to use the overlapping construction pattern, when passed a parameter construction, the first one has a required parameter, the second one has a required parameter and an optional parameter, the third one has two optional parameters… . Through overlapping structures, all multi-parameter structures are associated to achieve correlation; The following code:
public class Person { private String name; private String age; private String sex; private String country; private String edc; public Person(String name) { this(name,null); } public Person(String name, String age) { this(name,age,null); } public Person(String name, String age, String sex) { this(name,age,sex,null); } public Person(String name, String age, String sex, String country) { this(name,age,sex,country,null); } public Person(String name, String age, String sex, String country, String edc) { this.name = name; this.age = age; this.sex = sex; this.country = country; this.edc = edc; }}Copy the code
  • But there is a problem, one parameter, two parameters, three parameters, the use of its construction will be more clear, but for the construction of multiple parameters, will involve the problem of parameter order, sometimes error, and too many parameters, difficult to read, the control of parameters is not high, is not conducive to the reading of the program;

2. The javabeans model

  • For readability and controllability of program parameters, we will use Javabean for class instantiation and attribute assignment:
public class Person impletement Serializable{ private String name; private String age; private String sex; private String country; private String edc; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getEdc() { return edc; } public void setEdc(String edc) { this.edc = edc; }}Copy the code


  • When using Javabeans, especially multithreading, and retrieving the same Javabeans (which can be obtained by sequence/antisequence), the following scenario is simulated:

Thread A: Get person, name age sex set


Also thread B: gets person and performs the GET operation on it


There is A situation where thread B starts fetching properties before thread A completes the set

The javabean is in an inconsistent state; Related to this is thread-safety issues, so javabeans serve as padding for data and yao makes the necessary protective copies (not explained here)

3. Builder mode

  • Let’s go straight to the code
public class Person implements Serializable{ private String name; private String age; private String sex; private String country; private String edc; public Person(Builder builder) { this.name = builder.name; this.age = builder.age; this.sex = builder.sex; this.country = builder.country; this.edc = builder.edc; } public static class Builder{ private String name; private String age; private String sex; private String country; private String edc; public Builder setName(String name) { this.name = name; return this; } public Builder setAge(String age) { this.age = age; return this; } public Builder setSex(String sex) { this.sex = sex; return this; } public Builder setCountry(String country) { this.country = country; return this; } public Builder setEdc(String edc) { this.edc = edc; return this; } public Person build(){ return new Person(this); }}}Copy the code
  • In the builder mode, provide an auxiliary static builder builder, can set the attributes of Person, and javaBean is different, the builder is first set, in the build instance of Person, this can improve the code reading, but also can prevent the object without instantiation, is called; It does not cause inconsistency and solves the thread safety problem of Javabean mode.

As a Java engineer, should start from the basics, a deep understanding of object-oriented, with your mutual encouragement, thank you for reading ^.^