服装企业网站模板站长素材免费下载

张小明 2026/1/13 6:57:16
服装企业网站模板,站长素材免费下载,网站开发需求方案,平面设计欣赏网站推荐一、原型模式 1.1、定义 原型实例指定创建对象的种类#xff0c;并通过拷贝这些原型#xff0c;创建新的对象。即克隆#xff0c;细胞分裂等。 1.2、核心思想 通过复制现有对象(原型)来创建新对象#xff0c;而不是通过new新建实例 1.3、为什么需要原型模式 问题场景 创建角…一、原型模式1.1、定义原型实例指定创建对象的种类并通过拷贝这些原型创建新的对象。即克隆细胞分裂等。1.2、核心思想通过复制现有对象(原型)来创建新对象而不是通过new新建实例1.3、为什么需要原型模式问题场景创建角色以孙悟空为例// 装备类classEquipment{private:std::string name;intpower;public:Equipment(std::string name,intpower):name(name),power(power){}Equipment*clone()const{returnnewEquipment(name,power);}std::stringgetName()const{returnname;}intgetPower()const{returnpower;}voiddisplay()const{std::cout name (威力:power)std::endl;}};classSunWuKong{private:std::string name;inthealth;intattack;std::vectorstd::stringskills;std::vectorEquipment*equipment;public:SunWuKong(std::string name,inthealth,intattack):name(name),health(health),attack(attack){// 初始化技能skills.push_back(七十二变);skills.push_back(筋斗云);skills.push_back(火眼金睛);skills.push_back(法天象地);skills.push_back(身外身);// 初始化装备equipment.push_back(newEquipment(金箍棒,1000));equipment.push_back(newEquipment(锁子黄金甲,500));equipment.push_back(newEquipment(凤翅紫静,300));equipment.push_back(newEquipment(藕丝步云履,200));}// 复制构造函数SunWuKong(constSunWuKongother):name(other.name),health(other.health),attack(other.attack),skills(other.skills),equipment(other.equipment){}// 拷贝赋值运算符SunWuKongoperator(constSunWuKongother){if(this!other){nameother.name;healthother.health;attackother.attack;skillsother.skills;equipmentother.equipment;}return*this;}~SunWuKong(){for(autoeq:equipment){deleteeq;}}};// 创建分身voidcreateCloneMonkey(){// 创建本体SunWuKong*originalnewSunWuKong(齐天大圣,1000,100);// 吹毛化兵创建分身vectorSunWuKong*clones;for(inti0;i100000;i){std::count创建第 i 个分身\n;SunWuKong*clonenewSunWuKong(*original);clones.push_back(clone);}deleteoriginal;// 删除分身for(autoclone:clones){deleteclone;}}问题点性能上每次创建分身时都需要初始化技能和装备效率低下灵活性差如果修改了本体分身不会自动进行更新浅拷贝问题容易导致双重释放或访问已释放内存解决方案使用原型模式之后创建原型抽象类classCharacterPrototype{public:virtual~CharacterPrototype(){}default;virtualCharacterPrototype*clone()const0;virtualvoiddisplay()const0;virtualstd::stringgetName()const0;virtualvoidsetName(std::string name)0;};具体原型类classSunWuKong:publicCharacterPrototype{private:std::string name;inthealth;intattack;std::vectorstd::stringskills;std::vectorEquipment*equipment;public:SunWuKong(std::string name,inthealth,intattack):name(name),health(health),attack(attack){// 初始化技能skills.push_back(七十二变);skills.push_back(筋斗云);skills.push_back(火眼金睛);skills.push_back(法天象地);// 初始化装备equipment.push_back(newEquipment(金箍棒,1000));equipment.push_back(newEquipment(锁子黄金甲,500));equipment.push_back(newEquipment(凤翅紫金冠,300));equipment.push_back(newEquipment(藕丝步云履,200));}// 拷贝构造函数(采用深拷贝)SunWuKong(constSunWuKongother){nameother.name;healthother.health;attackother.attack;skillsother.skills;equipment.clear();for(autoeq:other.equipment){equipment.push_back(eq-clone());}}// 拷贝赋值运算符(采用深拷贝)SunWuKongoperator(constSunWuKongother){if(this!other){nameother.name;healthother.health;attackother.attack;skillsother.skills;equipment.clear();for(autoeq:other.equipment){equipment.push_back(eq-clone());}}}// 克隆方法(核心)CharacterPrototype*clone()constoverride{returnnewSunWuKong(*this);// 调用拷贝构造函数}voidsetName(std::string name)override{this-namename;}std::stringgetName()constoverride{returnname;}voiddisplay()constoverride{std::coutname: this-name , health: this-health , attack: this-attack , skills: \n;for(constautoskill:skills){std::coutskill ;}std::cout\n;std::coutequipment:\n;for(constautoeq:equipment){eq-display();}}~SunWuKong(){for(autoeq:equipment){deleteeq;}}};原型管理器classPrototypeManager{private:std::unordered_mapstd::string,CharacterPrototype*prototypes;public:~PrototypeManager(){for(autopair:prototypes){deletepair.second;}}voidaddPrototype(std::string key,CharacterPrototype*prototype){prototypes[key]prototype;}CharacterPrototype*getPrototype(std::string key){if(prototypes.find(key)!prototypes.end()){returnprototypes[key]-clone();}returnnullptr;}};使用voidusePrototype(){// 1. 创建原型SunWukong*originalnewSunWukong(齐天大圣孙悟空,1500,150);original-display();std::cout\n--- 孙悟空吹毫毛变分身 ---\n;// 2. 创建多个分身std::vectorCharacterPrototype*clones;for(inti1;i30;i){// 使用克隆方法创建分身CharacterPrototype*cloneoriginal-clone();clone-setName(孙悟空分身to_string(i));clones.push_back(clone);}// 3. 分身展示for(autoclone:clones){clone-display();}// 4. 清理内存for(autoclone:clones){deleteclone;}deleteoriginal;}voidusePrototypeManager(){PrototypeManager manager;manager.addPrototype(孙悟空,newSunWuKong(齐天大圣孙悟空,1500,150));// 从管理器获取原型并克隆autocloneFromManagermanager.getPrototype(sunwukong);if(cloneFromManager){cloneFromManager-setName(管理器中克隆的分身);cloneFromManager-display();deletecloneFromManager;}}voidcomparison(){autostart1std::chrono::high_resolution_clock::now();// 传统方式创建10000个分身std::vectorSunWuKong*clones1;SunWuKong*originalnewSunWuKong(齐天大圣,1000,100);for(inti0;i10000;i){clones1.push_back(newSunWuKong(分身,1000,100));}autoend1std::chrono::high_resolution_clock::now();autostart2std::chrono::high_resolution_clock::now();// 原型模式创建10000个分身std::vectorCharacterPrototype*clones2;for(inti0;i10000;i){clones2.push_back(original-clone());}autoend2std::chrono::high_resolution_clock::now();std::cout传统方式耗时: std::chrono::duration_caststd::chrono::milliseconds(end1-start1).count()ms\n;std::cout原型模式耗时: std::chrono::duration_caststd::chrono::milliseconds(end2-start2).count()ms\n;}附上UML图:二、总结2.1、与传统方式进行对比特点传统创建方式原型模式性能低每次都要执行完整初始化高一次初始化多次复制内存每个对象独立内存可共享不变部分代码复杂度低高需要实现clone灵活性低高可动态修改原型适用场景对象创建简单对象创建复杂,批量创建2.2、深拷贝 VS 浅拷贝浅拷贝:亦可称为值拷贝。将源对象的值拷贝到目标对象中如果对象中某个成员是指针类型数据并且是在堆上创建的那么源对象和目标对象都指向同一块内存区域此时如果其中一个对象释放了内存那么另一个对象的指针就会变成野指针。深拷贝在拷贝的时候先开辟出与源对象大小一样的空间然后将源对象的内容拷贝到新开辟的空间中。这样无论哪个对象释放内存都不会影响另一个对象的正常使用。2.3、原型模式 VS 工厂模式原型模式通过克隆原型对象来创建新的对象适用于创建复杂对象特别是当对象的创建过程较为耗时或复杂时。工厂模式通过工厂方法来创建对象适用于创建不同类型的对象特别是当对象的创建逻辑较为复杂时。三、附加由于C语法的灵活性原型模式实现可以有多种方式1. 结合智能指针版–原型模式// 使用智能指针和移动语义classSunWuKong1:publicCharacterPrototype{private:string name;inthealth;intattack;vectorstringskills;vectorunique_ptrEquipmentequipment;// 使用unique_ptrpublic:SunWuKong1(string name,inthealth,intattack):name(name),health(health),attack(attack){// 初始化技能skills.push_back(七十二变);skills.push_back(筋斗云);skills.push_back(火眼金睛);skills.push_back(法天象地);// 初始化装备equipment.push_back(newEquipment(金箍棒,1000));equipment.push_back(newEquipment(锁子黄金甲,500));equipment.push_back(newEquipment(凤翅紫金冠,300));equipment.push_back(newEquipment(藕丝步云履,200));}// 使用移动构造函数提高效率SunWuKong1(SunWuKong1other)noexcept:name(move(other.name)),health(other.health),attack(other.attack),skills(move(other.skills)),equipment(move(other.equipment)){}CharacterPrototype*clone()constoverride{// 先创建一个副本autoclonemake_uniqueSunWuKong1(name,health,attack);// 深拷贝equipmentfor(constautoeq:equipment){clone-equipment.push_back(make_uniqueEquipment(*eq));}returnclone.release();}};2. 结合函数模板通过注册的方式–原型模式classPrototypeRegistry{private:staticunordered_mapstring,functionunique_ptrCharacterPrototype()registry;public:staticvoidregisterPrototype(conststringkey,functionunique_ptrCharacterPrototype()creator){registry[key]move(creator);}staticunique_ptrCharacterPrototypeclone(conststringkey){if(autoitregistry.find(key);it!registry.end()){returnit-second();}returnnullptr;}};
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

在柬埔寨做网站彩票推广济源市住房和城乡建设局网站公示

多线程编程全解析:从基础到同步与优化 1. 线程属性设置 线程的属性设置在多线程编程中起着重要作用,它能影响线程的执行顺序和调试便利性。 - 优先级设置 : Priority 属性可在不将线程设为后台线程的情况下影响其优先级。它有 Normal 、 AboveNormal 、 BelowNo…

张小明 2026/1/5 5:31:25 网站建设

自己电脑做服务器网站网站开发周记30篇

想要在最短时间内掌握Livox激光雷达的开发技巧吗?Livox-SDK2作为专为Livox激光雷达设计的开发套件,让复杂的激光雷达数据采集和设备控制变得简单直观。无论你是机器人开发者还是自动驾驶爱好者,这篇文章都将带你轻松入门。 【免费下载链接】L…

张小明 2026/1/5 11:34:04 网站建设

网站建设前十名公司内部管理软件叫什么

feishu-doc-export:3步完成飞书文档批量导出的终极指南 【免费下载链接】feishu-doc-export 项目地址: https://gitcode.com/gh_mirrors/fe/feishu-doc-export 还在为飞书文档迁移而烦恼吗?面对成百上千的文档,手动下载不仅效率低下&…

张小明 2026/1/10 18:37:08 网站建设

智能模板网站建设价格福田做网站联系电话

EmotiVoice在语音日记应用中的个性化表达实现 在一个人越来越依赖数字方式记录情绪的时代,写日记早已不再是纸笔之间的私密对话。从早年的博客到如今的语音备忘录,人们渴望的不仅是“被听见”,更是“被理解”。然而,当AI朗读你的文…

张小明 2026/1/5 11:34:00 网站建设

视觉元素网站网站建设定金合同

GPT-SoVITS在无障碍辅助技术中的价值体现 在渐冻症患者用眼神艰难拼出“我想喝水”的那一刻,如果系统能以他十年前的声音说出这句话——那不只是语音合成,而是一次尊严的回归。这正是GPT-SoVITS正在实现的事:它让那些逐渐失去言语能力的人&am…

张小明 2026/1/5 8:41:23 网站建设

浏览器怎么做能不拦截网站个人网站建设维护

Wan2.2-T2V-5B如何处理复合动作指令?“边走边挥手”测试 在短视频内容井喷的今天,创意团队常常面临一个尴尬局面:脑子里的画面清晰得不得了,“一个人走在阳光斑驳的林荫道上,一边走路一边热情地向镜头挥手”&#xff0…

张小明 2026/1/9 11:56:38 网站建设