0

Builder Pattern

This pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the object�s representation. This way the construction process can be used to create different representations. The logic of this process is isolated from the actual steps used in creating the complex object so the process can be used again to create a different object from the same set of simple objects as the first one.

public class RequestObj {

	private String reportId;
	private String titile;
	private String date;

	public String getReportId() {
		return reportId;
	}

	public void setReportId(String reportId) {
		this.reportId = reportId;
	}

	public String getTitile() {
		return titile;
	}

	public void setTitile(String titile) {
		this.titile = titile;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}
}
public abstract class RequestBuilder {

	protected RequestObj json;

	public RequestObj getJson() {
		return json;
	}

	public void createNewJsonObj() {
		json = new RequestObj();
	}
	public abstract void buildId();

	public abstract void report();

	public abstract void date();

}
public class JsonObj extends RequestBuilder {

	@Override
	public void buildId() {
		// TODO Auto-generated method stub
		json.setReportId("123456");
		
	}

	@Override
	public void report() {
		// TODO Auto-generated method stub
		json.setTitile("test");	
	}
	@Override
	public void date() {
		// TODO Auto-generated method stub
		json.setDate("201010");
	}

}
class Waiter {
    private RequestBuilder builder;

    public void setBuilder(RequestBuilder pb) {
        builder = pb;
    }

    public RequestObj getJsonObj() {
        return builder.getJson();
    }

    public void constructJson() {
        builder.createNewJsonObj();
        builder.buildId();
        builder.report();
        builder.date();
    }
}
public class Application {

	public static void main(String[] args) {
        Waiter waiter = new Waiter();
        RequestBuilder builder = new JsonObj();

        waiter.setBuilder(builder);
        waiter.constructJson();

        RequestObj jsonObj = waiter.getJsonObj();
        System.out.println("Json Obj - " + jsonObj.getTitile());
    }
}

Toufiq Mahmud

Leave a Reply

Your email address will not be published.