Builder mode Builder (c++ design mode)

Interviewer: Please talk about the builder mode

The builder mode is to separate the construction of a complex object from its representation , so that the same construction process can create different representations. The client does not need to know the internal components and assembly methods of the complex object , only the type of builder required. Can

Model advantages

  • The client does not need to know the details of the internal composition of the product, and decouples the product itself from the creation process, so that the same creation can create different product objects
  • For replacement and addition of new concrete builders, the principle of opening and closing is met
  • Can finely control the product creation process
  • Features: high cohesion and low coupling

Model disadvantages

  • The difference between products is very large and it is not suitable to use the builder model
  • If the internal changes of the product are complex, many specific builder classes need to be defined, which leads to high system overhead

Applicable environment

  • The product that needs to be generated has a complex internal structure
  • The attributes of the product objects that need to be generated depend on each other
  • The creation process of an object is independent of the class that created the object
  • Isolate the creation and use of complex objects

Builder mode code

/*
 * @ Description: C++ Design Patterns___Builder
 * @ version: v1.0
 * @ Author: WeissxJ
 */
#include<iostream>
#include<string>

class Product
{
    
    
    private:
        std::string partA;
        std::string partB;
        std::string partC;
    public:
        void makeA(const std::string &part){
    
    
            partA=part;
        }
        void makeB(const std::string &part){
    
    
            partB=part;
        }
        void makeC(const std::string &part){
    
    
            partC=part;
        }
        std::string get(){
    
    
            return (partA+" "+partB+" "+partC);
        }
        // ...
};

class Builder
{
    
    
    protected:
        Product product;
    public:
        virtual ~Builder() {
    
    }

        Product get(){
    
    
            return product;
        }
        virtual void buildPartA()=0;
        virtual void buildPartB()=0;
        virtual void buildPartC()=0;
        // ...
};

class ConcretBuilderX : public Builder
{
    
    
    public:
        void buildPartA(){
    
    
            product.makeA("A-X");
        }
        void buildPartB(){
    
    
            product.makeB("B-X");
        }
        void buildPartC(){
    
    
            product.makeC("C-X");
        }
        // ...
};

class ConcretBuilderY : public Builder
{
    
    
    public:
        void buildPartA(){
    
    
            product.makeA("A-Y");
        }
        void buildPartB(){
    
    
            product.makeB("B-Y");
        }
        void buildPartC(){
    
    
            product.makeC("C-Y");
        }
        // ...
};

class Director
{
    
    
    private:
        Builder* builder;
    public:
        Director():builder() {
    
    }
        ~Director(){
    
    
            if(builder)
                delete builder;
        }
        void set(Builder* b){
    
    
            if(builder){
    
    
                delete builder;
            }
            builder=b;
        }
        Product get(){
    
    
            return builder->get();
        }
        void construct(){
    
    
            builder->buildPartA();
            builder->buildPartB();
            builder->buildPartC();
            // ...
        }
        // ...
};

int main(){
    
    
    Director director;
    director.set(new ConcretBuilderX);
    director.construct();

    Product product1=director.get();
    std::cout<<"1st product parts: "<<product1.get()<<std::endl;

    director.set(new ConcretBuilderY);
    director.construct();

    Product product2=director.get();
    std::cout<<"2nd product parts: "<<product2.get()<<std::endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43477024/article/details/111658451