Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

Generate a C++ code for builder design pattern

  1. User

    Generate a C++ code for builder design pattern

  2. 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 Pizza class as the product that the builder constructs. The Pizza class has setter methods to customize its attributes like dough, sauce, and topping.

    The PizzaBuilder is 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 PizzaDirector class is responsible for using a builder to construct a pizza. It has a setBuilder method to set the desired builder and a constructPizza method to execute the builder's methods in the proper order. Finally, the getPizza method is used to retrieve the constructed pizza.

    In the main function, we create a PizzaDirector and two specific builders (MargheritaPizzaBuilder, PepperoniPizzaBuilder). We set the director's builder to the desired builder and call constructPizza to build the pizza. Finally, we retrieve the constructed pizza using getPizza and call the describe method to print its attributes.

  3. User

    Generate a C++ code for singleton design pattern using C++11 or later version.

  4. 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 Singleton class has a private constructor to prevent direct instantiation of the class. The static member variable instance holds the single instance of the class.

    The getInstance is a static method that ensures only one instance of the class exists. It checks if the instance variable is nullptr and creates a new object if it is. If an instance already exists, it returns the existing instance.

    In the main function, we retrieve the singleton instance using getInstance method and call its public method showMessage to showcase the functionality of the singleton.