建造者模式(Builder)

 建造者模式:将一个复杂对象的构建与它的表现分离,使得同样的构建过程可以创建不同的表现。


 构建过程分为:客户-指导-生成器 Client - Director - Builder:

类图:

建造者模式(Builder)_第1张图片

 时序图:

建造者模式(Builder)_第2张图片具体实现:

#import 
#import 
//最终被建造产品
class IHouse{
public:
    virtual ~IHouse(){}
protected:
    int _foundationCount;
    int _cementFrameCount;
    int _brickCount;
    int _wattsCout;
    int _pipeCount;
    int _windowCount;
public:
    virtual void setfoundationCount(int count) = 0;
    
    virtual void setCementFrameCount(int count) = 0;
    
    virtual void setBrickCount(int count) = 0;
    
    virtual void setWattsCout(int count) = 0;
    
    virtual void setPipeCount(int count) = 0;
    
    virtual void setWindowCount(int count) = 0;
};

//别墅
class Villa:public IHouse{
    void setfoundationCount(int count){
        _foundationCount = count;
        std::cout << "构建别墅地基个数:" << _foundationCount < setfoundationCount(1);
    }
      //水泥框架
    void buildCementFramework(){
        std::cout << "开始构建别墅水泥框架" << std::endl;
         _house -> setCementFrameCount(20);
    }
      //墙
    void buildWall(){
        std::cout << "开始构建别墅墙" << std::endl;
         _house -> setWattsCout(90);
    }
      //砖
    void buildBrick(){
        std::cout << "开始构建别墅砖" << std::endl;
        _house -> setBrickCount(29900);
    }
      //瓦
    void buildWatts(){
        std::cout << "开始构建别墅瓦" << std::endl;
        _house -> setWattsCout(5000);
    }
      //管道
    void buildPipe(){
        std::cout << "开始构建别墅管道" << std::endl;
        _house -> setPipeCount(100);
    }
      //窗户
    void buildWindow(){
        std::cout << "开始构建别墅窗户" << std::endl;
        _house -> setCementFrameCount(20);
    }
    
    IHouse* getBuildResult(){
        return _house;
    }
};

//民房建造者
class PrivateHouseBuilder:public IBuilder{
private:
    IHouse*     _house;
public:
    PrivateHouseBuilder(IHouse* house){
        _house = house;
    }
public:
    void buildFoundation(){
        std::cout << "构建民房地基" << std::endl;
    }
      //水泥框架
    void buildCementFramework(){
        std::cout << "构建民房水泥框架" << std::endl;
    }
      //墙
    void buildWall(){
        std::cout << "构建民房墙" << std::endl;
    }
      //砖
    void buildBrick(){
        std::cout << "构建民房砖" << std::endl;
    }
      //瓦
    void buildWatts(){
        std::cout << "构建民房瓦" << std::endl;
    }
      //管道
    void buildPipe(){
        std::cout << "构建民房管道" << std::endl;
    }
      //窗户
    void buildWindow(){
        std::cout << "构建民房窗户" << std::endl;
    }
    
    IHouse* getBuildResult(){
        return _house;
    }
};

//指导者
class Director{
public:
    IHouse* constructVialla(IBuilder* builder){
        builder -> buildFoundation();
        builder -> buildCementFramework();
        builder -> buildWall();
        builder -> buildBrick();
        builder -> buildWatts();
        builder -> buildPipe();
        builder -> buildWindow();
        return builder -> getBuildResult();
    }
    
    IHouse* constructPrivateHouse(IBuilder* builder){
        builder -> buildFoundation();
        builder -> buildCementFramework();
        builder -> buildWall();
        builder -> buildBrick();
        builder -> buildWatts();
        builder -> buildPipe();
        builder -> buildWindow();
        return builder -> getBuildResult();
    }
};
//client 充当客户端
int main(int argc, const char * argv[]) {
    // insert code here...
    Director* director = new Director();
    
    IBuilder* viallaBuilder = new ViallaBuilder(new Villa());
    IHouse* viallaHouse = director -> constructVialla(viallaBuilder);
    
    IBuilder* privateHouseBuilder = new PrivateHouseBuilder(new PrivateHouse());
    IHouse* privateHouseHouse =  director -> constructPrivateHouse(privateHouseBuilder);

    return 0;
}

建造者模式(Builder)_第3张图片

你可能感兴趣的:(设计模式)