设计模式五(建造者模式,采用C++实现)

直接给实例。

说明:

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值