/*Product.h*/
#ifndef PRODUCT_H
#define PRODUCT_H
class Product
{
public:
Product();
~Product();
protected:
private:
};
#endif
/*Product.cpp*/
#include "Product.h"
Product::Product()
{
}
Product::~Product()
{
}
/*Builder.h*/
#ifndef BUILDER_H
#define BUILDER_H
#include <string>
class Product
{
public:
virtual ~Builder();
virtual void BuildPartA(const string &build_para)=0;
virtual void BuildPartB(const string &build_para)=0;
virtual void BuildPartC(const string &build_para)=0;
virtual Product *GetProduct()=0;
protected:
Builder();
private:
};
class ConcreteBuilder:public Builder
{
public:
ConcreteBuilder();
~ConcreteBuilder();
void BuilderPartA(const string &build_para);
void BuilderPartB(const string &build_para);
void BuilderPartC(const string &build_para);
Product *GetProduct();
};
#endif
/*Builder.cpp*/
#include "Builder.h"
#include "Product.h"
#include <iostream>
Builder::Builder()
{
}
Builder::~Builder()
{
}
ConcreteBuilder::ConcreteBuilder()
{
}
ConcreteBuilder::~ConcreteBuilder()
{
}
ConcreteBuilder::BuildPartA(const string &build_para)
{
std::cout<<"build_para"<<std::endl;
}
ConcreteBuilder::BuildPartB(const string &build_para)
{
std::cout<<"build_para"<<std::endl;
}
ConcreteBuilder::BuildPartC(const string &build_para)
{
std::cout<<"build_para"<<std::endl;
}
ConcreteBuilder *ConcreteBuilder::GetProduct()
{
BuildPartA("Defined");
BuildPartB("Defined");
BuildPartC("Defined");
return new Product();
}
/*Director.h*/
#ifndef DIRECTOR_H
#define DIRECTOR_H
class Builder
class Director
{
public:
Director(Builder *bld);
~Director();
void Construct();
protected:
private:
Builder *bld_;
};
#endif
/*Director.cpp*/
#include "Diretor.h"
#include "Builder.h"
Director::Director(Builder *bld)
{
bld_=bld;
}
Director::~Director()
{
}
void Director::Construct()
{
bld_->BuildPartA("Defined");
bld_->BuildPartB("Defined");
bld_->BuildPartC("Defined");
}
/*main.cpp*/
#include <iostream>
#include "Product.h"
#include "Builder.h"
#include "Director.h"
int main()
{
Director *d=new Director(new ConcreteBuilder());
d->Construct();
return 0;
}
时间: 2024-11-04 00:46:40