Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Construction with Builder | Creational Patterns
C++ Design Patterns

bookConstruction with Builder

The Builder pattern is a creational design pattern that enables you to construct complex objects step by step, allowing you to separate the construction process from the object's representation. This pattern is particularly useful when an object requires numerous parameters or configurations, making direct construction via constructors cumbersome and error-prone. By using a builder, you can create objects with varying configurations without exposing the construction logic to the client, and you can reuse the same construction process for different representations.

Use cases for the Builder pattern frequently arise when an object must be assembled with a combination of optional and mandatory parts, or when you want to ensure that an object is always created in a consistent state. Consider constructing a car: you may want to allow clients to specify whether the car has a sunroof, navigation system, or sport package, while always requiring basic features like an engine and wheels.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940
#include <iostream> #include <string> #include <optional> class Car { public: std::string engine, wheels; std::optional<std::string> sunroof, nav, sport; void show() const { std::cout << "Car: " << engine << ", " << wheels; if (sunroof) std::cout << ", Sunroof: " << *sunroof; if (nav) std::cout << ", Navigation: " << *nav; if (sport) std::cout << ", Sports: " << *sport; std::cout << "\n"; } }; class CarBuilder { Car car; public: CarBuilder& engine(const std::string& e) { car.engine = e; return *this; } CarBuilder& wheels(const std::string& w) { car.wheels = w; return *this; } CarBuilder& sunroof(const std::string& s) { car.sunroof = s; return *this; } CarBuilder& nav(const std::string& n) { car.nav = n; return *this; } CarBuilder& sport(const std::string& s) { car.sport = s; return *this; } Car build() { return car; } }; int main() { Car c1 = CarBuilder().engine("V4").wheels("Standard").build(); Car c2 = CarBuilder().engine("V8").wheels("Alloy").sunroof("Panoramic").nav("GPS Pro").sport("Plus").build(); Car c3 = CarBuilder().engine("V6").wheels("Sport").nav("Basic").build(); c1.show(); c2.show(); c3.show(); }

The Builder pattern improves code readability and flexibility by letting you construct objects using a clear, fluent interface. Rather than passing a long list of parameters to a constructor, you can chain builder methods to specify only the features you want. This approach also makes it easier to add new features to your objects in the future, since you can extend the builder with new methods without breaking existing code. The separation of construction and representation keeps your code organized and maintainable, especially as the complexity of the objects you need to build increases.

question mark

What is the primary purpose and benefit of using the Builder pattern in C++ design?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 10

bookConstruction with Builder

Sveip for å vise menyen

The Builder pattern is a creational design pattern that enables you to construct complex objects step by step, allowing you to separate the construction process from the object's representation. This pattern is particularly useful when an object requires numerous parameters or configurations, making direct construction via constructors cumbersome and error-prone. By using a builder, you can create objects with varying configurations without exposing the construction logic to the client, and you can reuse the same construction process for different representations.

Use cases for the Builder pattern frequently arise when an object must be assembled with a combination of optional and mandatory parts, or when you want to ensure that an object is always created in a consistent state. Consider constructing a car: you may want to allow clients to specify whether the car has a sunroof, navigation system, or sport package, while always requiring basic features like an engine and wheels.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940
#include <iostream> #include <string> #include <optional> class Car { public: std::string engine, wheels; std::optional<std::string> sunroof, nav, sport; void show() const { std::cout << "Car: " << engine << ", " << wheels; if (sunroof) std::cout << ", Sunroof: " << *sunroof; if (nav) std::cout << ", Navigation: " << *nav; if (sport) std::cout << ", Sports: " << *sport; std::cout << "\n"; } }; class CarBuilder { Car car; public: CarBuilder& engine(const std::string& e) { car.engine = e; return *this; } CarBuilder& wheels(const std::string& w) { car.wheels = w; return *this; } CarBuilder& sunroof(const std::string& s) { car.sunroof = s; return *this; } CarBuilder& nav(const std::string& n) { car.nav = n; return *this; } CarBuilder& sport(const std::string& s) { car.sport = s; return *this; } Car build() { return car; } }; int main() { Car c1 = CarBuilder().engine("V4").wheels("Standard").build(); Car c2 = CarBuilder().engine("V8").wheels("Alloy").sunroof("Panoramic").nav("GPS Pro").sport("Plus").build(); Car c3 = CarBuilder().engine("V6").wheels("Sport").nav("Basic").build(); c1.show(); c2.show(); c3.show(); }

The Builder pattern improves code readability and flexibility by letting you construct objects using a clear, fluent interface. Rather than passing a long list of parameters to a constructor, you can chain builder methods to specify only the features you want. This approach also makes it easier to add new features to your objects in the future, since you can extend the builder with new methods without breaking existing code. The separation of construction and representation keeps your code organized and maintainable, especially as the complexity of the objects you need to build increases.

question mark

What is the primary purpose and benefit of using the Builder pattern in C++ design?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 3
some-alt