Databases Reference
In-Depth Information
2.
Implement property methods that are used to set optional address
information. This information includes state and country. Each property
method returns a reference to the used builder.
3.
Implement a
build()
method that returns the build object.
The source code of the
Address.Builder
class is given as follows:
public static class Builder {
private Address built;
public Builder(String streetAddress, String postCode, String
postOffice) {
built = new Address();
built.streetAddress = streetAddress;
built.postCode = postCode;
built.postOffice = postOffice;
}
public Builder country(String country) {
built.country = country;
return this;
}
public Builder state(String state) {
built.state = state;
return this;
}
public Address build() {
return built;
}
}
We must also implement a method that is used to get a reference to the used
builder object. We can do this by simply creating a new
Address.Builder
object
and returning the created object. The source code of the static
getBuilder()
method
of the
Address
class is given as follows:
public static Builder getBuilder(String streetAddress, String
postCode, String postOffice) {
return new Builder(streetAddress, postCode, postOffice);
}