newwy
奋斗在IT路上的小蜗牛。一步一步往上爬,爬到小牛,在到大牛,然后是神牛,然后是犇,然后就可以离开IT行业,回归大自然了。 远离IT,珍爱生命!!! 记录学习的点滴。
/*********************************
*设计模式--生成器实现
*C++语言
*Author:WangYong
*Blog:http://www.cnblogs.com/newwy
********************************/
#include <iostream>
using namespace std;
class Product
{
	public:
	Product(){cout<<"return a product"<<endl;}
	~Product(){}
	void ProducePart(){cout<<"build part of product"<<endl;}
}; 
class ProductPart
{
	public:
	ProductPart(){cout<<"Build part of product"<<endl;}
	~ProductPart(){}
	
	ProductPart* BuildPart(){return new ProductPart;}
};

class Builder 
{
	public:
	virtual ~Builder(){};
	virtual void BuilderPartA(const string & buildpara) = 0;
	virtual void BuilderPartB(const string & buildpara) = 0;
	virtual void BuilderPartC(const string & buildpara) = 0;
	virtual Product * GetProduct() = 0;
};

class ConcreteBuilder:public Builder
{
	public:
	ConcreteBuilder(){}
	~ConcreteBuilder(){}
	void BuilderPartA(const string & buildpara){cout<<"step:buildparaA.."<<buildpara<<endl;}
	void BuilderPartB(const string & buildpara){cout<<"step:buildparaB.."<<buildpara<<endl;}
	void BuilderPartC(const string & buildpara){cout<<"step:buildparaC.."<<buildpara<<endl;}
	Product * GetProduct();
	
};
Product* ConcreteBuilder:: GetProduct()
{
	BuilderPartA("pre defined");
	BuilderPartB("pre defined");
	BuilderPartC("pre defined");
	return new Product();
}
class Director
{
	public:
	Director(Builder *bld){_bld = bld;}
	~Director(){}
	void Constructor();
	private:
	Builder *_bld;
};
void Director::Constructor()
{
	_bld->BuilderPartA("user defined");
	_bld->BuilderPartB("user defined");
	_bld->BuilderPartC("user defined");
}
int main()
{
	Director * d = new Director(new ConcreteBuilder());
	d->Constructor();
	return 0;
}

posted on 2010-10-18 23:32  newwy  阅读(377)  评论(0编辑  收藏  举报