构建器模式(Builder)的目标是将对象的创建与它的表示法分开,意味着创建过程保持原状,但产生对象的表示法可能不同,它通常用来创建复杂的对象,这个对象是由多个组件组成的。
对于销售组装电脑的销售商来说,他们要销售电脑,而电脑通常由CPU、主板及硬盘等部件组成,这时,电脑的组成就可以由Builder模式来解决。
UML图如下:
代码如下:
//ComputerPart.h
#include <string>
class ComputerPart
{
public:
ComputerPart();
ComputerPart(std::string);
virtual ~ComputerPart();
friend std::ostream& operator<<(std::ostream& os, ComputerPart& part);
private:
std::string m_strPart;
};
//ComputerPart.cpp
#include "stdafx.h"
#include "ComputerPart.h"
#include <iostream>
using namespace std;
ComputerPart::ComputerPart()
{
}
ComputerPart::ComputerPart(string part)
{
m_strPart = part;
}
ComputerPart::~ComputerPart()
{
}
std::ostream& operator<<(std::ostream& os, ComputerPart& part)
{
return os << part.m_strPart << " ";
}
//DesktopProduct.h
#include <vector>
class ComputerPart;
class DesktopProduct
{
public:
DesktopProduct();
virtual ~DesktopProduct();
void AddPart(ComputerPart*);
friend std::ostream& operator<<(std::ostream& os, DesktopProduct& computer);
private:
std::vector<ComputerPart*> m_vParts;
};
//DesktopProduct.cpp
#include "stdafx.h"
#include "DesktopProduct.h"
#include "ComputerPart.h"
#include <iostream>
#include <typeinfo>
using namespace std;
DesktopProduct::DesktopProduct()
{
}
DesktopProduct::~DesktopProduct()
{
}
void DesktopProduct::AddPart(ComputerPart* pPart)
{
m_vParts.push_back(pPart);
}
ostream& operator<<(ostream& os, DesktopProduct& computer)
{
vector<ComputerPart*>::iterator it;
for(it = computer.m_vParts.begin(); it != computer.m_vParts.end(); ++it)
{
os << **it;
}
return os;
}
//Builder.h
class DesktopProduct;
class Builder
{
public:
Builder();
virtual ~Builder();
virtual void BuilderCPU() = 0;
virtual void BuilderMainboard() = 0;
virtual void BuilderHarddisk() = 0;
void CreateComputer();
DesktopProduct* GetComputer();
protected:
DesktopProduct* m_pComputer;
};
//Builder.cpp
#include "stdafx.h"
#include "Builder.h"
#include "DesktopProduct.h"
Builder::Builder()
{
m_pComputer = 0;
}
Builder::~Builder()
{
}
void Builder::CreateComputer()
{
m_pComputer = new DesktopProduct;
}
DesktopProduct* Builder::GetComputer()
{
return m_pComputer;
}
//DesktopBuilder.h
#include "Builder.h"
class DesktopBuilder : public Builder
{
public:
DesktopBuilder();
virtual ~DesktopBuilder();
void BuilderCPU();
void BuilderMainboard();
void BuilderHarddisk();
};
//DesktopBuilder.cpp
#include "stdafx.h"
#include "DesktopBuilder.h"
#include "DesktopProduct.h"
#include "ComputerPart.h"
#include <string>
using namespace std;
DesktopBuilder::DesktopBuilder()
{
}
DesktopBuilder::~DesktopBuilder()
{
}
void DesktopBuilder::BuilderCPU()
{
m_pComputer->AddPart(new ComputerPart("CPU"));
}
void DesktopBuilder::BuilderHarddisk()
{
m_pComputer->AddPart(new ComputerPart("硬盘"));
}
void DesktopBuilder::BuilderMainboard()
{
m_pComputer->AddPart(new ComputerPart("主板"));
}
//Seller.h
class Builder;
class Seller
{
public:
Seller();
virtual ~Seller();
void SetBuilder(Builder*);
void Construct();
private:
Builder* m_pBuilder;
};
//Seller.cpp
#include "stdafx.h"
#include "Seller.h"
#include "Builder.h"
Seller::Seller()
{
m_pBuilder = 0;
}
Seller::~Seller()
{
}
void Seller::SetBuilder(Builder* pBuilder)
{
m_pBuilder = pBuilder;
}
void Seller::Construct()
{
m_pBuilder->CreateComputer();
m_pBuilder->BuilderCPU();
m_pBuilder->BuilderHarddisk();
m_pBuilder->BuilderMainboard();
}
//main.cpp
#include "stdafx.h"
#include "Builder.h"
#include "DesktopBuilder.h"
#include "Seller.h"
#include "DesktopProduct.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Builder* pBuilder = new DesktopBuilder;
Seller* pSeller = new Seller;
pSeller->SetBuilder(pBuilder);
pSeller->Construct();
cout << "组装一台电脑,由下列部件组成:\n" << *(pBuilder->GetComputer()) << endl;
return 0;
}
最后输出为:
组装一台电脑,由下列部件组成:
CPU 硬盘 主板
该例中,产品台式电脑(DesktopProduct)由CPU、硬盘及主板组成,它的组成过程是交给构建器(DesktopBuilder)来组合的,而构建器又是由电脑组装人员(Seller)来进行操作,最终,电脑组装人员组装完成一电脑,然后销售组客户。