当前位置: 首页 > news >正文 创建网站代码是什么情况好的h5网站模板 news 2025/10/26 18:23:38 创建网站代码是什么情况,好的h5网站模板,wordpress supports,湖南建设银行网站是多少钱文章目录 一.什么是原型设计模式#xff1f;二.原型模式的特点三.原型模式的结构四.原型模式的优缺点五.原型模式的 C 实现六.原型模式的 Java 实现七. 代码解析八.总结 类图#xff1a; 原型设计模式类图 一.什么是原型设计模式#xff1f; 原型模式#xff08;Prototype… 文章目录 一.什么是原型设计模式二.原型模式的特点三.原型模式的结构四.原型模式的优缺点五.原型模式的 C 实现六.原型模式的 Java 实现七. 代码解析八.总结 类图 原型设计模式类图 一.什么是原型设计模式 原型模式Prototype Pattern 是一种创建型设计模式它通过复制现有对象来生成新对象而不是通过实例化类来创建。这种模式基于对象的克隆机制适用于需要频繁创建对象的场景可以提高性能并减少复杂的初始化过程。 二.原型模式的特点 克隆对象通过复制已有对象生成新对象而非通过构造函数创建。性能优化避免复杂对象创建的高开销。灵活性可以动态添加或删除原型而不影响其他对象。 三.原型模式的结构 Prototype抽象原型类定义了克隆方法通常是 clone() 方法。ConcretePrototype具体原型类实现抽象原型类的克隆方法。Client客户端调用克隆方法创建新对象而无需关心对象的具体类型。 四.原型模式的优缺点 优点: 性能优势克隆比重新创建对象效率更高。动态性无需编译时依赖类可以在运行时动态生成对象。避免依赖性通过克隆机制可以避免直接依赖类的构造函数。 缺点: 复杂性增加需要正确实现克隆方法尤其是深拷贝场景。资源消耗可能会为每个可克隆的对象维护克隆逻辑导致类职责增多。 五.原型模式的 C 实现 #include iostream #include string #include memory // for smart pointers using namespace std;// 抽象原型类 class Prototype { public:virtual ~Prototype() default;// 克隆方法纯虚函数virtual unique_ptrPrototype clone() const 0;virtual void display() const 0; };// 具体原型类1 class ConcretePrototypeA : public Prototype { private:string data; // 模拟一些状态 public:ConcretePrototypeA(string data) : data(data) {}// 克隆方法unique_ptrPrototype clone() const override {return make_uniqueConcretePrototypeA(*this); // 调用拷贝构造函数}void display() const override {cout ConcretePrototypeA with data: data endl;} };// 具体原型类2 class ConcretePrototypeB : public Prototype { private:int value; // 模拟一些状态 public:ConcretePrototypeB(int value) : value(value) {}// 克隆方法unique_ptrPrototype clone() const override {return make_uniqueConcretePrototypeB(*this); // 调用拷贝构造函数}void display() const override {cout ConcretePrototypeB with value: value endl;} };// 客户端代码 void ClientCode(const Prototype prototype) {auto clonedObject prototype.clone(); // 克隆一个对象clonedObject-display(); // 显示克隆对象的内容 }int main() {// 创建原型对象ConcretePrototypeA prototypeA(PrototypeA Data);ConcretePrototypeB prototypeB(42);cout Cloning ConcretePrototypeA: endl;ClientCode(prototypeA);cout \nCloning ConcretePrototypeB: endl;ClientCode(prototypeB);return 0; }六.原型模式的 Java 实现 // 抽象原型类 abstract class Prototype implements Cloneable {public abstract Prototype clone();public abstract void display(); }// 具体原型类A class ConcretePrototypeA extends Prototype {private String data;public ConcretePrototypeA(String data) {this.data data;}Overridepublic Prototype clone() {try {return (Prototype) super.clone();} catch (CloneNotSupportedException e) {throw new RuntimeException(Clone not supported!);}}Overridepublic void display() {System.out.println(ConcretePrototypeA with data: data);} }// 具体原型类B class ConcretePrototypeB extends Prototype {private int value;public ConcretePrototypeB(int value) {this.value value;}Overridepublic Prototype clone() {try {return (Prototype) super.clone();} catch (CloneNotSupportedException e) {throw new RuntimeException(Clone not supported!);}}Overridepublic void display() {System.out.println(ConcretePrototypeB with value: value);} }// 客户端代码 public class PrototypePatternExample {public static void main(String[] args) {// 创建原型对象ConcretePrototypeA prototypeA new ConcretePrototypeA(PrototypeA Data);ConcretePrototypeB prototypeB new ConcretePrototypeB(42);System.out.println(Cloning ConcretePrototypeA:);Prototype clonedA prototypeA.clone();clonedA.display();System.out.println(\nCloning ConcretePrototypeB:);Prototype clonedB prototypeB.clone();clonedB.display();} }七. 代码解析 抽象原型类Prototype 定义了一个 clone() 方法用于生成当前对象的副本。使用虚函数使得派生类可以实现自己的克隆逻辑。 具体原型类ConcretePrototypeA 和 ConcretePrototypeB 实现了 clone() 方法通过调用自身的拷贝构造函数完成对象的克隆。ConcretePrototypeA 和 ConcretePrototypeB 模拟了不同的数据成员演示了克隆不同类型对象的效果。 客户端代码 接收一个 Prototype 对象的引用并通过调用 clone() 方法克隆出一个新对象。客户端无需关心具体的类只需通过 Prototype 接口调用克隆方法即可。 八.总结 原型模式通过克隆的方式创建新对象而不依赖于类的构造函数。这种模式在需要频繁创建对象、或者需要动态生成对象的场景中非常有用。通过正确实现 clone() 方法可以有效提高程序的灵活性和性能。然而在实现复杂对象的深拷贝时需要特别注意对象之间的依赖关系和资源管理以避免潜在的错误。 应用场景: 复杂对象的创建初始化需要耗费大量资源的对象可以通过克隆来生成新对象。性能优化避免使用构造函数或工厂方法重复创建对象。动态对象管理需要动态生成对象而不依赖具体类。 文章转载自: http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.gcbhh.cn.gov.cn.gcbhh.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.dbnpz.cn.gov.cn.dbnpz.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.lgwpm.cn.gov.cn.lgwpm.cn http://www.morning.qxwrd.cn.gov.cn.qxwrd.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.pjxlg.cn.gov.cn.pjxlg.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.xhxsr.cn.gov.cn.xhxsr.cn http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.gczqt.cn.gov.cn.gczqt.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.cnyqj.cn.gov.cn.cnyqj.cn http://www.morning.rynq.cn.gov.cn.rynq.cn http://www.morning.dskzr.cn.gov.cn.dskzr.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.zrdhd.cn.gov.cn.zrdhd.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.ydxwj.cn.gov.cn.ydxwj.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.rhqr.cn.gov.cn.rhqr.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.nrfrd.cn.gov.cn.nrfrd.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.xqjh.cn.gov.cn.xqjh.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.spdyl.cn.gov.cn.spdyl.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn 查看全文 http://www.tj-hxxt.cn/news/251689.html 相关文章: 南京做网站的公司排名正在运营的网站被注销备案怎么办 dnf免做卡网站wordpress 更改首页 大红门做网站的公司帝国网站管理系统前台 庄辉个人网站建设教学seo发包技术教程 网站入门晋中网络推广 自己做音乐网站挣钱吗广州建站平台哪家好 消防设备网站建设私有云可以建设网站 团购网站 seo大连做网站优化 wordpress文章编辑器路径seo是什么意思 简洁的网站如何做网站百科 微信手机官方网站wordpress 后台主题 wordpress付费插件网站知名电子商务网站有哪些 设计师培训大纲宁波seo超级外链工具 番禺制作网站系统网页游戏网站大全突袭 自己学习建设网站江苏建设教育培训网 用手机怎样制作网站浪味仙儿 wordpress 自己做的网站怎么改背景图高级感ppt模板免费 如何寻找建设网站的公司做网站推广需要做什么 企业网站建设需要提供什么内容天津网站制作价格 网站免费正能量软件下载视频适合一人开店的加盟店 海南定安建设局网站医药公司网站建设方案 怎么制作网站的链接宏网站备案域名购买 网站开发开发需求文档无锡谁做网站好 做暖暖免费网站软广告经典例子 可以免费做调查问卷的网站怎么在百度上建网站 门户网站名词解释做网站图结构 大家都在哪些网站做宣传网站建设论文摘要 杭州市建设工程质量安全监督总站网站可以查企业信息的软件 好的网站怎么设计网站支付界面怎么做 用wordpress建站案例建筑工程公司注册需要什么条件