#define __CRT_SECURE_NO_WARNINGS
/*
@作者:莫忘输赢
@时间:
2020/02/20 15:20
@版本:v1
@建造者模式
@作用
@优点:
在创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式适用
*/
#include<iostream>
#include<string>
#include<vector>
#include<vld.h>
//最终产品类
class Product
{
private:
std::vector<std::string> m_FinishProduct;
public:
void Add(std::string strTemp)
{
m_FinishProduct.push_back(strTemp);
}
void Show()
{
auto p = m_FinishProduct.begin();
while (p!= m_FinishProduct.end())
{
std::cout << *p << std::endl;
p++;
}
}
};
//建造者
class Builder
{
protected:
Product *m_Product;
public:
virtual void BuilderCell1() = 0;
virtual void BuilderCell2() = 0;
virtual Product* GetResult() = 0;
};
//第一种建造模式
class ConcreBuilder1 : public Builder
{
public:
ConcreBuilder1()
{
m_Product = new Product();
}
virtual void BuilderCell1( )
{
m_Product->Add("cb1-bc1");
}
virtual void BuilderCell2( )
{
m_Product->Add("cb1-bc2");
}
virtual Product* GetResult()
{
return m_Product;
}
};
//第二种建造模式
class ConcreBuilder2 : public Builder
{
public:
ConcreBuilder2()
{
m_Product = new Product( );
}
virtual void BuilderCell1()
{
m_Product->Add("cb2-bc1");
}
virtual void BuilderCell2( )
{
m_Product->Add("cb2-bc2");
}
virtual Product* GetResult()
{
return m_Product;
}
};
//指挥者类
class Direct
{
public:
void Construct(Builder* temp)
{
temp->BuilderCell1( );
temp->BuilderCell2( );
}
};
//using namespace std;
int main(int argc, char **argv)
{
Direct* p = new Direct();
Builder* b1 = new ConcreBuilder1();
Builder* b2 = new ConcreBuilder2();
p->Construct(b1);
Product *pb1 = b1->GetResult();
pb1->Show();
p->Construct(b2);
Product *pb2 = b2->GetResult();
pb2->Show();
delete pb2;
delete pb1;
delete b2;
delete b1;
delete p;
return 0;
}