#include <stdio.h>
//c++ 23种设计模式之建造者模式,(生成器模式)
namespace DesignPattern_Builder
{
class Product1{
};
class Product2{
};
class Builder
{
public:
virtual void BuilderPartA(){}
virtual void BuilderPartB(){}
virtual void BuilderPartC(){}
protected:
Builder(){}//构造函数
};
//建造者1
class ConcreteBuilder1:public Builder
{
public:
ConcreteBuilder1():_product(NULL){}
virtual void BuilderPartA(){ printf("生成器1 生成 PartA \n");}
virtual void BuilderPartB(){ printf("生成器1 生成 PartB \n");}
virtual void BuilderPartC(){ printf("生成器1 生成 PartC \n");}
virtual Product1 *GetProduct1(){ return _product;}
private:
Product1 *_product;
};
//建造者2,生成器2
class ConcreteBuilder2:public Builder
{
public:
ConcreteBuilder2():_product(NULL){}
virtual void BuilderPartA(){ printf("生成器2 生成 PartA \n");}
virtual void BuilderPartB(){ printf("生成器2 生成 PartB \n");}
virtual void BuilderPartC(){ printf("生成器2 生成 PartC \n");}
virtual Product2 *GetProduct2(){ return _product;}
private:
Product2 *_product;
};
class Director
{
public:
void Construct(Builder *builder)
{
builder->BuilderPartA();
builder->BuilderPartB();
builder->BuilderPartC();
}
};
}
/**
void main()
{
using namespace DesignPattern_Builder;
Director director;
ConcreteBuilder1 *pBuilder1=new ConcreteBuilder1();
director.Construct(pBuilder1);
Product1 *product1=pBuilder1->GetProduct1();
if(product1!=NULL)
{
printf("生成器1 生成的产品不为空!\n");
}
ConcreteBuilder2 *pBuilder2=new ConcreteBuilder2();
director.Construct(pBuilder2);
Product2 *product2=pBuilder2->GetProduct2();
if(product1!=NULL)
{
printf("生成器2 生成的产品不为空!\n");
}
};
*/
c++ 23种设计模式之建造者模式
最新推荐文章于 2024-03-12 09:28:06 发布