builder模式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <sstream> 5 using namespace std; 6 7 template<class T> 8 string ConvertToString(T value) { 9 stringstream ss; 10 ss << value; 11 return ss.str(); 12 } 13 14 class ExportHeaderModel { 15 public: 16 ExportHeaderModel(string strDepId, string strExportDate) : m_strDepId(strDepId), m_strExportDate(strExportDate) { 17 18 } 19 string getDepId() { 20 return m_strDepId; 21 } 22 string getExportDate() { 23 return m_strExportDate; 24 } 25 private: 26 string m_strDepId;//对账单的部门id 27 string m_strExportDate;//对账单的导出日期 28 }; 29 30 31 class ExportDataModel { 32 public: 33 ExportDataModel(string strTransId, double Quantity) : m_strTransId(strTransId), m_Quantity(Quantity) { 34 35 } 36 string getTransId() { 37 return m_strTransId; 38 } 39 double getQuantity() { 40 return m_Quantity; 41 } 42 private: 43 string m_strTransId;//交易Id 44 double m_Quantity; 45 }; 46 47 class ExportFooterModel { 48 public: 49 ExportFooterModel(string exportUser) : m_exportUser(exportUser) {} 50 string getExportUser() { 51 return m_exportUser; 52 } 53 private: 54 string m_exportUser; 55 }; 56 class Builder { 57 public: 58 virtual void builderHeader(ExportHeaderModel& ehm)=0; 59 virtual void builderBody(vector < ExportDataModel*>& edmCollection) = 0; 60 virtual void builderFoot(ExportFooterModel& efm) = 0; 61 virtual string getResult() = 0; 62 protected: 63 Builder(){} 64 }; 65 66 class TxtBuilder : public Builder { 67 public: 68 69 void builderHeader(ExportHeaderModel& ehm) { 70 m_strResulst.append(ehm.getDepId() + "," + ehm.getExportDate() + "\n"); 71 } 72 void builderBody(vector < ExportDataModel*>& edmCollection) { 73 for (vector<ExportDataModel*>::iterator iter = edmCollection.begin();iter != edmCollection.end();iter++) { 74 m_strResulst += (*iter)->getTransId() + ":" + ConvertToString((*iter)->getQuantity()) + "\n"; 75 } 76 } 77 void builderFoot(ExportFooterModel& efm) { 78 m_strResulst += efm.getExportUser() + "\n"; 79 } 80 81 string getResult() { 82 return m_strResulst; 83 } 84 public: 85 TxtBuilder() { 86 m_strResulst = ""; 87 } 88 private: 89 string m_strResulst; 90 }; 91 92 93 class XmlBuilder : public Builder { 94 public: 95 96 void builderHeader(ExportHeaderModel& ehm) { 97 m_strResulst.append("<?xml version='1.0' encoding='utf-8'>\n"); 98 m_strResulst.append("<Receipt>\n"); 99 m_strResulst.append(" <Header>\n"); 100 m_strResulst.append(" <DepId>"); 101 m_strResulst.append(ehm.getDepId() + "</DepId>\n"); 102 m_strResulst.append(" <ExportDate>" + ehm.getExportDate() + "</ExportDate>\n"); 103 m_strResulst.append(" </Header>\n"); 104 } 105 void builderBody(vector < ExportDataModel*>& edmCollection) { 106 m_strResulst.append(" <Body>\n"); 107 for (vector<ExportDataModel*>::iterator iter = edmCollection.begin();iter != edmCollection.end();iter++) { 108 m_strResulst.append(" <id>" + (*iter)->getTransId() + "</id>\n"); 109 m_strResulst.append(" <amount>" + ConvertToString((*iter)->getQuantity()) + "</amount>\n"); 110 } 111 m_strResulst.append(" </Body>\n"); 112 } 113 void builderFoot(ExportFooterModel& efm) { 114 m_strResulst.append(" <Footer>\n"); 115 m_strResulst.append(" <ExportUser>" + efm.getExportUser() + "</ExportUser>\n"); 116 m_strResulst.append(" </Footer>\n</Receipt>\n"); 117 } 118 119 string getResult() { 120 return m_strResulst; 121 } 122 public: 123 XmlBuilder() { 124 m_strResulst = ""; 125 } 126 private: 127 string m_strResulst; 128 }; 129 130 class Director { 131 public: 132 Director(Builder* pBuilder) : m_pBuilder(pBuilder) { 133 134 } 135 void construct(ExportHeaderModel& ehm, vector < ExportDataModel*>& edmCollection, ExportFooterModel& efm) { 136 m_pBuilder->builderHeader(ehm); 137 m_pBuilder->builderBody(edmCollection); 138 m_pBuilder->builderFoot(efm); 139 } 140 private: 141 Builder* m_pBuilder; 142 }; 143 int main(void) { 144 ExportHeaderModel* pEhm = new ExportHeaderModel("南京1支行", "6月1日"); 145 ExportDataModel* pEdm = new ExportDataModel("1", 10000.00f); 146 ExportDataModel* pEdm2 = new ExportDataModel("2", 20000.00f); 147 vector<ExportDataModel*> myVec; 148 myVec.push_back(pEdm); 149 myVec.push_back(pEdm2); 150 ExportFooterModel* pEfm = new ExportFooterModel("xcj"); 151 152 //ExportToTxtHelper helper; 153 //ExportToXmlHelper helper; 154 //helper.doExport(*pEhm, myVec, *pEfm); 155 Builder* pBuilder = new XmlBuilder(); 156 Director* pDirector = new Director(pBuilder); 157 pDirector->construct(*pEhm, myVec, *pEfm); 158 cout << pBuilder->getResult() << endl; 159 system("pause"); 160 return 0; 161 }