Builder Design Pattern in Java

Builder pattern is a pattern in which different representations are created for the same object.It is used for creation of complex Objects.

Since it deals with the creation of objects, it is an creational design pattern.

Let us look at the building blocks of thsi pattern.

1)Product : This is the final product or you can say the object which would be returned as the output having a particular representation. Remember the object in scope of the Builder Pattern belongs to a Single Class but yes the representations of those objects may be different.

2)Abstract Builder: It provides the steps or methods which would be needed to create the object.

3)Builder Classes: These are the classes which would build the object extending the abstract builder.

4)Executor: Executor is the one which controls the algorithm which generates the final product. It accepts the input, invokes the appropriate builder class and gets the final product returned by the builder.

Let’s look at the implementation.

//Product
class Coffee 
{ 
  
    private String name; 
    private String grindFactor; 
    private String taste; 
    private String Equipment; 
  
    public void setName(String name)  
    { 
        this.name = name; 
    } 
  
    public void setGrindFactor(String grindFactor)  
    { 
        this.grindFactor = grindFactor; 
    } 
  
    public void setTaste(String taste)  
    { 
        this.taste = taste; 
    } 
  
    public void setEquipment(String equipment)  
    { 
        this.equipment = equipment; 
    } 
  
} 

//Abstract Builder 
interface CoffeeBuilder 
{
  
    public void setName(); 
  
    public void addFlavour(); 
  
    public void addGrindFactor();

    public void addEquipment();  

    public Coffee getCoffee(); 

} 

//Builder Classes
class FilterCoffeeBuilder implements CoffeeBuilder 
{ 
    private Coffee coffee; 
  
    public FilterCoffeeBuilder()  
    { 
        this.coffee = new Coffee(); 
    } 
  
    public void setName()  
    { 
        coffee.setName("Filter"); 
    } 
  
    public void addFlavour()  
    { 
        coffee.setTaste("Sweet"); 
    } 
  
    public void addGrindFactor()  
    { 
        coffee.setGrindFactor("Less"); 
    } 
  
    public void addEquipment()  
    { 
        coffee.setEquipment("Manual"); 
    } 
  
    public Coffee getCoffee()  
    { 
        return this.coffee; 
    } 
}

class EspressoCoffeeBuilder implements CoffeeBuilder 
{ 
    private Coffee coffee; 
  
    public FilterCoffeeBuilder()  
    { 
        this.coffee = new Coffee(); 
    } 
  
    public void setName()  
    { 
        coffee.setName("Espresso"); 
    } 
  
    public void addFlavour()  
    { 
        coffee.setTaste("Bitter"); 
    } 
  
    public void addGrindFactor()  
    { 
        coffee.setGrindFactor("More"); 
    } 
  
    public void addEquipment()  
    { 
        coffee.setEquipment("Espresso Machine"); 
    } 
  
    public Coffee getCoffee()  
    { 
        return this.coffee; 
    } 
}

As you see above, the product to be created or you can say the object to be created with a particular representation here is a Coffee. Same is represented as a Coffee Class.

Then we create 2 builder classes which are two representations of Coffee i.e; Filter and Espresso. Both the classes have the properties to represent each of the coffee types.

Now let’s design the Coffee Order Executor.


class CoffeeExecutor  
{ 
  
    private CoffeeBuilder coffeeBuilder; 
  
    public CoffeeEexecutor(CoffeeBuilder coffeeBuilder) 
    { 
        this.coffeeBuilder = coffeeBuilder; 
    } 
  
    public Coffee getCoffee() 
    { 
        return this.coffeeBuilder.getCoffee(); 
    } 
  
    public void constructCoffee() 
    { 
        this.coffeeBuilder.setName(); 
        this.coffeeBuilder.addFlavour(); 
        this.coffeeBuilder.addGrindFactor(); 
        this.coffeeBuilder.addEquipment(); 
    } 
} 

The above is the Executor class which triggers the builder processes and creates the objects.

Let’s run the program then.

class CoffeeMachine 
{ 
    public static void main(String[] args) 
    { 
        CoffeeBuilder espressoBuilder = new EspressoCoffeeBuilder(); 
        espressoBuilder executor = new espressoBuilder(espressoBuilder); 
  
        executor.constructCoffee(); 
  
        Coffee coffee = executor.getCoffee(); 
  
        System.out.println("Coffee is built: "+ coffee); 
    } 
} 

This is how the Builder Pattern works. Hope it was clear.