Builder模式的关键是其中的Director对象并不直接返回对象,而是通过一步步来一步步进行对象的创建。当然这里Director可以提供一个默认的返回对象的接口(即返回通用的复杂对象的创建,即不指定或者特定唯一指定BuilderProduct*中的参数)。
Product.h
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
class CProduct
{
public :
CProduct();
~CProduct();
};
#endif
Produce.cpp
#include "Product.h"
CProduct::CProduct()
{
}
CProduct::~CProduct()
{
}
Builder.h
#ifndef __BUILDER_H_
#define __BUILDER_H_
#include "Product.h"
#include <string>
#include <iostream>
using namespace std;
class CBuilder //As a Interface
{
public:
CBuilder();
~CBuilder();
virtual void BuilderProductA(const string A)=0;
virtual void BuilderProductB(const string B)=0;
virtual void BuilderProductC(const string C)=0;
virtual CProduct *GetProduct()=0;
};
#endif
Builder.cpp
#include "Builder.h"
CBuilder::CBuilder()
{
}
CBuilder::~CBuilder()
{
}
ConcreterBuilder.h
#ifndef __CONCERTERBUILDER_H_
#define __CONCERTERBUILDER_H_
#include "Product.h"
#include "Builder.h"
class CConcerterBuilder:public CBuilder
{
public:
CConcerterBuilder();
~CConcerterBuilder();
void BuilderProductA(const string A);
void BuilderProductB(const string B);
void BuilderProductC(const string C);
CProduct *GetProduct();
};
#endif
ConcreterBuilder.cpp
#include "ConcreterBuilder.h"
CConcerterBuilder::CConcerterBuilder()
{
}
CConcerterBuilder::~CConcerterBuilder()
{
}
void CConcerterBuilder::BuilderProductA(const string A)
{
cout<<A<<endl;
}
void CConcerterBuilder::BuilderProductB(const string B)
{
cout<<B<<endl;
}
void CConcerterBuilder::BuilderProductC(const string C)
{
cout<<C<<endl;
}
CProduct * CConcerterBuilder::GetProduct()
{
return new CProduct;
}
Main.cpp
#include <iostream>
#include "Director.h"
#include "ConcreterBuilder.h"
using namespace std;
int main()
{
CDirector *pDir=new CDirector(new CConcerterBuilder());
pDir->Construct();
delete pDir;
return 0;
}
标签:设计模式,const,Builder,CBuilder,模式,CProduct,void,include,CConcerterBuilder
From: https://blog.51cto.com/u_13566975/7272461