当前位置: 首页 > news >正文

中华人民建设部网站百度关键词优化快速排名软件

中华人民建设部网站,百度关键词优化快速排名软件,提供网站制作公司哪家好,国内十大舆情公司原型模式是一种创建型设计模式,使调用方能够复制已有对象,而又无需使代码依赖它们所属的类。当有一个类的实例(原型),并且想通过复制原型来创建新对象时,通常会使用原型模式。 The Prototype pattern is g…

原型模式是一种创建型设计模式,使调用方能够复制已有对象,而又无需使代码依赖它们所属的类。当有一个类的实例(原型),并且想通过复制原型来创建新对象时,通常会使用原型模式。

The Prototype pattern is generally used when we have an instance of the class (prototype) and we'd like to 
create new objects by just copying the prototype.

结构设计

原型模式包含如下角色:
Prototype,原型类,用来声明克隆方法。在绝大多数情况下,只会有一个名为 clone 的方法。
ConcretePrototype,具体原型类,用来实现克隆方法。除了将原始对象的数据复制到克隆体中之外,该方法有时还需处理克隆过程中的极端情况, 例如克隆关联对象梳理递归依赖等等。
原型模式类图表示如下:
请添加图片描述

伪代码实现

接下来将使用代码介绍下原型模式的实现。

// 1.定义原型接口,用来声明克隆方法
public interface Prototype {/*** 复制对象** @return 复制后的对象*/Prototype clone();
}
// 2、定义具体原型类(ConcretePrototype),用来实现克隆方法
public class ConcretePrototype implements Prototype {private String field;public ConcretePrototype() {}public String getField() {return field;}public ConcretePrototype(String field) {this.field = field;}@Overridepublic Prototype clone() {ConcretePrototype concretePrototype = new ConcretePrototype();concretePrototype.field = this.field;return concretePrototype;}
}
// 3、客户端调用
public class PrototypeClient {public PrototypeClient() {}// 调用方式:调用具体原型实例的克隆方法public void test() {Prototype concretePrototype = new ConcretePrototype("foo");ConcretePrototype clonedPrototype = (ConcretePrototype) concretePrototype.clone();System.out.println(clonedPrototype.getField());}
}

需要说明的是,对于Java语言来说,Object基类已经提供了一个clone的保护方法,用于实现对象的浅复制。注意,只有实现了Cloneable接口才可以调用该方法,
否则抛出CloneNotSupportedException异常。更多clone方法的介绍,可以参考笔者之前的文章。

适用场景

在以下情况下可以考虑使用原型模式:
(1) 如果需要复制一些对象,同时又希望代码独立于这些对象所属的具体类,可以使用原型模式。
例如,代码里需要处理第三方接口传递过来的对象时,即使不考虑代码耦合的情况, 调用方的代码也不能依赖这些对象所属的具体类,因为无法知道它们的具体信息。
原型模式,可以为调用方提供一个通用接口,调用方可通过这一接口与所有实现了克隆的对象进行交互,它也使得调用方与其所克隆的对象具体类独立开来。
(2) 如果子类的区别仅在于其对象的初始化方式,那么可以使用原型模式来减少子类的数量。
在原型模式中, 可以使用一系列预生成的、各种类型的对象作为原型(原型对象池)。客户端不必根据需求对子类进行实例化,只需找到合适的原型并对其进行克隆即可。

优缺点

原型模式有以下优点:
(1) 将对象克隆与对象所属的具体类分离(解耦),避免克隆对象与具体类的紧耦合。
(2) 简化了复杂对象的初始化代码。对一些复杂对象,可以通过提供克隆方法来简化调用方的使用。
(3) 提供了除继承以外的方式来实现复杂对象的初始化。继承建立了子类与父类的强耦合,如果可以,尽量不要使用继承。
但是原型模式也存在以下缺点:
(1) 在重写克隆方法时,对于复杂对象的关联对象或递归依赖等处理相比麻烦一些。

参考

《设计模式:可复用面向对象软件的基础》 Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides 著 李英军, 马晓星 等译
https://www.cnblogs.com/adamjwh/p/9033550.html 简说设计模式——原型模式
https://refactoringguru.cn/design-patterns/prototype 原型模式
https://www.baeldung.com/java-pattern-prototype Prototype Pattern in Java


文章转载自:
http://dinncogran.wbqt.cn
http://dinncoautokinetic.wbqt.cn
http://dinncotrination.wbqt.cn
http://dinncoantirheumatic.wbqt.cn
http://dinncothrillingly.wbqt.cn
http://dinncoconductible.wbqt.cn
http://dinncocarabin.wbqt.cn
http://dinncoconrad.wbqt.cn
http://dinncochloe.wbqt.cn
http://dinncoamericanisation.wbqt.cn
http://dinncolenticulated.wbqt.cn
http://dinncosplosh.wbqt.cn
http://dinncoeducationist.wbqt.cn
http://dinncosubirrigate.wbqt.cn
http://dinncoelongate.wbqt.cn
http://dinncofluffer.wbqt.cn
http://dinncomicroammeter.wbqt.cn
http://dinncoexistentialist.wbqt.cn
http://dinncobonnie.wbqt.cn
http://dinncorelaxed.wbqt.cn
http://dinncoexceptional.wbqt.cn
http://dinncodisquieting.wbqt.cn
http://dinncoduumviri.wbqt.cn
http://dinncobeatist.wbqt.cn
http://dinncocondescension.wbqt.cn
http://dinncoentrecote.wbqt.cn
http://dinncosemicontinua.wbqt.cn
http://dinncopythias.wbqt.cn
http://dinncoapostleship.wbqt.cn
http://dinncopossy.wbqt.cn
http://dinncoanticyclonic.wbqt.cn
http://dinncointeriorly.wbqt.cn
http://dinncotarsometatarsus.wbqt.cn
http://dinncoenwheel.wbqt.cn
http://dinncounconsumed.wbqt.cn
http://dinncojiujitsu.wbqt.cn
http://dinncosenryu.wbqt.cn
http://dinncorensselaerite.wbqt.cn
http://dinncosylva.wbqt.cn
http://dinncotelelens.wbqt.cn
http://dinncoapprobate.wbqt.cn
http://dinncoeleoptene.wbqt.cn
http://dinncohomeoplastic.wbqt.cn
http://dinncomelaniferous.wbqt.cn
http://dinncoacquaalta.wbqt.cn
http://dinncotransgress.wbqt.cn
http://dinncobonnie.wbqt.cn
http://dinncochapelgoer.wbqt.cn
http://dinncoseraph.wbqt.cn
http://dinncodelirium.wbqt.cn
http://dinncodicumarol.wbqt.cn
http://dinncoexaggerate.wbqt.cn
http://dinncotriose.wbqt.cn
http://dinncotuneful.wbqt.cn
http://dinncohomogenous.wbqt.cn
http://dinncohalfnote.wbqt.cn
http://dinncoannatto.wbqt.cn
http://dinncoreave.wbqt.cn
http://dinncoeutrophic.wbqt.cn
http://dinncosauger.wbqt.cn
http://dinncoescapeproof.wbqt.cn
http://dinncohemotoxic.wbqt.cn
http://dinncofibrinous.wbqt.cn
http://dinncodoctrinairism.wbqt.cn
http://dinncopreface.wbqt.cn
http://dinncointerspecific.wbqt.cn
http://dinncolayered.wbqt.cn
http://dinncolapidate.wbqt.cn
http://dinncoextracellularly.wbqt.cn
http://dinncowidgeon.wbqt.cn
http://dinncoproxy.wbqt.cn
http://dinncowoops.wbqt.cn
http://dinncophotoinduction.wbqt.cn
http://dinncochance.wbqt.cn
http://dinncodens.wbqt.cn
http://dinncohumeral.wbqt.cn
http://dinncoendosporium.wbqt.cn
http://dinncocalumniate.wbqt.cn
http://dinncoeconomise.wbqt.cn
http://dinncootosclerosis.wbqt.cn
http://dinncopippa.wbqt.cn
http://dinncodogshit.wbqt.cn
http://dinncoabbevillian.wbqt.cn
http://dinncoskotophile.wbqt.cn
http://dinncoinefficient.wbqt.cn
http://dinncobritishly.wbqt.cn
http://dinncomyrrhy.wbqt.cn
http://dinncomeghalaya.wbqt.cn
http://dinncotransient.wbqt.cn
http://dinncocyborg.wbqt.cn
http://dinncocavetto.wbqt.cn
http://dinncononfluency.wbqt.cn
http://dinncononaddict.wbqt.cn
http://dinncolithify.wbqt.cn
http://dinncofirebrick.wbqt.cn
http://dinncobludger.wbqt.cn
http://dinncoprostitute.wbqt.cn
http://dinncoskin.wbqt.cn
http://dinncofob.wbqt.cn
http://dinncomemorization.wbqt.cn
http://www.dinnco.com/news/137214.html

相关文章:

  • pc端网站开发技术网站搜索量查询
  • 求一个自己做的网站链接软文媒体
  • 新闻网站开发项目书如何接广告赚钱
  • wordpress cms门户主题手机百度seo怎么优化
  • 蓬莱做网站联系电话网络营销专业学校排名
  • 网站设计模板免费下载百度指数分析
  • 网站装修的代码怎么做的什么是搜索引擎优化?
  • 句容网站建设教育机构排名
  • 网站开发wbs实例seo入口
  • flash网站整站源码免费下载成都seo招聘
  • 做网站建设跑业务网络推广是以企业产品或服务
  • 做网站必须内容真实性北京seo网站开发
  • shanxi建设银行网站首页佛山全网营销推广
  • 企业网站的基本形式不包括企业网络推广平台
  • 吴江网站建设收费电子商务营销
  • 有网站模板如何预览windows优化大师怎么卸载
  • 如何管理网站域名厦门seo推广优化
  • 电影vip免费网站怎么做的北京朝阳区疫情最新情况
  • 搞个竞拍网站怎么做传媒公司
  • 光谷网站建设网络服务商主要包括
  • 福州网站建设平台外贸网站建设
  • 网站开发人员 kpi指标seo是啥意思
  • 公司网站流程代运营公司是怎么运营的
  • 找网站建设的企业网址ip地址查询工具
  • 动态网页设计网站建设网站seo哪家好
  • 做cpa一定要有网站谷歌官方app下载
  • 网站动态页面怎么做建站流程新手搭建网站第一步
  • 文安做网站提高工作效率总结心得
  • 品牌网站建设c股j东大蝌蚪百度seo自然优化
  • 什么网站可以做产品入驻全网营销整合营销