Mode Motive
Both in the real world and in software systems, there are complex objects that have a number of components, such as automobiles, including wheels, steering wheel, engine, and many more. For most users, it is not necessary to know the assembly details of these parts, but rather to use a complete car, which can be designed and described by the builder mode, which separates the parts from the assembly process, creating a complex object step by step. The user simply needs to specify the type of the complex object to be able to get the object without having to know the specifics of the construction inside.
In software development, there are also a lot of complex objects like automobiles, which have a series of member properties, some of which are member objects of reference types. And in these complex objects, there may be some restrictions, such as some properties are not assigned to a complex object can not be used as a complete product; Some attributes must be assigned in a certain order, one property may not be assigned before a value is assigned, and so on.
Complex objects are equivalent to a car to be built, and the properties of the object are equivalent to the parts of the car, and the process of building the product is equivalent to the assembly process. Because of the complexity of the assembly process, the assembly process of these components is often "externally" to an object called a builder, which is returned to the client by a complete product object that has been built, and the user does not have to care about the properties that the object contains and how they are assembled. This is the model motive of the builder pattern. Pattern Definition
Builder pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations.
The builder pattern is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of the complex objects, and the user does not need to know the specifics of the build in-house. Builder mode belongs to the object creation mode. Depending on the Chinese translation, the builder pattern can also be called the generator pattern. Example
In the second edition of effective Java, Josh Bloch mentioned in the second chapter that using the builder pattern to handle constructors that require a lot of parameters. Not only does he demonstrate the use of builder, but it also describes the benefits of this approach relative to the use of constructors with many parameters. What needs to be pointed out is that Josh Bloch has been running through this idea in his book. Standard Builder
Person.java
Package com.ricky.builder.ch1; /** * ${description} * * @author Ricky Fung * @create 2016-07-06 16:39 * * public class Person {private final Str
ing FirstName;
Private final String LastName;
Private final String MiddleName;
private final int age;
Private final String Street;
Private final String District;
Private final String city;
Private final String province;
Private Final Boolean Isfemale;
Private Final Boolean isemployed; Public person (string firstName, String lastName, String middlename, Int. age, String Street, String Distr
ICT, String city, String province, Boolean Isfemale, Boolean isemployed) {this.firstname = FirstName;
This.lastname = LastName;
This.middlename = MiddleName;
This.age = age;
This.street = Street;
this.district = District;
this.city = City;
this.province = Province;
This.isfemale = Isfemale; this.isemployed = Isemployed; } @Override Public String toString () {return "person{" + "firstname= '" + firstName + ' \ '
' + ', lastname= ' + lastName + ' \ ' + ', middlename= ' + middlename + ' +
", age=" + Age + ", street= ' + street + ' \ ' +", district= ' + district + ' \ ' + ", city= ' + city + ' \ ' +", province= ' + province + ' \ ' + ", isfemale=" +
Isfemale + ", isemployed=" + isemployed + '} ';
}
}
Personbuilder
Package com.ricky.builder.ch1; /** * ${description} * * @author Ricky Fung * @create 2016-07-06 16:40 * public class Personbuilder {private St
Ring FirstName;
Private String LastName;
Private String MiddleName;
private int age;
Private String Street;
Private String District;
Private String City;
Private String Province;
Private Boolean Isfemale;
Private Boolean isemployed;
Public Personbuilder () {} public personbuilder FirstName (String firstName) {this.firstname = FirstName;
return this;
} public Personbuilder LastName (String lastName) {this.lastname = LastName;
return this;
} public Personbuilder MiddleName (String middlename) {this.middlename = MiddleName;
return this;
Public Personbuilder Age (int.) {this.age = age;
return this;
Public Personbuilder Street (String street) {this.street = Street; ReturnThis
} public Personbuilder District (String district) {this.district = district;
return this;
Personbuilder City (String city) {this.city = city;
return this;
} public Personbuilder Province (String province) {this.province = province;
return this;
} Public Personbuilder Isfemale (Boolean isfemale) {this.isfemale = Isfemale;
return this;
} Public Personbuilder isemployed (Boolean isemployed) {this.isemployed = isemployed;
return this; ' Public ' Create () {return new person (FirstName, LastName, MiddleName, age, Street, District, city, p
Rovince, Isfemale, isemployed);
}
}
Package com.ricky.builder;
Import Com.ricky.builder.ch1.Person;
Import Com.ricky.builder.ch1.PersonBuilder;
/**
* ${description}
*
* @author Ricky Fung
* @create 2016-07-06 16:41
*/public class Personbuilderdemo {public
static void Main (string[] args) {person person
= new Personbuilder ()
. FirstName ("Ricky"). LastName ("Fung"). Age ("the")
Street ("Jianguo Road").
District ("Chaoyang")
. City (" Beijing ").
Province (" Peking ").
Isfemale (False)
. Isemployed (True).
create ();
SYSTEM.OUT.PRINTLN (person);
}
}
static inner class
Siamessage.java
Package COM.RICKY.BUILDER.CH2; /** * ${description} * * @author Ricky Fung * @create 2016-07-06 13:19 * public class Siamessage {private final
String Businesscode;
Private final String type;
Private final String message;
private final int timeout;
Siamessage (String Businesscode, String type, string message, int timeout) {this.businesscode = Businesscode;
This.type = type;
this.message = message;
This.timeout = timeout;
} public static Builder custom () {return new builder ();
} public String Getbusinesscode () {return businesscode;
} public String GetType () {return type;
Public String GetMessage () {return message;
} public int GetTimeout () {return timeout;
} public static class Builder {private String businesscode;
Private String type;
Private String message;
private int timeout; PublicBuilder Setbusinesscode (String businesscode) {this.businesscode = Businesscode;
return this;
Public Builder SetType (String type) {this.type = type;
return this;
Public Builder setmessage (String message) {this.message = message;
return this;
Public Builder setTimeout (int timeout) {this.timeout = timeout;
return this;
Public Siamessage Build () {Initdefaultvalue (this);
return new Siamessage (businesscode, message, type, timeout); } private void Initdefaultvalue (Builder builder) {if (Builder.businesscode==null | | builder.business
Code.length () <1) {throw new IllegalArgumentException ("Businesscode can not is empty!"); } if (Builder.message==null | | builder.message.length () <1) {throw new IllegalargumentexcePtion ("message can not is empty!"); } if (Builder.type==null | | builder.type.length () <1) {//set default Mime-type Builder.type = "Appl
Ication/json ";
} if (builder.timeout<1) {builder.timeout = 6*1000; }}} @Override public String toString () {return ' siamessage{' + ' Businessco
De= ' + businesscode + ' \ ' + ', type= ' + type + ' \ ' + ', message= ' + message + ' \ ' +
", timeout=" + timeout + '} ';
}
}
Client Calls
Package com.ricky.builder;
Import Com.ricky.builder.ch2.SIAMessage;
/**
* ${description}
*
* @author Ricky Fung
* @create 2016-07-06 13:22
*/public class Siamessagedemo {public
static void Main (string[] args) {
//1.
Siamessage message = new Siamessage.builder ()
. Setbusinesscode ("Yrd").
setmessage ("Hello World")
. SetTimeout (+)
. Build ();
SYSTEM.OUT.PRINTLN (message);
2.
message = Siamessage.custom ().
Setbusinesscode ("Yrd").
setmessage ("Hello World")
. SetTimeout (1000)
. Build ();
SYSTEM.OUT.PRINTLN (message);
}
}
the complex builder
Builder nesting
Package Com.ricky.builder.ch3; /** * ${description} * * @author Ricky Fung * @create 2016-07-06 15:38 * * public class Car {private Final String manufacturer; Manufacturer private final String brand; Brand private final String model; Model private final String Producingarea; Origin private final String producingdate;
Production time private final engine engine;
Private final Wheel Wheel;
Private final light light; Car (string manufacturer, string brand, string model, String Producingarea, String producingdate, Engine engine, Wh
Eel Wheel, Light light) {this.manufacturer = manufacturer;
This.brand = brand;
This.model = model;
This.producingarea = Producingarea;
This.producingdate = producingdate;
This.engine = engine;
This.wheel = wheel;
This.light = light; } public static class builder{private String manufacturer; Manufacturer Private String brand;
Brand Private String model; Model Private String Producingarea; Origin private String producingdate;
Production time private engine engine;
Private Wheel Wheel;
Private light light;
Public Builder (string manufacturer, string brand, String model) {this.manufacturer = manufacturer;
This.brand = brand;
This.model = model;
Public Builder Producingarea (String producingarea) {this.producingarea = Producingarea;
return this;
Public Builder producingdate (String producingdate) {this.producingdate = producingdate;
return this;
Public Builder engine (engine engine) {this.engine = engine;
return this;
Public Builder Wheel (wheel wheel) {This.wheel = wheel;
return this;
"Public Builder light" {this.light = light; return this; Public car Build () {return new car (manufacturer, brand, model, Producingarea, producingdate, Engin
E, wheel, light);
}
}
}
Package Com.ricky.builder.ch3; /** * Car engine * * @author Ricky Fung * @create 2016-07-06 15:42 * * public class Engine {private final String pl; Displacement private final String maxoutputpower; Maximum output power private final int rpm;
RPM engine (string pl, string maxoutputpower, int rpm) {this.pl = pl;
This.maxoutputpower = Maxoutputpower;
this.rpm = RPM;
} public static Builder custom () {return new builder (); } public static class builder{private String pl; Displacement private String maxoutputpower; Maximum output power private int rpm;
RPM Public Builder Pl (String pl) {this.pl = pl;
return this;
Public Builder Maxoutputpower (String maxoutputpower) {this.maxoutputpower = Maxoutputpower;
return this;
Public Builder rpm (int rpm) {this.rpm = RPM;
return this;
} public Engine build () { return new Engine (PL, maxoutputpower, RPM);
}
}
}
package Com.ricky.builder.ch3;
/** * Car Wheels * * @author Ricky Fung * @create 2016-07-06 15:40 * * public class Wheel {private String brand;
Private String producingdate;
Wheel (string brand, String producingdate) {This.brand = brand;
This.producingdate = producingdate;
} public static Builder custom () {return new builder ();
} public static class builder{private String brand;
Private String producingdate;
Public Builder brand (String brand) {This.brand = brand;
return this;
Public Builder producingdate (String producingdate) {this.producingdate = producingdate;
return this;
Public Wheel Build () {return new Wheel (brand, producingdate); }
}
}
Package Com.ricky.builder.ch3;
/** * Lights * * @author Ricky Fung * @create 2016-07-06 15:43 * * public class Light {private String brand; private String structure;
Structure private String producingdate;
Light (string brand, string structure, string producingdate) {This.brand = brand;
This.structure = structure;
This.producingdate = producingdate;
} public static Builder custom () {return new builder ();
} public static class builder{private String brand; private String structure;
Structure private String producingdate;
Public Builder brand (String brand) {This.brand = brand;
return this;
Public Builder structure (String structure) {this.structure = structure;
return this;
Public Builder producingdate (String producingdate) {this.producingdate = producingdate; return thi