Construction 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
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.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Can you give an example of the Builder pattern in code?
What are some real-world scenarios where the Builder pattern is especially useful?
How does the Builder pattern differ from other creational patterns like Factory or Prototype?
Awesome!
Completion rate improved to 10
Construction with Builder
Svep för att visa menyn
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
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.
Tack för dina kommentarer!