业务场景
未使用建造者模式实现
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
template<class T>
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
class ExportHeaderModel {
public:
ExportHeaderModel(string id, string date) :m_DepId(id), m_ExportDate(m_ExportDate) {
};
string getId() { return m_DepId; };
string getDate() { return m_ExportDate; };
private:
string m_DepId;
string m_ExportDate;
};
class ExportDataModel {
public:
ExportDataModel(string strTransId, double Quantity) :m_strTransID(strTransId), m_Quantity(Quantity) {};
string getStrTransId() { return m_strTransID; };
double getQuantity() { return m_Quantity; };
private:
string m_strTransID;
double m_Quantity;
};
class ExportFooterModel {
public:
ExportFooterModel(string user):m_exportUser(user) {}
string getExportUser() { return m_exportUser; };
private:
string m_exportUser;
};
class ExportToTxtHelper {
public:
void doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm);
};
void ExportToTxtHelper::doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm) {
//
string strTmp = "";
//1.文件头
strTmp += header.getId() + "," + header.getDate() + "\n";
//2.文件体
auto iter = edmCollection.begin();
for ( ; iter != edmCollection.end() ; iter++)
{
strTmp += (*iter)->getStrTransId() + ":" + ConvertToString((*iter)->getQuantity()) + "\n";
}
//3.文件尾
strTmp += efm.getExportUser() + "\n";
printf("******\n");
printf("%s", strTmp.c_str());
printf("******\n");
}
class ExportToXmlHelper {
public:
void doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm);
};
void ExportToXmlHelper::doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm) {
string strTmp = "";
strTmp.append("<?xml version='1.0' encoding='utf-8'>\n");
strTmp.append("<Root>\n");
strTmp.append(" <header>\n");
strTmp.append(" <DepId>\n");
strTmp.append(header.getId() + "</DepId>\n");
strTmp.append(" <Date>\n");
strTmp.append(header.getDate() + "</Date>\n");
strTmp.append(" </header>\n");
/*后面省略*/
printf("%s", strTmp.c_str());
}
int main(void) {
ExportHeaderModel* header = new ExportHeaderModel("NanJing", "6月1号");
auto data1 = new ExportDataModel("1", 1000.00f);
auto data2 = new ExportDataModel("2", 2000.00f);
vector<ExportDataModel*> mVec;
mVec.push_back(data1);
mVec.push_back(data2);
ExportFooterModel* footer = new ExportFooterModel("ZJ");
//ExportToTxtHelper helper;
ExportToXmlHelper helper;
helper.doExport(*header, mVec, *footer);
delete header;
delete data1;
delete data2;
delete footer;
}
使用建造者模式
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
template<class T>
string ConvertToString(T value) {
stringstream ss;
ss << value;
return ss.str();
}
class ExportHeaderModel {
public:
ExportHeaderModel(string id, string date) :m_DepId(id), m_ExportDate(m_ExportDate) {
};
string getId() { return m_DepId; };
string getDate() { return m_ExportDate; };
private:
string m_DepId;
string m_ExportDate;
};
class ExportDataModel {
public:
ExportDataModel(string strTransId, double Quantity) :m_strTransID(strTransId), m_Quantity(Quantity) {};
string getStrTransId() { return m_strTransID; };
double getQuantity() { return m_Quantity; };
private:
string m_strTransID;
double m_Quantity;
};
class ExportFooterModel {
public:
ExportFooterModel(string user) :m_exportUser(user) {}
string getExportUser() { return m_exportUser; };
private:
string m_exportUser;
};
/*
class ExportToTxtHelper {
public:
void doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm);
};
void ExportToTxtHelper::doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm) {
//
string strTmp = "";
//1.文件头
strTmp += header.getId() + "," + header.getDate() + "\n";
//2.文件体
auto iter = edmCollection.begin();
for (; iter != edmCollection.end(); iter++)
{
strTmp += (*iter)->getStrTransId() + ":" + ConvertToString((*iter)->getQuantity()) + "\n";
}
//3.文件尾
strTmp += efm.getExportUser() + "\n";
printf("******\n");
printf("%s", strTmp.c_str());
printf("******\n");
}
*/
/*
class ExportToXmlHelper {
public:
void doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm);
};
void ExportToXmlHelper::doExport(ExportHeaderModel& header, vector<ExportDataModel*>& edmCollection, ExportFooterModel& efm) {
string strTmp = "";
strTmp.append("<?xml version='1.0' encoding='utf-8'>\n");
strTmp.append("<Root>\n");
strTmp.append(" <header>\n");
strTmp.append(" <DepId>\n");
strTmp.append(header.getId() + "</DepId>\n");
strTmp.append(" <Date>\n");
strTmp.append(header.getDate() + "</Date>\n");
strTmp.append(" </header>\n");
printf("%s", strTmp.c_str());
}
*/
class Builder {
public:
virtual void builderHeader(ExportHeaderModel header) = 0;
virtual void builderBody(vector<ExportDataModel*>& data) = 0;
virtual void builderFoot(ExportFooterModel& footer)=0;
virtual string getResult() { return strTmp; };
protected:
Builder() { strTmp = ""; };
string strTmp;
};
class TxtBuiler : public Builder {
public:
virtual void builderHeader(ExportHeaderModel header) {
strTmp += header.getId() + "," + header.getDate() + "\n";
}
virtual void builderBody(vector<ExportDataModel*>& data) {
auto iter = data.begin();
for (; iter != data.end(); iter++)
{
strTmp += (*iter)->getStrTransId() + ":" + ConvertToString((*iter)->getQuantity()) + "\n";
}
}
virtual void builderFoot(ExportFooterModel& footer) {
strTmp += footer.getExportUser() + "\n";
}
};
class Director {
public:
Director(Builder* builder) : m_builder(builder){};
void builder(ExportHeaderModel& header, vector<ExportDataModel*>& data, ExportFooterModel& footer) {
m_builder->builderHeader(header);
m_builder->builderBody(data);
m_builder->builderFoot(footer);
}
private:
Builder* m_builder;
};
int main(void) {
ExportHeaderModel* header = new ExportHeaderModel("NanJing", "6月1号");
auto data1 = new ExportDataModel("1", 1000.00f);
auto data2 = new ExportDataModel("2", 2000.00f);
vector<ExportDataModel*> mVec;
mVec.push_back(data1);
mVec.push_back(data2);
ExportFooterModel* footer = new ExportFooterModel("ZJ");
//ExportToTxtHelper helper;
TxtBuiler builder;
Director director(&builder);
director.builder(*header, mVec, *footer);
cout << (&builder)->getResult();
delete header;
delete data1;
delete data2;
delete footer;
}
标签:void,建造,模式,header,strTmp,ExportFooterModel,iter,设计模式,append
From: https://www.cnblogs.com/xcodingfork/p/17134694.html