Builder Design Patterns

Ahsan Majeed
3 min readJul 29, 2023

--

Builder design pattern is a creational design pattern that is used to construct complex objects step by step. It separates the construction of an object from its representation, allowing the same construction process to create different representations of an object. This pattern is particularly useful when an object has many optional or mandatory parameters and different configurations are required during its creation.

Advantages of the Builder design pattern:

1. Improved readability: The pattern makes the code more readable by giving descriptive method names for setting different attributes of the object being constructed.

2. Flexible object creation: It allows you to create complex objects with various configurations without modifying the underlying object class or adding numerous constructor overloads.

3. Encapsulated construction logic: The construction logic is contained within the builder class, keeping the main object’s class clean and less cluttered.

4. Reusability: Builders can be reused to create similar objects with different configurations, reducing code duplication.

Usage of the Builder design pattern:

The Builder pattern is commonly used in situations where:

1. The object being created has many optional or mandatory parameters.
2. There are multiple configurations or variations of the object.
3. The object creation process involves multiple steps or complex logic.

Let’s consider real-life example of the Builder design pattern for creating a MobilePhone object with various optional features

// MobilePhone class (the object we want to create)
public class MobilePhone {
private String brand;
private String model;
private int storage;
private int ram;
private boolean hasFrontCamera;
private boolean hasDualSim;
// other mobile phone features and properties

// Private constructor to prevent direct instantiation
private MobilePhone(Builder builder) {
this.brand = builder.brand;
this.model = builder.model;
this.storage = builder.storage;
this.ram = builder.ram;
this.hasFrontCamera = builder.hasFrontCamera;
this.hasDualSim = builder.hasDualSim;
// initialize other properties
}

// Nested Builder class
public static class Builder {
private String brand;
private String model;
private int storage;
private int ram;
private boolean hasFrontCamera;
private boolean hasDualSim;
// other mobile phone features and properties

public Builder(String brand, String model, int storage, int ram) {
this.brand = brand;
this.model = model;
this.storage = storage;
this.ram = ram;
}

public Builder hasFrontCamera(boolean hasFrontCamera) {
this.hasFrontCamera = hasFrontCamera;
return this;
}

public Builder hasDualSim(boolean hasDualSim) {
this.hasDualSim = hasDualSim;
return this;
}

// Methods for other mobile phone features and properties

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

// Usage of the Builder pattern to create a MobilePhone
public class MobilePhoneBuilderExample {
public static void main(String[] args) {
MobilePhone phone1 = new MobilePhone.Builder("Samsung", "Galaxy S21", 128, 8)
.hasFrontCamera(true)
.hasDualSim(true)
.build();

MobilePhone phone2 = new MobilePhone.Builder("Apple", "iPhone 13", 256, 6)
.hasFrontCamera(true)
.hasDualSim(false)
.build();

// Add more configurations and features as needed
}
}

In this example, we have a MobilePhone class that has a nested Builder class. The MobilePhone class represents a mobile phone with various features like brand, model, storage, RAM, and optional features like a front camera and dual SIM support. The Builder class provides methods to set different features and properties of the MobilePhone object, and the build() method constructs the MobilePhone object using the private constructor of MobilePhone.

By using the Builder pattern, we can create different configurations of MobilePhone objects with various optional features without having to create multiple constructors or modify the MobilePhone class every time we want to add or change optional attributes. The Builder pattern provides a clean and flexible way to construct complex objects with different combinations of features and properties, in this case, a customizable mobile phone with various specifications.

In conclusion, the Builder design pattern simplifies complex object creation, enhances code readability, and promotes flexibility in software engineering. Embrace its power to build sophisticated objects effortlessly in your projects. Happy coding!

--

--

Ahsan Majeed

My thoughts and world. Just want to keep sharing thoughts, experiments & new stuff.