图片.png
#include <iostream>
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<<m_door<<endl;
return m_door;
}
string getWall() {
cout<<m_wall<<endl;
return m_wall;
}
string getWindow() {
cout << m_window << endl;
return m_window;
}
};
class Builder {
public:
virtual void buildWall() = 0;
virtual void buildDoor() = 0;
virtual void buildWindow() = 0;
virtual House* getHouse() = 0;
};
class FlatBuilder :public Builder{
public:
House* house;
FlatBuilder(House* house) {
this->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");
}
网友评论