cpp: Prototype 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 268 269 270 271 272 | // Gold.h : 此文件包含 "Gold" 类。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLD_H #define GOLD_H #include <iostream> using namespace std; namespace DuJewelryPrototypePattern { /// <summary> /// 黄金 /// </summary> class Gold { public : /// <summary> /// 构造函数 /// </summary> /// <param name="life"></param> /// <param name="magic"></param> /// <param name="attack"></param> Gold( int technology, int business, int talents) :Gtechnology(technology), Gbusiness(business), Gtalents(talents) {} /// <summary> /// 做父类时析构函数应该为虚函数 /// </summary> virtual ~Gold() {} public : //virtual Monster* creatGold() = 0; //具体的实现在子类中进行 /// <summary> /// 具体的实现在子类中进行 /// </summary> /// <returns></returns> virtual Gold* clone() = 0; protected : //可能被子类访问的成员,用protected修饰 //属性 /// <summary> /// 技术 /// </summary> int Gtechnology; /// <summary> /// 业务 /// </summary> int Gbusiness; /// <summary> /// 人才 /// </summary> int Gtalents; }; } #endif // GoldCorporation.h : 此文件包含 "GoldCorporation" 类。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCORPORATION_H #define GOLDCORPORATION_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryPrototypePattern { /// <summary> /// 黄金公司 /// </summary> class GoldCorporation: public Gold { public : /// <summary> /// 构造函数 /// </summary> /// <param name="technology"></param> /// <param name="business"></param> /// <param name="talents"></param> GoldCorporation( int technology, int business, int talents) :Gold(technology, business, talents) { cout << "创建了一个黄金公司" << endl; } public : /// <summary> /// 拷贝构造函数 /// </summary> /// <param name="tmpobj"></param> GoldCorporation( const GoldCorporation& tmpobj) :Gold(tmpobj) { cout << "调用了GoldCorporation::GoldCorporation(const GoldCorporation& tmpobj)拷贝构造函数创建了一个黄金公司" << endl; } /// <summary> /// 拷贝构造函数 /// </summary> /// <returns></returns> virtual Gold* clone() { //return new GoldCorporation(800, 950, 880); //创建黄金公司 return new GoldCorporation(* this ); //触发拷贝构造函数的调用来创建了一个黄金公司 } //其他代码略.... }; } #endif // GoldCommerce.h : 此文件包含 "GoldCommerce" 类。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDCOMMERCE_H #define GOLDCOMMERCE_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryPrototypePattern { /// <summary> /// 商贸商会 /// </summary> class GoldCommerce : public Gold { public : /// <summary> /// 构造函数 /// </summary> /// <param name="technology"></param> /// <param name="business"></param> /// <param name="talents"></param> GoldCommerce( int technology, int business, int talents) :Gold(technology, business, talents) { cout << "创建了一个商贸商会" << endl; } public : /// <summary> /// 拷贝构造函数 /// </summary> /// <param name="tmpobj"></param> GoldCommerce( const GoldCommerce& tmpobj) :Gold(tmpobj) //初始化列表中注意对父类子对象的初始化 { cout << "调用了GoldCommerce::GoldCommerce(const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会" << endl; } /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* clone() { //return new GoldCommerce(800, 180, 990); //创建商贸商会 return new GoldCommerce(* this ); } //其他代码略.... }; } #endif // GoldShop.h : 此文件包含 "GoldShop" 类。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. #pragma once #ifndef GOLDSHOP_H #define GOLDSHOP_H #include <iostream> #include "Gold.h" using namespace std; namespace DuJewelryPrototypePattern { /// <summary> /// 连锁店舖 /// </summary> class GoldShop : public Gold { public : /// <summary> /// 构造函数 /// </summary> /// <param name="technology"></param> /// <param name="business"></param> /// <param name="talents"></param> GoldShop( int technology, int business, int talents) :Gold(technology, business, talents) { cout << "创建了一个连锁店舖" << endl; } public : /// <summary> /// 拷贝构造函数 /// </summary> /// <param name="tmpobj"></param> GoldShop( const GoldShop& tmpobj) :Gold(tmpobj) { cout << "调用了GoldShop::GoldShop(const GoldShop& tmpobj)拷贝构造函数创建了一个连锁店舖" << endl; } /// <summary> /// /// </summary> /// <returns></returns> virtual Gold* clone() { //return new GoldShop(800, 10, 110); //创建连锁店舖 return new GoldShop(* this ); } //其他代码略.... }; } #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 | // GeovinDu.h : 此文件包含 "GeovinDu" 类。。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du edit. #pragma once #ifndef GEOVINDU_H #define GEOVINDU_H using namespace std; namespace DuJewelryPrototypePattern { class GeovinDu { private : public : /// <summary> /// /// </summary> void displayGold(); }; } #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 | // GeovinDu.cpp : 此文件包含 "GeovinDu" 类。。原型模式 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du edit. #include "GeovinDu.h" #include "Gold.h" #include "GoldCommerce.h" #include "GoldCorporation.h" #include "GoldShop.h" namespace DuJewelryPrototypePattern { /// <summary> /// 全局的用于创建对象的函数 /// </summary> /// <param name="pGold"></param> void GlobalCreateGold(Gold* pGold) { /* Gold* ptmpco = nullptr; if (dynamic_cast<GoldCommerce*>(pGold) != nullptr) { ptmpco = new GoldCommerce(300, 50, 80); //创建商贸商会 } else if (dynamic_cast<GoldCorporation*>(pGold) != nullptr) { ptmpco = new GoldCorporation(200, 80, 100); //创建黄金公司 } else if (dynamic_cast<GoldShop*>(pGold) != nullptr) { ptmpco = new GoldShop(400, 0, 110); //创建连锁店舖 } if (ptmpco != nullptr) { //这里可以针对ptmpobj对象实现各种业务逻辑 //...... //不要忘记释放资源 delete ptmpco; } */ Gold* ptmpobj = pGold->clone(); //根据已有对象直接创建新对象,不需要知道已有对象所属的类型 //这里可以针对ptmpobj对象进行实现各种业务逻辑 //...... //不要忘记释放资源 delete ptmpobj; } /// <summary> /// /// </summary> void GeovinDu::displayGold() { /**/ DuJewelryPrototypePattern::GoldShop myPropMecGold(400, 0, 110); //创建连锁店舖对象作为原型对象以用于克隆目的 DuJewelryPrototypePattern::Gold* pmyPropGoldCommerce = new DuJewelryPrototypePattern::GoldCommerce(200, 80, 100); //创建商贸商会对象作为原型对象以用于克隆目的,这里可以直接new,也可以通过工厂模式创建原型对象,取决于程序员自己的喜好 //...... DuJewelryPrototypePattern::Gold* p_CloneObj1 = myPropMecGold.clone(); //使用原型对象克隆出新的连锁店舖对象 DuJewelryPrototypePattern::Gold* p_CloneObj2 = pmyPropGoldCommerce->clone(); //使用原型对象商贸商会对象 //可以对p_CloneObj1、p_CloneObj2所指向的对象进行各种操作(实现具体业务逻辑) //...... //释放资源 //释放克隆出来的怪物对象 delete p_CloneObj1; delete p_CloneObj2; //释放原型对象(堆中的) delete pmyPropGoldCommerce; DuJewelryPrototypePattern::Gold* pMonsterObj = new DuJewelryPrototypePattern::GoldCommerce(200, 80, 100); GlobalCreateGold(pMonsterObj); delete pMonsterObj; } } |
调用:
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 | // ConsoleDuPrototypePattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // 原型模式 Prototype Pattern // ConsoleDuPrototypePattern.cpp : 此文件包含 "ConsoleDuPrototypePattern" 类。 Prototype Pattern C++ 14 // 2023年5月1日 涂聚文 Geovin Du Visual Studio 2022 edit. 文章来源《C++新经典设计模式》 王健伟编著 清华大学出版社 #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 DuJewelryPrototypePattern; int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //程序退出时检测内存泄漏并显示到“输出”窗口 std::cout << "Hello World!!Programa Olá Mundo!涂聚文 Geovin Du\n" ; GeovinDu geovindu; geovindu.displayGold(); system ( "pause" ); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 #define UNICODE |
输出
1 2 3 4 5 6 7 8 | Hello World!!Programa Olá Mundo!涂聚文 Geovin Du 创建了一个连锁店舖 创建了一个商贸商会 调用了GoldShop::GoldShop( const GoldShop& tmpobj)拷贝构造函数创建了一个连锁店舖 调用了GoldCommerce::GoldCommerce( const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会 创建了一个商贸商会 调用了GoldCommerce::GoldCommerce( const GoldCommerce& tmpobj)拷贝构造函数创建了一个商贸商会 请按任意键继续. . . |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Cpp programming
标签:
desgin patterns
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2011-05-02 Passing Multiple Fields With GridView HyperLink Column