Builder Header
-
namespace builder_pattern
Overloads the output stream operator for the
Productclass.Namespace for the Builder pattern.
Allows the details of the
Productto 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.
-
class Builder
- #include <builder.hpp>
Abstract interface for constructing a Product.
The
Builderinterface 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 ~Builder() = default
-
class ConcreteBuilder : public builder_pattern::Builder
- #include <builder.hpp>
Concrete implementation of the Builder interface.
The
ConcreteBuilderclass constructs a specific type of product by implementing the steps defined in theBuilderinterface.Public Functions
-
ConcreteBuilder()
Constructor initializes a new
Productinstance.Constructs a new
ConcreteBuilderand initializes a product.Creates a new
Productinstance and initializes the builder.Initializes the builder with a fresh
Productinstance 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.
Private Members
-
std::shared_ptr<Product> product
The product being constructed.
-
ConcreteBuilder()
-
class Director
- #include <builder.hpp>
Orchestrates the construction process.
The
Directorclass uses aBuilderto 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
Directorwith a specific builder to use for the construction process.- Parameters:
builder – A pointer to the
Builderinstance.
-
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.
Private Members
-
Builder *builder
The builder used to construct the product.
-
explicit Director(Builder *builder)
-
class Product
- #include <builder.hpp>
Represents the complex product being built.
The
Productclass contains componentspartAandpartBwhich 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.
Private Members
-
std::string partA
Represents Part A of the product.
-
std::string partB
Represents Part B of the product.
Friends
-
friend std::ostream &operator<<(std::ostream &os, const Product &product)
Overloads the output stream operator for the
Productclass.Allows the details of the
Productto 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.
-
void setPartA(const std::string &part)