builder design pattern

The intent behind the Builder Design Pattern is to separate the construction of a complex object from its representation so that the same construction process can create different representations.
It’s a type of creational design pattern.

This is useful when we need to create objects that have multiple options or configuration settings, Instead of having a constructor with a long list of parameters that are difficult to manage and remember, the Builder pattern provides a more organized and flexible approach.

Using the Builder pattern allows you to create objects step by step, with each step adding more options or configurations. The final result is an object that is fully configured and ready to use.

Structure of the builder pattern

Builder design pattern UML — source wiki

In the above UML class diagram, the Director class doesn't create and assemble the ProductA1 and ProductB1 objects directly. Instead, the Director refers to the Builder interface for building (creating and assembling) the parts of a complex object, which makes the Director independent of which concrete classes are instantiated (which representation is created). The Builder1 class implements the Builder interface by creating and assembling the ProductA1 and ProductB1 objects.

Simple Example
In the below code, we are creating the Car object using the Builder pattern.
This is the method chaining builder pattern implementation where each method in the builder class returns the Builder instance.

public class Car {
private String model;
private String color;
private int year;

public static class Builder {
private String model;
private String color;
private int year;

public Builder() {}

public Builder setModel(String model) {
this.model = model;
return this;
}
public Builder setColor(String color) {
this.color = color;
return this;
}

public Builder setYear(int year) {
this.year = year;
return this;
}

public Car build() {
return new Car(this);
}
}

private Car(Builder builder) {
this.model = builder.model;
this.color = builder.color;
this.year = builder.year;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}
}

*****
package com.ecommercearchitect.designpatterns.builder.methodchainingexample;

public class BuilderMethodChainingExampleMain {
public static void main(String[] args) {
Car car = new Car.Builder().setModel("Honda Civic").setColor("Red").setYear(2021).build();
System.out.println(car.getModel()); // output: Honda Civic
System.out.println(car.getColor()); // output: Red
System.out.println(car.getYear()); // output: 2021
}
}

**** OUTPUT ****
Honda Civic
Red
2021

StringBuilder and StringBuffer classes in Java are used to implement the Builder pattern, allowing you to append strings and other data types to build a final string which is Immutable.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code examples then please consider checking the detailed article at [builder-pattern-explained-8-min-read]

Thanks for reading. Love IT, Live IT, Enjoy IT.

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 5–10 min post along with a code example, please consider following if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT