* * Create an object's builder mode * Builder mode is only suitable for scenarios where you need to specify multiple optional parameters when creating an object.
* Client: * 1: In the client can obtain the person object through the following code * Person.personbuilder builder=new Person.personbuilder ("111", "Wanda Less", "male");
* Here to select the optional parameter age hobby for example demo; * Person person=builder.setage. Sethobby ("reading")-build ();
* * public class Person {* * * necessary parameters * ID number, name, sex * Declaration as private to avoid external unsafe modification/private String ID;
private String name;
Private String sex;
* * Optional Parameters * age, telephone, hobby, address, educational background;/private int ages;
Private String phone;
Private String hobby;
Private String addr;
Private String major;
* * * The Person class private constructor, why use a private constructor.
* * Private person (Personbuilder builder) {this.id=builder.id;
This.name=builder.name;
This.sex=builder.sex;
This.age=builder.age;
This.phone=builder.phone;
This.hobby=builder.hobby;
THIS.ADDR=BUILDER.ADDR;
This.major=builder.major; * * * Call Setter,getter method to get change Private member */public void setId (String ID) {this.id=id;
Public String GetId () {return id; * * * This personbuilder is specifically used to create a person object, * So the Personbuilder class is subordinate to the person class. Therefore, the use of the * inner class can clearly express this subordination. Use the "Static" * inner class to create a Personbuilder object when no person object exists/public static class Personbuilde
r{* * Necessary parameters * ID number, name, gender * * Private String ID;
private String name;
Private String sex;
* * Optional Parameters * age, telephone, hobby, address, educational background;/private int ages;
Private String phone;
Private String hobby;
Private String addr;
Private String major; * * To have the client invoke the constructor using all the necessary parameters to get a Builder object/public Personbuilder (String id,string name,string Sex
) {this.id=id;
This.name=name;
This.sex=sex;
* * * Call the parameterless build method to create the Person object * Note that the person object is not created before the build method call, so there is no need to worry about the issue of early disclosure.
Public person build () { Return to new person (this);
* * * Invokes a setter-like method to set each of the relevant optional parameters * Here is the Personbuilder object, which makes it easy to set optional parameters in cascading mode, so that the hierarchy is clear and the code is simple
* * Public personbuilder setage (int age) {this.age = age;
return this;
Public Personbuilder Setphone (String phone) {this.phone = phone;
return this;
Public Personbuilder sethobby (String hobby) {this.hobby = hobby;
return this;
Public Personbuilder setaddr (String addr) {this.addr = addr;
return this;
Public Personbuilder Setmajor (String major) {this.major = major;
return this;
}
}
}