Builder Pattern Introduction

Builder Pattern Introduction

Welcome back! So far, we've covered various creational design patterns like the Singleton Pattern, Factory Method Pattern, and Abstract Factory Pattern. These patterns have helped you control and simplify object creation in your programs. Today, we are delving into another powerful creational pattern — the Builder Pattern. This pattern allows you to construct complex objects step by step, making the creation process more manageable and modular.

What You'll Learn

In this lesson, you will learn how to implement the Builder Pattern in C++. Specifically, you'll understand how to:

  1. Define the Builder Pattern: Recognize the core components and when to use this pattern.
  2. Implement a Concrete Builder: See how to create concrete builders for constructing complex objects.
  3. Use a Director: Use a Director class to manage the construction process.

Here's a snippet of the code you'll work with:

The House.hpp file to define the House class — the product being constructed:

#include <iostream>
#include <string>

// Product House that will be constructed using builder pattern
class House {
public:
    // Setters for the house properties - foundation, structure, and roof
    void setFoundation(const std::string& foundation) { this->foundation = foundation; }
    void setStructure(const std::string& structure) { this->structure = structure; }
    void setRoof(const std::string& roof) { this->roof = roof; }

    // Display the house properties
    void showHouse() {
        std::cout << "House with " << foundation << ", " << structure << ", and " << roof << "." << std::endl;
    }
private:
    std::string foundation;
    std::string structure;
    std::string roof;
};

The HouseBuilder.hpp file to define the HouseBuilder interface:

// Abstract class for HouseBuilder with methods to build foundation, structure, and roof
class HouseBuilder {
public:
    virtual void buildFoundation() = 0;
    virtual void buildStructure() = 0;
    virtual void buildRoof() = 0;
    virtual House* getHouse() = 0;
};

The ConcreteHouseBuilder.hpp file to implement the HouseBuilder interface:

// Concrete builder class for constructing a house with concrete foundation, structure, and roof
class ConcreteHouseBuilder : public HouseBuilder {
public:
    ConcreteHouseBuilder() { house = new House(); }
    ~ConcreteHouseBuilder() { delete house; }
    void buildFoundation() override {
        house->setFoundation("Concrete Foundation");
    }
    void buildStructure() override {
        house->setStructure("Concrete Structure");
    }
    void buildRoof() override {
        house->setRoof("Concrete Roof");
    }
    House* getHouse() override {
        return house;
    }

private:
    House* house;
};

The WoodenHouseBuilder.hpp to implement the HouseBuilder interface:

// Concrete builder class for constructing a house with wooden foundation, structure, and roof
class WoodenHouseBuilder : public HouseBuilder {
public:
    WoodenHouseBuilder() { house = new House(); }
    ~WoodenHouseBuilder() { delete house; }
    void buildFoundation() override {
        house->setFoundation("Wooden Foundation");
    }
    void buildStructure() override {
        house->setStructure("Wooden Structure");
    }
    void buildRoof() override {
        house->setRoof("Wooden Roof");
    }
    House* getHouse() override {
        return house;
    }

private:
    House* house;
};

The BrickHouseBuilder.hpp to implement the HouseBuilder interface:

// Concrete builder class for constructing a house with brick foundation, structure, and roof
class BrickHouseBuilder : public HouseBuilder {
public:
    BrickHouseBuilder() { house = new House(); }
    ~BrickHouseBuilder() { delete house; }
    void buildFoundation() override {
        house->setFoundation("Brick Foundation");
    }
    void buildStructure() override {
        house->setStructure("Brick Structure");
    }
    void buildRoof() override {
        house->setRoof("Brick Roof");
    }
    House* getHouse() override {
        return house;
    }

private:
    House* house;
};

The main.cpp file to demonstrate the Builder Pattern:

// Director class to manage the construction process by encapsulating the steps within the `constructHouse` method
class Director {
public:
    void setBuilder(HouseBuilder* builder) {
        this->builder = builder;
    }
    House* constructHouse() {
        builder->buildFoundation();
        builder->buildStructure();
        builder->buildRoof();
        return builder->getHouse();
    }
private:
    HouseBuilder* builder;
};
int main() {
    Director director; // Director to manage the construction process
    BrickHouseBuilder builder; // Concrete builder to build a brick house
    director.setBuilder(&builder); // Set the builder to construct a brick house
    House* house = director.constructHouse(); // Construct the house
    house->showHouse();
    return 0;
}

This example demonstrates how to use the Builder Pattern to create a House object step by step.

The Builder Pattern has the following components:

  • Product: The object being constructed. In this example, House is the product.
  • Builder: An abstract class that defines the steps for constructing the product. In this example, HouseBuilder is the builder.
  • Concrete Builder: A concrete class that implements the builder interface to construct the product. In this example, ConcreteHouseBuilder, WoodenHouseBuilder, and BrickHouseBuilder are concrete builders. In real-world scenarios the builder method (the constructHouse in this example) can be more complex and can have multiple steps and conditions to build the actual product, but for simplicity we just call the three methods to build the product.

The Director class manages the construction process by using a builder to create the product. In this example, the Director class sets the builder to BrickHouseBuilder and constructs a House object.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal