19.C++设计模式-建造者模式

19.C++设计模式-建造者模式_第1张图片
图片.png
#include 
using namespace std;

class House {
private:
    string m_door;
    string m_wall;
    string m_window;
public:
    void setDoor(string door) {
        this->m_door = door;
    }
    void setWall(string wall) {
        this->m_wall = wall;
    }
    void setWindow(string window) {
        this->m_window = window;
    }

    string getDoor() {
        cout<house = house;
    }
    virtual void buildWall() {
        house->setWall("flat wall");
    }
    virtual void buildDoor() {
        house->setDoor("flat door");
    }

    virtual void buildWindow() {
        house->setWindow("flat window");
    }
    virtual House* getHouse() {
        return house;
    }
};

class NiceBuilder :public Builder {
public:
    House* house;
    NiceBuilder(House* house) {
        this->house = house;
    }
    virtual void buildWall() {
        house->setWall("nice wall");
    }
    virtual void buildDoor() {
        house->setDoor("nice door");
    }

    virtual void buildWindow() {
        house->setWindow("nice window");
    }
    virtual House* getHouse() {
        return house;
    }
};

class Director {
public:
    Builder* builder;
    Director(Builder* builder) {
        this->builder = builder;
    }
    House* construct() {
        builder->buildDoor();
        builder->buildWall();
        builder->buildWindow();
        return builder->getHouse();
    }
};

void main() {
    House* house;
    Builder* builder;
    Director* director;

    house = new House;
    builder = new FlatBuilder(house);
    director = new Director(builder);
    house = director->construct();
    house->getDoor();
    house->getWall();
    house->getWindow();

    delete director;
    delete builder;
    delete house;

    house = new House;
    builder = new NiceBuilder(house);
    director = new Director(builder);
    house = director->construct();
    house->getDoor();
    house->getWall();
    house->getWindow();
    delete director;
    delete builder;
    delete house;

    system("pause");
}

你可能感兴趣的:(19.C++设计模式-建造者模式)