直接给实例。
说明:
1.Product 是需要的产品,该产品由两个部件组成,产品本体body和产品外包装case。
2.Builder为构建产品的祖先,ConcreteBuilderA,ConcreteBuilderB负责具体构建产品。其中ConcreteBuilderA为构建出口产品,ConcreteBuilderB构建国内市场产品。
3.ConcreteBuilderA构建出口产品过程由:
AddBody():从出口区仓库取产品本体body
AddCase():从出口区仓库取产品包装case
check():按照出口国家标准进行检查。
ConreteBuilderB:与A类同
4.指挥者 Director.传统的指挥者封装了产品建造的顺序,使得各产品建造顺序必须一致,这里为了克服这个问题,每个产品建造顺序隐藏在建造者内部BuildPart()
代码:
///
// codes.h
// Implementation of the Class Builder
// Created on: 10-十二月-2012 14:01:14
///
#if !defined(EA_2169EEBA_E97F_4ea0_B23A_77E26A661C6A__INCLUDED_)
#define EA_2169EEBA_E97F_4ea0_B23A_77E26A661C6A__INCLUDED_
#include <string>
#include <list>
using namespace std;
/**
* This class constructs an object using the Builder interface.
*/
class Director
{
public:
Builder* m_Builder;
Director();
virtual ~Director();
void Construct(Builder* m_Builder);
};
/**
* This class specifies an abstract interface for creating parts of a Product
* object.
*/
class Director;
class Builder;
class ConcreteBuilderA;
class ConcreteBuilderB;
class Product;
/**
* This class specifies an abstract interface for creating parts of a Product
* object.
*/
class Builder
{
public:
Builder();
virtual ~Builder();
virtual void BuildPart();
protected:
Product* m_Product;
};
/**
* This class (a) constructs and assembles parts of the product by implementing
* the Builder interface, (b) defines and keeps track of the representation it
* creates, and (c) provides an interface for retrieving the product.
*/
class ConcreteBuilderA : public Builder
{
public:
ConcreteBuilderA();
virtual ~ConcreteBuilderA();
void AddBody();
void AddCase();
virtual void BuildPart();
void check();
Product* GetResult();
};
/**
* This class (a) constructs and assembles parts of the product by implementing
* the Builder interface, (b) defines and keeps track of the representation it
* creates, and (c) provides an interface for retrieving the product.
*/
class ConcreteBuilderB : public Builder
{
public:
ConcreteBuilderB();
virtual ~ConcreteBuilderB();
void AddBody();
void AddCase();
virtual void BuildPart();
void check();
Product* GetResult();
};
/**
* This class (a) represents the complex object under construction.
* ConcreteBuilder builds the product's internal representation and defines the
* process by which it's assembled, and (b) includes classes that define the
* constituent parts, including interfaces for assembling the parts into the final
* result.
*/
class Product
{
public:
Product();
virtual ~Product();
void Add(string name);
list<string> getLog();
int getState();
void setState(int value);
private:
int name;
list<string> partLists;
int state;
};
#endif // !defined(EA_2169EEBA_E97F_4ea0_B23A_77E26A661C6A__INCLUDED_)
///
// codes.cpp
// Implementation of the Class Builder
// Created on: 10-十二月-2012 14:01:14
///
#include "stdafx.h"
#include "codes.h"
Director::Director(){
}
Director::~Director(){
}
void Director::Construct(Builder* m_Builder){
this->m_Builder=m_Builder;
m_Builder->BuildPart();
}
Builder::Builder(){
m_Product=new Product();
}
Builder::~Builder(){
if (m_Product!=NULL){
delete m_Product;
}
}
void Builder::BuildPart(){
}
ConcreteBuilderA::ConcreteBuilderA(){
}
ConcreteBuilderA::~ConcreteBuilderA(){
}
void ConcreteBuilderA::AddBody(){
string str="get body from export depot";
m_Product->Add(str);
}
void ConcreteBuilderA::AddCase(){
string str="get case from export depot";
m_Product->Add(str);
}
void ConcreteBuilderA::BuildPart(){
m_Product->setState(1);
AddCase();
AddBody();
check();
}
void ConcreteBuilderA::check(){
string str="pass for export market";
m_Product->Add(str);
}
Product* ConcreteBuilderA::GetResult(){
return m_Product;
}
ConcreteBuilderB::ConcreteBuilderB(){
}
ConcreteBuilderB::~ConcreteBuilderB(){
}
void ConcreteBuilderB::AddBody(){
string str="get body from domestic depot";
m_Product->Add(str);
}
void ConcreteBuilderB::AddCase(){
string str="get case from domestic depot";
m_Product->Add(str);
}
void ConcreteBuilderB::BuildPart(){
AddCase();
AddBody();
check();
}
void ConcreteBuilderB::check(){
string str="pass for domestic market";
m_Product->Add(str);
}
Product* ConcreteBuilderB::GetResult(){
return m_Product;
}
Product::Product(){
}
Product::~Product(){
}
void Product::Add(string name){
partLists.push_back(name);
}
list<string> Product::getLog(){
return partLists;
}
int Product::getState(){
return state;
}
void Product::setState(int value){
state=value;
}
客户端:
builder.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "codes.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str;
str="hello world";
ConcreteBuilderB *builder= new ConcreteBuilderB();
Director* director=new Director();
director->Construct(builder);
Product* product=builder->GetResult();
list<string> strLst=product->getLog();
list<string>::iterator it;
for(it=strLst.begin();it!=strLst.end();it++){
cout<<*it<<endl;
}
delete builder;
delete director;
return 0;
}