Builder Design Pattern

Builder  Design Pattern Approch
Construct a complex object from simple objects using step-by-step approach.

When would you use the Builder Pattern?

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.



Builder Design Pattern in Java

Let’s see how we can implement builder design pattern in java.
  1. First of all you need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention and if the class name is Computerthen builder class should be named as ComputerBuilder.
  2. Java Builder class should have a public constructor with all the required attributes as parameters.
  3. Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute.
  4. The final step is to provide a build() method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument.

 Required Parameters
1. Tea - String (Name of Tea)

 Optional Parameters

1. Cigarettes - String (Name of Cigarette)
2. Cookies - Boolean (if yes he will provide 1 packet of cookies) 



package tutorials.interviewbubble.creationaldesignpattern;

public class TeaStall {

// Required Parameter
private String tea;
// Optional Parameter
private String Cigarette;
private boolean cookies = false;
private TeaStall(TeaStallBuilder tsb)
{
  this.tea = tsb.tea;
  this.Cigarette = tsb.Cigarette;
  this.cookies = tsb.cookies;
}
public String getTea() {
return tea;
}

public String getCigarette() {
return Cigarette;
}

public boolean isCookies() {
return cookies;
}
public static class TeaStallBuilder{ // here tea stall builder is waiter
// Required Parameter
private String tea;
// Optional Parameter
private String Cigarette;
private boolean cookies = false;
public TeaStallBuilder(String tea) {
this.tea = tea;
}
public TeaStallBuilder setCigarette(String cigarette) {
Cigarette = cigarette;
return this;
}


public TeaStallBuilder setCookies(boolean cookies) {
this.cookies = cookies;
return this;
}

public TeaStallBuilder setTea(String tea) {
this.tea = tea;
return this;
}
public TeaStall build() {
return new TeaStall(this);
}
}
}


/**
 * 
 */
package tutorials.interviewbubble.creationaldesignpattern;

/**
 * @author interviewbubble
 *
 */
public class BuilderDesignPattern {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TeaStall order = new TeaStall.TeaStallBuilder("Black Tea").setCigarette("GOLD FLAKE KINGS").setCookies(false).build();
        System.out.println("Your Order is Successfully Placed. No one can change your order not even me");
        System.out.println("Your Order is: " + "\nTea Type: " + order.getTea() + "\nCigarette Type: " + order.getCigarette() + "\nDo you need 1 Cookie Pack: " + order.isCookies());
    }

}





Comments