Builder Pattern
The builder creational pattern is used to make a complex object from the simple objects step by step. the builder pattern is similar to the abstract factory pattern because it is also return to the group of the related object. only in both different is that the builder is make the complex object with help of simple objects.
Use of the Builder
- Make a complex object by type and content.
-
If we want to decouple the process of building a complex object from the parts that make up the object then we can do.
-
It is Isolate code for construction and representation.
-
Control over the construction process.
Example
package r4r;
import java.util.*;
import java.util.Collection;
class Shop {
public void construct(HomeBuilder hb) {
hb.Foundationbuilder();
hb.Framebuilder();
hb.Exteriorbuilder();
hb.Interiorbuilder();
}
}
abstract class HomeBuilder {
protected Home house = new Home();
protected String showProgress() {
return house.toString();
}
abstract public void Foundationbuilder();
abstract public void Framebuilder();
abstract public void Exteriorbuilder();
abstract public void Interiorbuilder();
}
class singlefloar extends HomeBuilder {
public singlefloar(String features) {
house.setType(this.getClass() + " " + features);
}
public void Foundationbuilder() {
house.setProgress("foundation is done");
}
public void Framebuilder() {
house.setProgress("operation of frame is done");
}
public void buildExterior() {
house.setProgress("operation Exterior building is going on");
}
public void Interiorbuilder() {
house.setProgress("Interior is under construction");
}
@Override
public void Exteriorbuilder() {
// TODO Auto-generated method stub
}
}
class Doublefloar extends HomeBuilder {
public Doublefloar(String features) {
house.setType(this.getClass() + " " + features);
}
public void Foundationbuilder() {
house.setProgress("foundation is done");
}
public void Framebuilder() {
house.setProgress("construction of frame is done");
}
public void Exteriorbuilder() {
house.setProgress("Exterior is waiting to start");
}
public void Interiorbuilder() {
house.setProgress("Interior is also waiting process");
}
}
class Home {
private String type = null;
private List features = new ArrayList();
public Home() {
}
public Home(String type) {
this.type = type;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setProgress(String s) {
features.add(s);
}
public String toString() {
StringBuffer sb = new StringBuffer();
String t = type.substring(6);
sb.append(t + "\n ");
for (int i = 0; i < features.size(); i ++) {
sb.append(features.get(i) + "\n ");
}
return sb.toString();
}
}
public class buildertests {
public static void main(String[] args) {
HomeBuilder hb1 = new singlefloar("2 bedrooms, 2 baths, 1-car garage, 2000 sqft");
HomeBuilder hb2 = new Doublefloar("3 bedrooms, 3 baths, 2-car garage, 5000 sqft");
Shop shop = new Shop();
shop.construct(hb1);
shop.construct(hb2);
System.out.println("Check house building progress: \n");
System.out.println(hb1.showProgress());
System.out.println(hb2.showProgress());
}
}