builder_pattern

namespace builder_pattern

Overloads the output stream operator for the Product class.

Namespace for the Builder pattern.

Allows the details of the Product to be printed to an output stream.

Param os:

The output stream to write to.

Param product:

The product whose details are printed.

Return:

The output stream with appended product details.

Functions

std::ostream &operator<<(std::ostream &os, const Product &product)

Allows the details of the Product to be printed to an output stream.

Parameters:
  • os – The output stream.

  • product – The product to be printed.

Returns:

The output stream with product details appended.

class Builder
#include <builder.hpp>

Abstract interface for constructing a Product.

The Builder interface defines the steps required to build different parts of a product and retrieve the final result. Concrete implementations of this interface construct specific types of products.

Subclassed by builder_pattern::ConcreteBuilder

Public Functions

virtual ~Builder() = default

Virtual destructor to allow proper cleanup in derived classes.

virtual void buildPartA() = 0

Builds Part A of the product.

Defines the step required to construct Part A of the product.

virtual void buildPartB() = 0

Builds Part B of the product.

Defines the step required to construct Part B of the product.

virtual std::shared_ptr<Product> getResult() = 0

Retrieves the constructed product.

After all parts of the product are built, this method returns the fully constructed product.

Returns:

A shared pointer to the constructed Product.

class ConcreteBuilder : public builder_pattern::Builder
#include <builder.hpp>

Concrete implementation of the Builder interface.

The ConcreteBuilder class constructs a specific type of product by implementing the steps defined in the Builder interface.

Public Functions

ConcreteBuilder()

Constructor initializes a new Product instance.

Constructs a new ConcreteBuilder and initializes a product.

Creates a new Product instance and initializes the builder.

Initializes the builder with a fresh Product instance for construction.

virtual void buildPartA() override

Builds Part A of the product.

Builds Part A of the product.

Defines the step required to construct Part A of the product.

Sets Part A of the product to a specific value defined by the builder.

virtual void buildPartB() override

Builds Part B of the product.

Builds Part B of the product.

Defines the step required to construct Part B of the product.

Sets Part B of the product to a specific value defined by the builder.

virtual std::shared_ptr<Product> getResult() override

Retrieves the constructed product.

Retrieves the constructed product.

After all parts of the product are built, this method returns the fully constructed product.

Returns the fully constructed product after all parts have been built.

Returns:

A shared pointer to the constructed Product.

Returns:

A shared pointer to the constructed Product.

class Director
#include <builder.hpp>

Orchestrates the construction process.

The Director class uses a Builder to construct a product in a predefined sequence. It ensures that the construction process is consistent and follows a specific order.

Public Functions

explicit Director(Builder *builder)

Constructor accepts a builder.

Initializes the Director with a specific builder to use for the construction process.

Parameters:

builder – A pointer to the Builder instance.

void construct()

Constructs the product by invoking the builder’s methods in order.

Constructs the product by invoking the builder’s methods in sequence.

Executes the sequence of steps to construct the product using the builder.

Orchestrates the construction process by calling the builder’s methods in a predefined order.

std::shared_ptr<Product> getProduct()

Retrieves the constructed product.

Retrieves the constructed product from the builder.

After the construction process is complete, this method retrieves the fully constructed product from the builder.

After the construction process is complete, this method retrieves the final constructed product from the builder.

Returns:

A shared pointer to the constructed Product.

Returns:

A shared pointer to the constructed Product.

class Product
#include <builder.hpp>

Represents the complex product being built.

The Product class contains components partA and partB which represent the individual parts of the product. It provides methods to set its parts and to display the details of the constructed product.

Public Functions

void setPartA(const std::string &part)

Sets Part A of the product.

Parameters:

part – A string representing Part A.

void setPartB(const std::string &part)

Sets Part B of the product.

Parameters:

part – A string representing Part B.

std::string toString() const

Provides a string representation of the product.

Combines details of Part A and Part B into a formatted string representation of the product.

Combines the details of Part A and Part B into a formatted string that describes the constructed product.

Returns:

A string describing the product’s parts.

Returns:

A string representation of the product’s parts.

Friends

friend std::ostream &operator<<(std::ostream &os, const Product &product)

Overloads the output stream operator for the Product class.

Allows the details of the Product to be printed to an output stream.

Parameters:
  • os – The output stream.

  • product – The product to be printed.

Returns:

The output stream with product details appended.