Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ set(APP_SOURCES
"src/patterns/behavioral/Observer.cpp"
"src/patterns/creational/Singleton.cpp"
"src/patterns/creational/FactoryMethod.cpp"
"src/patterns/creational/Builder.cpp"
)

# Test files
Expand Down
4 changes: 4 additions & 0 deletions docs/uml/patterns_creational_builder.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
213 changes: 213 additions & 0 deletions src/patterns/creational/Builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// cppcheck-suppress-file [functionStatic]

// Builder is a creational design pattern that lets you construct complex objects step by step.
// The pattern allows you to produce different types and representations of an object using the same construction code.
// Appicability:
// (*) Use the Builder pattern to get rid of a “telescoping constructor”.
// (**) when you want your code to be able to create different representations of some product (for example, stone and wooden houses).

// UML: docs/uml/patterns_behavioral_iterator.drawio.svg

#include <iostream>

#include <iostream>
#include <string>
#include <vector>
#include <memory>

namespace
{
namespace BuilderPattern
{
class Product
{
private:
std::vector<std::string> m_parts;

public:
void addPart(const std::string &part)
{
m_parts.push_back(part);
}

void print() const
{
std::cout << "Product parts: ";
for (size_t i = 0; i < m_parts.size(); ++i)
{
std::cout << m_parts[i];
if (i + 1 < m_parts.size())
std::cout << ", ";
}
std::cout << "\n\n";
}
};

/**
* The Builder interface specifies methods for creating the different parts of
* the Product objects.
*/
class IBuilder
{
public:
virtual ~IBuilder() = default;
virtual IBuilder &reset() = 0;
virtual IBuilder &producePart1() = 0;
virtual IBuilder &producePart2() = 0;
virtual IBuilder &producePart3() = 0;

virtual Product *const build() = 0;
};

class AbstractBuilder : public IBuilder
{
protected:
Product *m_product;

public:
explicit AbstractBuilder()
{
m_product = new Product();
}

~AbstractBuilder()
{
delete m_product;
}

AbstractBuilder(const AbstractBuilder &other)
{
if (m_product != nullptr)
{
delete m_product;
}
m_product = new Product();
*m_product = *other.m_product;
}

AbstractBuilder &operator=(const AbstractBuilder &other)
{
if (this == &other)
{
return *this;
}
if (m_product != nullptr)
{
delete m_product;
}
m_product = new Product();
*m_product = *other.m_product;
return *this;
}

IBuilder &reset() override final
{
if (m_product != nullptr)
{
delete m_product;
}
m_product = new Product();

return *this;
}
};

/**
* The Concrete Builder classes follow the Builder interface and provide
* specific implementations of the building steps. Your program may have several
* variations of Builders, implemented differently.
*/
class SimpleBuilder : public AbstractBuilder
{
public:
IBuilder &producePart1() override
{
m_product->addPart("PART1");
return *this;
}

IBuilder &producePart2() override
{
m_product->addPart("PART2");
return *this;
}

IBuilder &producePart3() override
{
m_product->addPart("PART3");
return *this;
}

Product *const build() override
{
return m_product;
}
};

class ComplexBuilder : public AbstractBuilder
{
public:
IBuilder &producePart1() override
{
m_product->addPart("PART_1-X9a7Fq!2@Lm#48Z");
return *this;
}

IBuilder &producePart2() override
{
m_product->addPart("PART_2-X9a7Fq!2@Lm#48Z");
return *this;
}

IBuilder &producePart3() override
{
m_product->addPart("PART_3-X9a7Fq!2@Lm#48Z");
return *this;
}

Product *const build() override
{
return m_product;
}
};

namespace Client
{
void clientCode(IBuilder *const builder)
{
const Product *product1 = (*builder).producePart1().producePart2().producePart3().build();
product1->print();

const Product *product2 = (*builder).reset().producePart1().build();
product2->print();
}
}

void run()
{
{
std::cout << "ConcreteBuilder: Simple\n";
IBuilder *builder = new SimpleBuilder();
Client::clientCode(builder);
delete builder;
}
{
std::cout << "ConcreteBuilder: Complex\n";
IBuilder *builder = new ComplexBuilder();
Client::clientCode(builder);
delete builder;
}
}
}
}

struct BuilderAutoRunner
{
BuilderAutoRunner()
{
std::cout << "\n--- Builder Pattern Example ---\n";
BuilderPattern::run();
}
};

static BuilderAutoRunner instance;