对购物网站建设的建议怎样在wordpress设置伪静态
对购物网站建设的建议,怎样在wordpress设置伪静态,oppo手机网站建设需求分析,wordpress很卡一、原型模式
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;}};