cpp: Builder Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | // Gold.h : 此文件包含 "Gold" 类。 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLD_H #define GOLD_H #include <iostream> #include <sstream> #include <vector> using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// /// </summary> class Gold { private : public : /// <summary> /// 做父类时析构函数应该为虚函数 /// </summary> virtual ~Gold() {} /* /// <summary> /// 参数:模型编号,形如“1253679201245”等,每些位的组合都有一些特别的含义,这里无需深究 /// </summary> /// <param name="strmodelno"></param> void Assemble(string strmodelno) { LoadOperationModel(strmodelno.substr(4, 3)); LoadIntellectualModel(strmodelno.substr(7, 3)); LoadResourceModel(strmodelno.substr(10, 3)); } /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) = 0; //这里也可以写为空函数体,子类决定是否重新实现 /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) = 0; /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) = 0;*/ }; } #endif // GoldCommerce.h : 此文件包含 "GoldCommerce" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCOMMERCE_H #define GOLDCOMMERCE_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 商贸商会 /// </summary> class GoldCommerce : public Gold { /**/ public : /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "商贸商会载入运营管理,最佳运营模式,需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "商贸商会载入智能技术管理,需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "商贸商会载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } }; } #endif // GoldCorporation.h : 此文件包含 "GoldCorporation" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCORPORATION_H #define GOLDCORPORATION_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 黄金公司 /// </summary> class GoldCorporation : public Gold { /* public: /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "黄金公司载入运营管理,最佳运营模式,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "黄金公司载入智能技术管理,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "黄金公司载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } */ }; } #endif // GoldShop.h : 此文件包含 "GoldShop" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDSHOP_H #define GOLDSHOP_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 连锁店舖 /// </summary> class GoldShop : public Gold { /* public: /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "连锁店舖载入运营管理,最佳运营模式,需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "连锁店舖载入智能技术管理,需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "连锁店舖载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; } */ }; } #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDBUILDER_H #define GOLDBUILDER_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 构建器父类 /// </summary> class GoldBuilder { private : public : /// <summary> /// /// </summary> /// <param name="strmodelno"></param> //void Assemble(string strmodelno) //参数:模型编号,形如“333333333333”等,每些位的组合都有一些特别的含义,这里无需深究 //{ // LoadOperationModel(strmodelno.substr(4, 3)); // LoadIntellectualModel(strmodelno.substr(7, 3)); // LoadResourceModel(strmodelno.substr(10, 3)); //} //返回指向Gold类的成员变量指针m_pGold,当一个复杂的对象构建完成后,可以通过该成员函数把对象返回 /// <summary> /// 返回指向Gold类的成员变量指针m_pGold,当一个复杂的对象构建完成后,可以通过该成员函数把对象返回 /// </summary> /// <returns></returns> Gold* GetResult() { return m_pGold; } /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) = 0; //这里也可以写为空函数体,子类决定是否重新实现 /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) = 0; /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) = 0; /// <summary> /// 做父类时析构函数应该为虚函数 /// </summary> virtual ~GoldBuilder() {} protected : Gold* m_pGold; //指向Gold类的成员变量指针 }; } #endif // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCOMMERCEBUILDER_H #define GOLDCOMMERCEBUILDER_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" #include "GoldBuilder.h" #include "GoldCommerce.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 商贸商会构建器类 /// </summary> class GoldCommerceBuilder : public GoldBuilder { public : GoldCommerceBuilder() //构造函数 { m_pGold = new GoldCommerce(); } /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "商贸商会载入运营管理,最佳运营模式,需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "商贸商会载入智能技术管理,需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "商贸商会载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldCommerce类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } }; } #endif // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCORPORATIONBUILDER_H #define GOLDCORPORATIONBUILDER_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" #include "GoldCorporation.h" #include "GoldBuilder.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 黄金公司构建器类 /// </summary> class GoldCorporationBuilder: public GoldBuilder { private : public : /// <summary> /// 构造函数 /// </summary> GoldCorporationBuilder() // { m_pGold = new GoldCorporation(); } /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "黄金公司载入运营管理,最佳运营模式,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "黄金公司载入智能技术管理,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "黄金公司载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } }; } #endif // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDSHOPBUILDER_H #define GOLDSHOPBUILDER_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" #include "GoldBuilder.h" #include "GoldShop.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// 连锁店舖构建器类 /// </summary> class GoldShopBuilder : public GoldBuilder { public : /// <summary> /// 构造函数 /// </summary> GoldShopBuilder() // { m_pGold = new GoldShop(); } /// <summary> /// 运营管理 operation /// </summary> /// <param name="strno"></param> virtual void LoadOperationModel(string strno) { cout << "连锁店舖载入运营管理,最佳运营模式,需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 智能技术 intellectual technology /// </summary> /// <param name="strno"></param> virtual void LoadIntellectualModel(string strno) { cout << "连锁店舖载入智能技术管理,需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } /// <summary> /// 资源配置 (整合)resource allocation /// </summary> /// <param name="strno"></param> virtual void LoadResourceModel(string strno) { cout << "连锁店舖载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldShop类或其父类中其他诸多成员函数,逻辑代码略......" << endl; //m_pGold->......略 } }; } #endif // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDDIRECTOR_H #define GOLDDIRECTOR_H #include <iostream> #include <sstream> #include <vector> #include "Gold.h" #include "GoldBuilder.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// CEO 指挥者类 /// </summary> class GoldDirector { public : /// <summary> /// 构造函数 /// </summary> /// <param name="ptmpBuilder"></param> GoldDirector(GoldBuilder* ptmpBuilder) { m_pGoldBuilder = ptmpBuilder; } /// <summary> /// 指定新的构建器 /// </summary> /// <param name="ptmpBuilder"></param> void SetBuilder(GoldBuilder* ptmpBuilder) { m_pGoldBuilder = ptmpBuilder; } /// <summary> /// 原GoldBuilder类中的Assemble成员函数 /// </summary> /// <param name="strmodelno"></param> /// <returns></returns> Gold* Construct(string strmodelno) //参数:模型编号,形如“1253679201245”等,每些位的组合都有一些特别的含义,这里无需深究 { m_pGoldBuilder->LoadOperationModel(strmodelno.substr(4, 3)); m_pGoldBuilder->LoadIntellectualModel(strmodelno.substr(7, 3)); m_pGoldBuilder->LoadResourceModel(strmodelno.substr(10, 3)); return m_pGoldBuilder->GetResult(); //返回构建后的对象 } private : GoldBuilder* m_pGoldBuilder; //指向所有构建器类的父类 }; } #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | // GoldBuilder.h : 此文件包含 "GoldBuilder" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GEOVINDU_H #define GEOVINDU_H using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// /// </summary> class GeovinDu { private : public : /// <summary> /// /// </summary> void displayBuilder(); }; } #endif // GeovinDu.cpp : 此文件包含 "GeovinDu" 类。原型模式 Builder Pattern C++ 14 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #include "GeovinDu.h" #include "Gold.h" #include "GoldBuilder.h" #include "GoldCorporation.h" #include "GoldDirector.h" #include "GoldShop.h" #include "GoldCommerce.h" #include "GoldCommerceBuilder.h" #include "GoldCorporationBuilder.h" #include "GoldShopBuilder.h" #include "GoldDirector.h" using namespace std; namespace DuJewelryBuilderPattern { /// <summary> /// /// </summary> void GeovinDu::displayBuilder() { /* DuJewelryBuilderPattern::Gold* pmonster = new DuJewelryBuilderPattern::GoldCommerce(); //创建 pmonster->Assemble("1253679201245"); //释放资源 delete pmonster; */ //DuJewelryBuilderPattern::GoldBuilder* pGoldBuilder = new GoldShopBuilder(); GoldBuilder* pGoldBuilder = new DuJewelryBuilderPattern::GoldCorporationBuilder(); //创建构建器类对象 DuJewelryBuilderPattern::GoldDirector* pDirector = new DuJewelryBuilderPattern::GoldDirector(pGoldBuilder); DuJewelryBuilderPattern::Gold* pGold = pDirector->Construct( "33333333333333333" ); //这里就构造出了一个对象 //释放资源 delete pGold; delete pDirector; delete pGoldBuilder; } } |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // ConsoleDuBuilderPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // Jewelry Builder Pattern 生成器模式 建造者模式、Builder // // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. // #define _UNICODE #include <iostream> #include "GeovinDu.h" #ifdef _DEBUG //只在Debug(调试)模式下 #ifndef DEBUG_NEW #define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定义new运算符 #define new DEBUG_NEW #endif #endif //#include <boost/type_index.hpp> using namespace std; //#pragma warning(disable : 4996) using namespace DuJewelryBuilderPattern; int main() { std::cout << "Hello World!!Programa Olá Mundo!涂聚文 Geovin Du\n" ; GeovinDu geovindu; geovindu.displayBuilder(); system ( "pause" ); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 #define UNICODE |
输出:
1 2 3 4 5 | Hello World!!Programa Olá Mundo!涂聚文 Geovin Du 黄金公司载入运营管理,最佳运营模式,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略...... 黄金公司载入智能技术管理,需要调用GoldCorporation类或其父类中其他诸多成员函数,逻辑代码略...... 黄金公司载入公司人力,资本,信息,技术,物流等资源的最佳配置(整合),需要调用GoldCorporation类或其父类中其他诸多成员函 数,逻辑代码略...... 请按任意键继续. . . |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
desgin patterns
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本