Generate a C++ code for builder design pattern
- User
Generate a C++ code for builder design pattern
- Assistant
Here is an example of implementing the Builder design pattern in C++:
#include <iostream> #include <string> // Product class class Pizza { private: std::string dough; std::string sauce; std::string topping; public: void setDough(const std::string& dough) { this->dough = dough; } void setSauce(const std::string& sauce) { this->sauce = sauce; } void setTopping(const std::string& topping) { this->topping = topping; } void describe() { std::cout << "Pizza with dough: " << dough << ", sauce: " << sauce << ", and topping: " << topping << std::endl; } }; // Abstract builder class PizzaBuilder { protected: Pizza* pizza; public: Pizza* getPizza() { return this->pizza; } void createNewPizza() { this->pizza = new Pizza(); } virtual void buildDough() = 0; virtual void buildSauce() = 0; virtual void buildTopping() = 0; }; // Concrete builder 1 class MargheritaPizzaBuilder : public PizzaBuilder { public: void buildDough() override { pizza->setDough("regular"); } void buildSauce() override { pizza->setSauce("tomato"); } void buildTopping() override { pizza->setTopping("cheese"); } }; // Concrete builder 2 class PepperoniPizzaBuilder : public PizzaBuilder { public: void buildDough() override { pizza->setDough("thin crust"); } void buildSauce() override { pizza->setSauce("spicy"); } void buildTopping() override { pizza->setTopping("pepperoni"); } }; // Director class PizzaDirector { private: PizzaBuilder* builder; public: void setBuilder(PizzaBuilder* builder) { this->builder = builder; } Pizza* getPizza() { return builder->getPizza(); } void constructPizza() { builder->createNewPizza(); builder->buildDough(); builder->buildSauce(); builder->buildTopping(); } }; int main() { PizzaDirector director; MargheritaPizzaBuilder margheritaBuilder; PepperoniPizzaBuilder pepperoniBuilder; director.setBuilder(&margheritaBuilder); director.constructPizza(); Pizza* margherita = director.getPizza(); margherita->describe(); director.setBuilder(&pepperoniBuilder); director.constructPizza(); Pizza* pepperoni = director.getPizza(); pepperoni->describe(); delete margherita; delete pepperoni; return 0; }In this code, we have a
Pizzaclass as the product that the builder constructs. ThePizzaclass has setter methods to customize its attributes like dough, sauce, and topping.The
PizzaBuilderis an abstract base class that defines the interface for creating a pizza. It has pure virtual methods for building the dough, sauce, and topping. Each concrete builder (e.g.,MargheritaPizzaBuilder,PepperoniPizzaBuilder) implements these methods to create different types of pizzas.The
PizzaDirectorclass is responsible for using a builder to construct a pizza. It has asetBuildermethod to set the desired builder and aconstructPizzamethod to execute the builder's methods in the proper order. Finally, thegetPizzamethod is used to retrieve the constructed pizza.In the
mainfunction, we create aPizzaDirectorand two specific builders (MargheritaPizzaBuilder,PepperoniPizzaBuilder). We set the director's builder to the desired builder and callconstructPizzato build the pizza. Finally, we retrieve the constructed pizza usinggetPizzaand call thedescribemethod to print its attributes. - User
Generate a C++ code for singleton design pattern using C++11 or later version.
- Assistant
Here is an example of implementing the Singleton design pattern in C++11 or later versions:
#include <iostream> class Singleton { private: static Singleton* instance; // Private constructor to prevent direct instantiation Singleton() {} public: // Static method to retrieve the singleton instance static Singleton* getInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; } // Public method void showMessage() { std::cout << "Hello, I am the singleton instance!" << std::endl; } }; Singleton* Singleton::instance = nullptr; int main() { // Singleton instance retrieved Singleton* singleton = Singleton::getInstance(); // Call public methods singleton->showMessage(); return 0; }In this code, the
Singletonclass has a private constructor to prevent direct instantiation of the class. The static member variableinstanceholds the single instance of the class.The
getInstanceis a static method that ensures only one instance of the class exists. It checks if theinstancevariable isnullptrand creates a new object if it is. If an instance already exists, it returns the existing instance.In the
mainfunction, we retrieve the singleton instance usinggetInstancemethod and call its public methodshowMessageto showcase the functionality of the singleton.