Builder 模式和 AbstractFactory 模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:Builder 模式强调的是一步步创建对象,并通过相同的创建过程可以获得不同的结果对象,一般来说 Builder 模式中对象不是直接返回的。而在 AbstractFactory 模式中对象是直接返回的,AbstractFactory模式强调的是为创建多个相互依赖的对象提供一个同一的接口。
////////////Product.h//////////////////////
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include <vector>
#include <string>
class Product
{
public: Product();
~Product();
void Add( std::string part);
void Show(); protected:
private:
std::vector<std::string> svec;
}; #endif
////////////Product.cpp//////////////////////
#include "Product.h"
#include <iostream>
using namespace std; void Product::Add( string part)
{
svec.push_back(part);
} void Product::Show()
{
vector<string>::iterator it = svec.begin();
while (it != svec.end())
{
cout<<*it<<" ";
it++;
}
cout<<endl;
} Product::Product(){}
Product::~Product(){}
////////////Builder.h//////////////////////
#ifndef _BUILDER_H
#define _BUILDER_H
#include <string>
using namespace std; class Product ;
class Builder
{
public:
virtual ~Builder();
virtual void BuildPartA() = ;
virtual void BuildPartB() = ;
virtual void BuildPartC() = ;
virtual Product* GetProduct() = ;
protected:
Builder();
private: }; class ConcreteBuilder : public Builder
{
public:
~ConcreteBuilder();
ConcreteBuilder(); void BuildPartA();
void BuildPartB();
void BuildPartC();
Product* GetProduct();
protected:
private:
Product* product;
}; class ConcreteBuilder2 : public Builder
{
public:
~ConcreteBuilder2();
ConcreteBuilder2(); void BuildPartA();
void BuildPartB();
void BuildPartC();
Product* GetProduct();
protected:
private:
Product* product;
}; #endif
/////////Builder.cpp//////////////////
#include "Builder.h"
#include "Product.h"
#include <iostream>
using namespace std; Builder::Builder(){}
Builder::~Builder(){} ConcreteBuilder::ConcreteBuilder()
{
product = NULL;
}
ConcreteBuilder::~ConcreteBuilder()
{ }
void ConcreteBuilder::BuildPartA()
{
product = new Product();
product->Add("A");
cout<<"加入A"<<endl;
} void ConcreteBuilder::BuildPartB()
{
product->Add("B");
cout<<"加入B"<<endl;
} void ConcreteBuilder::BuildPartC()
{
product->Add("C");
cout<<"加入C"<<endl;
} Product* ConcreteBuilder::GetProduct()
{
return product ;
} ConcreteBuilder2::ConcreteBuilder2()
{
product = NULL;
}
ConcreteBuilder2::~ConcreteBuilder2()
{ }
void ConcreteBuilder2::BuildPartA()
{
product = new Product();
product->Add("x");
cout<<"加入x"<<endl;
} void ConcreteBuilder2::BuildPartB()
{
product->Add("y");
cout<<"加入y"<<endl;
} void ConcreteBuilder2::BuildPartC()
{
product->Add("z");
cout<<"加入z"<<endl;
} Product* ConcreteBuilder2::GetProduct()
{
return product ;
}
////////////////////////////Director.h//////////////////////////////////////////////
#ifndef _DIRECTOR_H
#define _DIRECTOR_H class Builder;
class Director
{
public:
Director( Builder* bld); ~Director(); void Construct();
protected:
private:
Builder* _bld ;
}; #endif
//////////////////////////////Director.cpp////////////////////////////////////////////
#include "Director.h"
#include "Builder.h"
using namespace std ; Director::Director(Builder* bld)
{
_bld = bld ;
}
Director::~Director()
{
}
void Director::Construct()
{
_bld->BuildPartA();
_bld->BuildPartB();
_bld->BuildPartC();
}
/////////////////////////////main.cpp/////////////////////////////////////////////
#include "Builder.h"
#include "Director.h"
#include "Product.h"
using namespace std; int main()
{
Builder* b1 = new ConcreteBuilder();
Director* d1 = new Director(b1);
d1->Construct();
Product* p1 = b1->GetProduct();
p1->Show(); Builder* b2 = new ConcreteBuilder2();
Director* d2 = new Director(b2);
d2->Construct();
Product* p2 = b2->GetProduct();
p2->Show(); system("pause");
return ;
}
以上代码都是经过测试可以正常运行的哦···········