• 设计模式生成器实现C++


    /*********************************
    *设计模式--生成器实现
    *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;
    }
    
    
  • 相关阅读:
    学习笔记10-用户和组
    学习笔记9-环境变量
    学习笔记8-检测磁盘空间
    学习笔记7-监测程序
    学习笔记6-权限管理
    【数学】矩阵的逆
    【数学】矩阵
    【数学】Polya定理
    【图论】必经点和必经边
    【图论】点双连通分量
  • 原文地址:https://www.cnblogs.com/newwy/p/1855200.html
Copyright © 2020-2023  润新知