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

145 lines
2.4 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;
//<2F><>Ʒ<EFBFBD><C6B7>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;
}
//<2F><>ӡHouse<73><65>Ϣ
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;
};
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>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();
}
2020-11-29 07:28:46 +00:00
virtual ~AbstractBuilder()
{
if (house != nullptr)
{
delete house;
house = nullptr;
}
}
//<2F><><EFBFBD>󷽷<EFBFBD><F3B7BDB7><EFBFBD>
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;
};
//<2F><><EFBFBD><EFBFBD><E5BDA8><EFBFBD><EFBFBD>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");
}
2020-11-29 07:28:46 +00:00
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
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;
}
};
//<2F><><EFBFBD><EFBFBD><E5BDA8><EFBFBD><EFBFBD>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");
}
2020-11-29 07:28:46 +00:00
~ConcreteBuilderA()
{
if (this->house != nullptr)
{
delete house;
house = nullptr;
}
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
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;
}
};
//ָ<><D6B8><EFBFBD><EFBFBD>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;
}
}
//<2F><><EFBFBD><EFBFBD>ʵ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
2020-11-29 07:28:46 +00:00
void setBuilder(AbstractBuilder *iBuilder) {
2019-10-19 07:52:26 +00:00
this->builder = iBuilder;
}
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__