您的位置:首页 > 编程语言 > C语言/C++

设计模式builder的C++实现源码

2010-05-21 16:57 696 查看
1、产品类Product定义

#ifndef PRODUCT_H
#define PRODUCT_H

#define STR_COLOR " Color: "
#define STR_SPACE " Space: "
#define STR_OTHER " Other: "

class Product
{
public:
Product();
void buildColor(char* color);
void buildSpace(int space);
void buildOther(char* other);
char* toString();
private:
int first;
char* color;
int space;
char* other;
char* res;
};

#endif

2、产品类Product实现

#include <stdio.h>
#include <string.h>
#include "Product.h"

Product::Product()
{
this->first = 0;
this->res = NULL;
}

void Product::buildColor(char* col)
{
if(first == 0)
{
first = 1;
}
this->color = col;
}

void Product::buildSpace(int space)
{
if(first == 0)
{
first = 2;
}
this->space = space;
}

void Product::buildOther(char* other)
{
this->other = other;
}

char* Product::toString()
{
char strspace[3];
sprintf(strspace,"%d",space);
if(res != NULL)
{
delete res;
}
int len = strlen(STR_COLOR) + strlen(color);
len += strlen(STR_SPACE) + strlen(strspace);
len += strlen(STR_OTHER) + strlen(other);
res = new char[len + 2];
switch(first)
{
case 1:
strcpy(res,STR_COLOR);
strcat(res,color);
strcat(res,STR_SPACE);
strcat(res,strspace);
strcat(res,STR_OTHER);
strcat(res, other);
strcat(res,"/n");
break;
case 2:
strcpy(res,STR_SPACE);
strcat(res,strspace);
strcat(res,STR_COLOR);
strcat(res,color);
strcat(res,STR_OTHER);
strcat(res, other);
strcat(res,"/n");
break;
default:
strcpy(res,STR_OTHER);
strcat(res, other);
strcat(res,STR_SPACE);
strcat(res,strspace);
strcat(res,STR_COLOR);
strcat(res,color);
strcat(res,"/n");
break;
}
return res;
}

3、构造产品抽象类Builder定义

#ifndef BUILDER_H
#define BUILDER_H
#include "Product.h"

class Builder
{
public:
virtual void CreatePartA(char* col, int spac) = 0;
virtual void CreatePartB(char* col, int spac) = 0;
virtual Product* GetProduct() = 0;
};

#endif

4、构造产品具体类ConcreteBuilder0定义

#ifndef CONCRETEBUILDER0_H
#define CONCRETEBUILDER0_H
#include "Builder.h"

class ConcreteBuilder0 : public Builder
{
public:
ConcreteBuilder0();
~ConcreteBuilder0();
void CreatePartA(char* col, int spac);
void CreatePartB(char* col, int spac);
Product* GetProduct();
private:
Product* p;
};
#endif

5、构造产品具体类ConcreteBuilder0实现

#include <stdio.h>
#include <string.h>
#include "ConcreteBuilder0.h"

ConcreteBuilder0::ConcreteBuilder0()
{
p = new Product();
}

ConcreteBuilder0::~ConcreteBuilder0()
{
if(p != NULL)
{
delete p;
p = NULL;
}
}

void ConcreteBuilder0::CreatePartA(char* col, int spac)
{
if(p == NULL)
{
return;
}
p->buildColor(col);
p->buildSpace(spac);
}

void ConcreteBuilder0::CreatePartB(char* col, int spac)
{
if(p == NULL)
{
return;
}
char stmp[3];
sprintf(stmp,"%d",spac);
int len = strlen(col) + strlen(" ") + strlen(stmp);
char* tmp = new char[len];
strcpy(tmp, col);
strcat(tmp," ");
strcat(tmp, stmp);
p->buildOther(tmp);
}

Product* ConcreteBuilder0::GetProduct()
{
return p;
}

6、构造产品具体类ConcreteBuilder1定义

#ifndef CONCRETEBUILDER1_H
#define CONCRETEBUILDER1_H
#include "Builder.h"

class ConcreteBuilder1 : public Builder
{
public:
ConcreteBuilder1();
~ConcreteBuilder1();
void CreatePartA(char* col, int spac);
void CreatePartB(char* col, int spac);
Product* GetProduct();
private:
Product* p;
};
#endif

7、构造产品具体类ConcreteBuilder1实现

#include <stdio.h>
#include <string.h>
#include "ConcreteBuilder1.h"

ConcreteBuilder1::ConcreteBuilder1()
{
p = new Product();
}

ConcreteBuilder1::~ConcreteBuilder1()
{
if(p != NULL)
{
delete p;
p = NULL;
}
}

void ConcreteBuilder1::CreatePartA(char* col, int spac)
{
if(p == NULL)
{
return;
}
p->buildSpace(spac);
p->buildColor(col);
}

void ConcreteBuilder1::CreatePartB(char* col, int spac)
{
if(p == NULL)
{
return;
}
char stmp[3];
sprintf(stmp,"%d",spac);
int len = strlen(col) + strlen(" ") + strlen(stmp);
char* tmp = new char[len];
strcpy(tmp, stmp);
strcat(tmp," ");
strcat(tmp, col);
p->buildOther(tmp);
}

Product* ConcreteBuilder1::GetProduct()
{
return p;
}

8、定义构造产品类的Director类定义

#ifndef DIRECTOR_H
#define DIRECTOR_H
#include "Builder.h"
class Director
{
public:
void CreateProduct(Builder* b);
};

#endif

9、定义构造产品类的Director类实现

#include <stdio.h>
#include "Director.h"

void Director::CreateProduct(Builder* b)
{
if(b == NULL)
{
return;
}
b->CreatePartA("Red",5);
b->CreatePartB("Blue",12);
}

10、客户使用类Client实现

#include <stdio.h>
#include "Product.h"
#include "ConcreteBuilder0.h"
#include "Builder.h"
#include "Director.h"
#include "ConcreteBuilder1.h"

int main()
{
Director* d = new Director();
Builder* b = new ConcreteBuilder0();
d->CreateProduct(b);
Product* p = b->GetProduct();
printf(p->toString());
delete b;
b = NULL;
b = new ConcreteBuilder1();
d->CreateProduct(b);
p = b->GetProduct();
printf(p->toString());
delete b;
b = NULL;
delete d;
d = NULL;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: