c ++ design patterns -Builder

Motivation
in the software system, sometimes faced "a complex object" to create work, which usually consists of various parts of sub-objects with a certain algorithm; due to changes in demand, the various parts of this complex subject often face dramatic changes , but the algorithm combining them is relatively stable.

How to respond to this change? How to provide a "package mechanism" to isolate the change in "various parts of the complex objects", thereby maintaining the system of "stable construction algorithm" does not change as needs change?

Definition of
the construct represents its phase separation, so that the same construction process (stabilizer) may create different representations (variation) of a complex object.
- "Design Patterns" GoF

Structure
QQ picture 20200124191818.png
example

class House{
    //....
};
//Builder
class HouseBuilder {
public:
    House* GetResult(){
        return pHouse;
    }
    virtual ~HouseBuilder(){}
protected:
    
    House* pHouse;
    virtual void BuildPart1()=0;
    virtual void BuildPart2()=0;
    virtual void BuildPart3()=0;
    virtual void BuildPart4()=0;
    virtual void BuildPart5()=0;
    
};

class StoneHouse: public House{
    
};
//ConcreteBuilder
class StoneHouseBuilder: public HouseBuilder{
protected:
    
    virtual void BuildPart1(){
        //pHouse->Part1 = ...;
    }
    virtual void BuildPart2(){
        
    }
    virtual void BuildPart3(){
        
    }
    virtual void BuildPart4(){
        
    }
    virtual void BuildPart5(){
        
    }
    
};

//Director
class HouseDirector{
    
public:
    HouseBuilder* pHouseBuilder;
    
    HouseDirector(HouseBuilder* pHouseBuilder){
        this->pHouseBuilder=pHouseBuilder;
    }
    
    House* Construct(){
        
        pHouseBuilder->BuildPart1();
        
        for (int i = 0; i < 4; i++){
            pHouseBuilder->BuildPart2();
        }
        
        bool flag=pHouseBuilder->BuildPart3();
        
        if(flag){
            pHouseBuilder->BuildPart4();
        }
        
        pHouseBuilder->BuildPart5();
        
        return pHouseBuilder->GetResult();
    }
};

Summary
1. is a complex object composed of a plurality of parts, is to create a model builder to create complex objects and member are respectively open, respectively Builder classes and classes to represent Director. Construction of the final complex object with Director, encapsulated in the above method is created Builder interface member (complex objects composed of these members is made), that is, how the Director responsible for final assembly into products member. So let the builder pattern design and implementation of the decoupling.
2. Where the change point, where the package - Builder mode mainly in response to "the complexity of the
changes as various parts of the" frequent demand. The disadvantage is that it is difficult to deal with "step by step build
algorithms" demand change.

Guess you like

Origin www.cnblogs.com/Redwarx008/p/12232532.html