构建者模式
主要是将一个复杂对象的构建和表示分离(类似模板方法模式)。
直接上代码:
#pragma once
#include<iostream>
#include<string>
using namespace std;
class House {
public:
virtual void setWall(const string& str) = 0;
virtual void setWindow(const string& str) = 0;
virtual void setDoor(const string& str) = 0;
virtual void setRoom(const string& str) = 0;
virtual void show() = 0;
virtual ~House() {}
string wall, Window, door, room;
};
class HouseBuild {
public:
virtual House* GetResult() = 0;
virtual void BuildWall() = 0;
virtual void BuildWindow() = 0;
virtual void BuildDoor() = 0;
virtual void BuildRoom() = 0;
virtual ~HouseBuild() {}
};
class StoneHouse :public House {
public:
void setWall(const string& str) override{
this->wall = str;
}
void setWindow(const string& str) override {
this->Window = str;
}
void setDoor(const string& str) override {
this->door = str;
}
void setRoom(const string& str) override {
this->room = str;
}
void show()override {
cout << "Stone House data:" << "wall=" << wall << ", window="
<< Window << ", door=" << door << ", room=" <<room<< endl;
}
};
class StoneHouseBuild :public HouseBuild {
public:
StoneHouseBuild(StoneHouse* pStoneHouse) {
this->pStoneHouse = pStoneHouse;
}
void BuildWall() override{
cout << "Stone Wall" << endl;
pStoneHouse->setWall("Stonewall");
}
void BuildWindow() override {
cout << "Stone Window" << endl;
pStoneHouse->setWindow("Stonewindow");
}
void BuildDoor() override {
cout << "Stone Door" << endl;
pStoneHouse->setDoor("Stonedoor");
}
void BuildRoom() override {
cout << "Stone Room" << endl;
pStoneHouse->setRoom("Stoneroom");
}
House* GetResult()override {
return pStoneHouse;
}
~StoneHouseBuild() {
delete pStoneHouse;
this->pStoneHouse = nullptr;
}
private:
StoneHouse* pStoneHouse;
};
class WoodHouse :public House {
public:
void setWall(const string& str) override {
this->wall = str;
}
void setWindow(const string& str) override {
this->Window = str;
}
void setDoor(const string& str) override {
this->door = str;
}
void setRoom(const string& str) override {
this->room = str;
}
void show()override {
cout << "Wood House data:" << "wall=" << wall << ", window="
<< Window << ", door=" << door << ", room=" <<room<< endl;
}
};
class WoodHouseBuild :public HouseBuild {
public:
WoodHouseBuild(WoodHouse* pWoodHouse) {
this->pWoodHouse = pWoodHouse;
}
void BuildWall() override {
cout << "Wood Wall" << endl;
pWoodHouse->setWall("Woodwall");
}
void BuildWindow() override {
cout << "Wood Window" << endl;
pWoodHouse->setWindow("Woodwindow");
}
void BuildDoor() override {
cout << "Wood Door" << endl;
pWoodHouse->setDoor("Wooddoor");
}
void BuildRoom() override {
cout << "Wood Room" << endl;
pWoodHouse->setRoom("Woodroom");
}
House* GetResult()override {
return pWoodHouse;
}
~WoodHouseBuild() {
delete pWoodHouse;
this->pWoodHouse = nullptr;
}
private:
WoodHouse* pWoodHouse;
};
Build.h。在这里,以建房子为例。其中建房子的顺序是一致的,都是Wall、Window、Door、Room,这部分是固定的,因此把该部分整体抽出来在另一个抽象基类HouseBuild中执行,实现分离。每种类型的房子都继承自抽象基类House,对应的建房子算法继承自抽象基类HouseBuild。
#include<iostream>
#include"Build.h"
using namespace std;
class HouseDirector {
public:
HouseBuild* pHouseBuild;
HouseDirector(HouseBuild* pHouseBuild){
this->pHouseBuild = pHouseBuild;
}
//流程一样,稳定的
House* Construct() {
pHouseBuild->BuildWall();
pHouseBuild->BuildWindow();
pHouseBuild->BuildDoor();
pHouseBuild->BuildRoom();
return pHouseBuild->GetResult();
}
~HouseDirector() {
delete pHouseBuild;
this->pHouseBuild = nullptr;
}
};
int main()
{
HouseBuild* pHouse1 = new StoneHouseBuild(new StoneHouse());
HouseDirector* director1 = new HouseDirector(pHouse1);
director1->Construct()->show();
cout << "===========" << endl;
HouseBuild* pHouse2 = new WoodHouseBuild(new WoodHouse());
HouseDirector* director2 = new HouseDirector(pHouse2);
director2->Construct()->show();
return 0;
}
Build.cpp。其中Construct方法就是稳定不变的。这样就实现了构建和表示的分离。
实验结果如下: