Skip to content

Builder Design Pattern

Posted on:September 4, 2022 at 01:13 PM

In the realm of software design, patterns play a pivotal role in solving recurring problems efficiently. One such pattern that stands out for its utility and prevalence is the Builder Design Pattern. This pattern comes into play when we need to handle multiple parameters during the initialization of an object.

Imagine dealing with an object that requires a multitude of parameters, say five, ten, or even twenty. Constructing traditional constructors for all possible parameter combinations becomes impractical and leads to an explosion of constructors. For instance, with five parameters, we would need 120 constructors, and the complexity grows exponentially with more parameters.

The Builder Design Pattern addresses this issue by providing an elegant solution. It excels when dealing with optional parameters, allowing flexibility in initialization based on specific requirements.

Table of contents

Open Table of contents

Sections

The Core Idea

The crux of the Builder Design Pattern lies in streamlining the construction of objects with numerous parameters. Rather than burdening the client code with an overwhelming array of constructors, the pattern offers a cleaner approach. By segregating parameter setting through a builder, we simplify object creation, making the client code concise and comprehensible.

Dealing with Complexity

While the Builder Design Pattern proves remarkably effective, it is important to acknowledge that it can introduce verbosity due to the need for boilerplate code. However, the benefits of improved readability and efficient parameter handling outweigh this trade-off. Once the initial setup is in place, using the pattern becomes seamless, allowing for concise client code.

Building a Case: The Apartment Example

To illustrate the implementation of the Builder Design Pattern, let’s consider an ‘Apartment’ class. Apartments can vary in size, rooms, location, and amenities, making them an ideal candidate for showcasing this pattern.

We start by defining our Apartment class with its five main parameters: square meters (sqm), number of rooms (rooms), city, area within the city, and a boolean indicating whether the apartment has a kitchen.

// [Apartment.java]
public class Apartment {
    private int sqm;
    private int rooms;
    private String city;
    private String area;
    private boolean hasKitchen;

    public Apartment(int sqm, int rooms, String city, String area, boolean hasKitchen) {
        this.sqm = sqm;
        this.rooms = rooms;
        this.city = city;
        this.area = area;
        this.hasKitchen = hasKitchen;
    }

    // Getters for parameters (sqm, rooms, city, area, hasKitchen)

    public void display() {
        // Display apartment information
    }
}

In contrast to traditional constructors, the Builder pattern offers a more flexible and readable way to construct Apartment objects.

// [ApartmentBuilder.java]
public class ApartmentBuilder {
    private int sqm = -1;
    private int rooms = -1;
    private String city = "";
    private String area = "";
    private boolean hasKitchen = false;

    public ApartmentBuilder setSqm(int sqm) {
        this.sqm = sqm;
        return this;
    }

    public ApartmentBuilder setRooms(int rooms) {
        this.rooms = rooms;
        return this;
    }

    public ApartmentBuilder setCity(String city) {
        this.city = city;
        return this;
    }

    public ApartmentBuilder setArea(String area) {
        this.area = area;
        return this;
    }

    public ApartmentBuilder setHasKitchen(boolean hasKitchen) {
        this.hasKitchen = hasKitchen;
        return this;
    }

    public Apartment build() {
        Apartment apartment = new Apartment(sqm, rooms, city, area, hasKitchen);

        // Reset parameters for the next build
        sqm = -1;
        rooms = -1;
        city = "";
        area = "";
        hasKitchen = false;

        return apartment;
    }
}

To make the most of the Builder Design Pattern, we employ a Client class to showcase its capabilities by creating instances of apartments with varying parameters.

// [Client.java]
public class Client {
    public static void main(String[] args) {
        ApartmentBuilder builder = new ApartmentBuilder();

        // Build and display the first apartment
        Apartment ap1 = builder.setSqm(25)
                                .setRooms(2)
                                .setCity("London")
                                .setArea("Kensington")
                                .setHasKitchen(true)
                                .build();
        ap1.display();

        // Build and display the second apartment with minimal parameters
        Apartment ap2 = builder.setRooms(3)
                                .setCity("Paris")
                                .build();
        ap2.display();
    }
}

Conclusion

The Builder Design Pattern provides an elegant solution to handle complex object creation, especially when dealing with multiple parameters and optional configurations. By allowing for a more readable and flexible approach to object initialization, it helps maintain clean and maintainable code.

In summary, the Builder Design Pattern is a valuable tool in a developer’s toolkit, enabling efficient object creation and promoting code clarity.