DesignPattern/04.BuilderPattern/2.Code/BuilderPattern.h

152 lines
2.8 KiB
C
Raw Normal View History

2019-10-19 07:52:26 +00:00
#ifndef __BUILDER_PATTERN__
#define __BUILDER_PATTERN__
#include <iostream>
#include <string.h>
using namespace std;
2021-10-28 15:15:51 +00:00
// 产品类House
2019-10-19 07:52:26 +00:00
class House
{
public:
2020-11-29 07:28:46 +00:00
House() {}
void setFloor(string iFloor) {
2019-10-19 07:52:26 +00:00
this->floor = iFloor;
}
2020-11-29 07:28:46 +00:00
void setWall(string iWall) {
2019-10-19 07:52:26 +00:00
this->wall = iWall;
}
2020-11-29 07:28:46 +00:00
void setRoof(string iRoof) {
2019-10-19 07:52:26 +00:00
this->roof = iRoof;
}
2021-10-28 15:15:51 +00:00
// 打印House信息
2020-11-29 07:28:46 +00:00
void printfHouseInfo() {
2019-10-19 07:52:26 +00:00
printf("Floor:%s\t\n", this->floor.c_str());
printf("Wall:%s\t\n", this->wall.c_str());
printf("Roof:%s\t\n", this->roof.c_str());
}
private:
string floor;
string wall;
string roof;
};
2021-10-28 15:15:51 +00:00
// 抽象建造者AbstractBall
2019-10-19 07:52:26 +00:00
class AbstractBuilder
{
public:
2020-11-29 07:28:46 +00:00
AbstractBuilder() {
2019-10-19 07:52:26 +00:00
house = new House();
}
AbstractBuilder(const AbstractBuilder& o) = delete;
AbstractBuilder& operator=(const AbstractBuilder& o) = delete;
2020-11-29 07:28:46 +00:00
virtual ~AbstractBuilder()
{
if (house != nullptr)
{
delete house;
house = nullptr;
}
}
2021-10-28 15:15:51 +00:00
// 抽象方法:
2019-10-19 07:52:26 +00:00
virtual void buildFloor() = 0;
virtual void buildWall() = 0;
virtual void buildRoof() = 0;
virtual House *getHouse() = 0;
House *house;
};
2021-10-28 15:15:51 +00:00
// 具体建造者ConcreteBuilderA
2019-10-19 07:52:26 +00:00
class ConcreteBuilderA :public AbstractBuilder
{
public:
2020-11-29 07:28:46 +00:00
ConcreteBuilderA() {
2019-10-19 07:52:26 +00:00
printf("ConcreteBuilderA\n");
}
ConcreteBuilderA(const ConcreteBuilderA& o) = delete;
ConcreteBuilderA& operator=(const ConcreteBuilderA& o) = delete;
2020-11-29 07:28:46 +00:00
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
2021-10-28 15:15:51 +00:00
// 具体实现方法
2020-11-29 07:28:46 +00:00
void buildFloor() {
2019-10-19 07:52:26 +00:00
this->house->setFloor("Floor_A");
}
2020-11-29 07:28:46 +00:00
void buildWall() {
2019-10-19 07:52:26 +00:00
this->house->setWall("Wall_A");
}
2020-11-29 07:28:46 +00:00
void buildRoof() {
2019-10-19 07:52:26 +00:00
this->house->setRoof("Roof_A");
}
2020-11-29 07:28:46 +00:00
House *getHouse() {
2019-10-19 07:52:26 +00:00
return this->house;
}
};
2021-10-28 15:15:51 +00:00
// 具体建造者ConcreteBuilderB
2019-10-19 07:52:26 +00:00
class ConcreteBuilderB :public AbstractBuilder
{
public:
2020-11-29 07:28:46 +00:00
ConcreteBuilderB() {
2019-10-19 07:52:26 +00:00
printf("ConcreteBuilderB\n");
}
ConcreteBuilderB(const ConcreteBuilderB&) = delete;
ConcreteBuilderB& operator=(const ConcreteBuilderB&) = delete;
~ConcreteBuilderB()
2020-11-29 07:28:46 +00:00
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
2021-10-28 15:15:51 +00:00
// 具体实现方法
2020-11-29 07:28:46 +00:00
void buildFloor() {
2019-10-19 07:52:26 +00:00
this->house->setFloor("Floor_B");
}
2020-11-29 07:28:46 +00:00
void buildWall() {
2019-10-19 07:52:26 +00:00
this->house->setWall("Wall_B");
}
2020-11-29 07:28:46 +00:00
void buildRoof() {
2019-10-19 07:52:26 +00:00
this->house->setRoof("Roof_B");
}
2020-11-29 07:28:46 +00:00
House *getHouse() {
2019-10-19 07:52:26 +00:00
return this->house;
}
};
2021-10-28 15:15:51 +00:00
// 指挥者Director
2019-10-19 07:52:26 +00:00
class Director
{
public:
2020-11-29 07:28:46 +00:00
Director() :builder(nullptr) {}
~Director()
{
if (this->builder != nullptr)
{
delete builder;
builder = nullptr;
}
}
2021-10-28 15:15:51 +00:00
// 具体实现方法
2020-11-29 07:28:46 +00:00
void setBuilder(AbstractBuilder *iBuilder) {
2019-10-19 07:52:26 +00:00
this->builder = iBuilder;
}
2021-10-28 15:15:51 +00:00
// 封装组装流程,返回建造结果
2020-11-29 07:28:46 +00:00
House *construct() {
2019-10-19 07:52:26 +00:00
builder->buildFloor();
builder->buildWall();
builder->buildRoof();
return builder->getHouse();
}
private:
AbstractBuilder *builder;
};
#endif //__BUILDER_PATTERN__