A Look at the Builder Pattern
See some examples of the Builder pattern in action in order to create complicated objects with optional inputs.
Join the DZone community and get the full member experience.
Join For FreeBuilder is a part of the Creational pattern family, which is useful for creating complex objects with a lot of options — where all of them aren't necessary. In short, it's great when we need customized objects with optional inputs.
Let's take a real world example to understand it. A Subway shop is well-known for its customized sandwiches — as per the user inputs. The bread is the base of a sandwich, and toppings get added based on the user. So, as per the user inputs, Subway is customizing their sandwiches. That is exactly what we do when we use the builder pattern to build an object.
Let's look at implementing the pattern in Java to build the reports; as reports are something which need high level of customization.
IBuilder.java
public interface IBuilder {
public void buildPreface();
public void buildReportTitle();
public void buildHeader();
public void buildContent();
public void buildFooter();
public Report getReport();
}
Mybuilder.java
public class MyBuilder implements IBuilder{
private Report report;
public MyBuilder(){
report = new Report();
}
@Override
public Report getReport(){
return report;
}
@Override
public void buildContent() {
report.setContent("my 1st report............");
}
@Override
public void buildFooter() {
report.setFooter("copyright softwareArchitecture&DesignWithArun.com : 2014");
}
@Override
public void buildHeader() {
report.setHeader(new String[]{"header_1", "header_2"});
}
@Override
public void buildPreface() {
report.setPreface("Report using Builder by : "+System.getProperty("user.info"));
}
@Override
public void buildReportTitle() {
report.setReportTitle("My Report");
}
}
ReportDirector.java
public class ReportDirector {
private IBuilder myBuilder;
public ReportDirector(){
myBuilder = new MyBuilder();
}
public void buildReport(){
// report building logic/steps
myBuilder.buildReportTitle();
myBuilder.buildHeader();
myBuilder.buildPreface();
myBuilder.buildContent();
myBuilder.buildFooter();
}
public Report getReport(){
return this.myBuilder.getReport();
}
}
Report.java
public class Report {
private String reportTitle;
private String header[];
private String preface;
private String content;
private String footer;
public String getReportTitle() {
return reportTitle;
}
public void setReportTitle(String reportTitle) {
this.reportTitle = reportTitle;
}
public String[] getHeader() {
return header;
}
public void setHeader(String[] header) {
this.header = header;
}
public String getPreface() {
return preface;
}
public void setPreface(String preface) {
this.preface = preface;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getFooter() {
return footer;
}
public void setFooter(String footer) {
this.footer = footer;
}
}
Client.java
public class Client {
public static void main(String[] args) {
ReportDirector reportDirector = new ReportDirector();
reportDirector.buildReport();
Report report = reportDirector.getReport();
//use report object as per business
System.out.println(report.getContent());
}
}
And there you are. You've seen the builder pattern used to help create complex objects, but whose user inputs aren't all required. Now that you've seen it in action, go out and grab some Subway.
Published at DZone with permission of Arun Pandey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments