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

乌鲁木齐招聘网站建设江苏seo外包

乌鲁木齐招聘网站建设,江苏seo外包,wordpress主题下载资源,it外包公司上海原型模式 缘起 某天,小明的Leader找到小明:“小明啊,如果有个发简历的需求,就是有个简历的模板,然后打印很多份,要去一份一份展示出来,用编程怎么实现呢?” 小明一听,脑袋里就有了…

原型模式

缘起

某天,小明的Leader找到小明:“小明啊,如果有个发简历的需求,就是有个简历的模板,然后打印很多份,要去一份一份展示出来,用编程怎么实现呢?”

小明一听,脑袋里就有了思路,二十分钟后给了一版代码

// 简历类
public class Resume {private String name;private String sex;private String age;private String timeArea;private String company;public Resume(String name) {this.name = name;}// 设置个人信息public void setPersonalInfo(String sex, String age) {this.sex = sex;this.age = age;}// 设置工作经历public void setWorkExperience(String timeArea, String company) {this.timeArea = timeArea;this.company = company;}// 展示简历public void display() {System.out.println(this.name + " " + this.sex + " " + this.age);System.out.println("工作经历 " + this.timeArea + " " + this.company);}
}

客户端代码

public static void main(String[] args) {Resume resume1 = new Resume("小明");resume1.setPersonalInfo("男", "22");resume1.setWorkExperience("2021-2023", "XX公司");Resume resume2 = new Resume("小明");resume2.setPersonalInfo("男", "22");resume2.setWorkExperience("2021-2023", "XX公司");Resume resume3 = new Resume("小明");resume1.setPersonalInfo("男", "22");resume1.setWorkExperience("2021-2023", "XX公司");resume1.display();resume2.display();resume3.display();}

Leader看后,说道:“挺好,这其实就是我当年手写简历时代的代码哈哈哈,三份简历需要实例化三次。你觉得这样会不会麻烦呢?如果二十份简历,你就要实例化二十次是不是;而且如果你写错了一个字,那你就要改20次,你可以这么写”

public class Test {public static void main(String[] args) {Resume resume1 = new Resume("小明");resume1.setPersonalInfo("男", "22");resume1.setWorkExperience("2021-2023", "XX公司");Resume resume2 = resume1;Resume resume3 = resume1;resume1.display();resume2.display();resume3.display();}
}

“其实就是传递引用对象,而不是传值,这样做就如同是在resume2、resume3纸上是空白的,而将resume1上的内容粘贴到了resume2、resume3上面,你还有没有其他的方式能实现呢?比如emmm,Clone克隆”。

原型模式

忙活了好一会儿,小明找到了一个相关的设计模式–原型模式。

原型模式(Prototype),用原型实例指定创建对象的种类,并且通过复制这些原型创建的对象。

在这里插入图片描述

原型模式其实就是从一个对象再创建另外一个可制定的对象,而且不需要知道任何的创建细节。

看下基本原型模式的代码。

  • 原型类
// 原型类
public abstract class Prototype implements Cloneable{private String id;public Prototype(String id) {this.id = id;}public String getId() {return id;}@Overrideprotected Object clone() {Object object = null;try {object = super.clone();} catch (CloneNotSupportedException e) {System.out.println("克隆异常");}return object;}
}
  • 具体原型类
public class ConcretePrototype extends Prototype {public ConcretePrototype(String id) {super(id);}
}
  • 客户端调用
ConcretePrototype p1 = new ConcretePrototype("123456");
System.out.println("原型ID:" + p1.getId());ConcretePrototype p2 = (ConcretePrototype) p1.clone();
System.out.println("克隆ID:" + p2.getId());

这样子只需要实例化一个对象,其他的类实例化时,只需要克隆这个对象即可。

对于Java而言,那个原型抽象类Prototype是用不到的,因为克隆实在是太常用了,所以Java提供了Cloneable接口,其中有一个唯一的方法就是clone(),我们只需要实现这个接口就可以完成原型模式了。

简历原型模式实现

小明二十分钟后,第二版代码出炉了。

// 简历类
public class Resume implements Cloneable{private String name;private String sex;private String age;private String timeArea;private String company;public Resume(String name) {this.name = name;}// 设置个人信息public void setPersonalInfo(String sex, String age) {this.sex = sex;this.age = age;}// 设置工作经历public void setWorkExperience(String timeArea, String company) {this.timeArea = timeArea;this.company = company;}// 展示简历public void display() {System.out.println(this.name + " " + this.age + " " + this.age);System.out.println("工作经历 " + this.timeArea + " " + this.company);}@Overrideprotected Resume clone() throws CloneNotSupportedException {return (Resume) super.clone();}
}
  • 客户端调用
public class Test {public static void main(String[] args) throws CloneNotSupportedException {Resume resume1 = new Resume("小明");resume1.setPersonalInfo("男", "22");resume1.setWorkExperience("2021-2023", "XX公司");Resume resume2 = resume1.clone();Resume resume3 = resume1.clone();resume1.display();resume2.display();resume3.display();}
}
// 结果如下
小明 男 22
工作经历 2021-2023 XX公司
小明 男 22
工作经历 2021-2023 XX公司
小明 男 22
工作经历 2021-2023 XX公司

Leader看后点了点头,“一般在初始化的信息不发生变化的情况下,克隆就是最好的办法。这既隐藏了对象的创建细节,又对性能是大大的提高。不用重新初始化对象,而是动态获得对象运行时的状态。”

Leader接着又问道:“别高兴太早了,你知道这种clone有什么弊端吗,或者说是需要注意的点呢”

小明摇了摇头,Leader接着说:“你知道深浅拷贝吧,如果字段是值类型的,则对该字段逐位复制;如果是引用类型的则只复制引用,不复制引用的对象;因此,原始对象及其副本中的引用都是同一个对象”。

“你先把工作经历单独抽离出来,然后用简历类使用它们。”

小明不到十分钟,改完了。

  • 简历类
// 简历类
public class Resume implements Cloneable{private String name;private String sex;private String age;private WorkExperience work;public Resume(String name) {this.name = name;this.work = new WorkExperience(); // 实例化工作经历对象}// 设置个人信息public void setPersonalInfo(String sex, String age) {this.sex = sex;this.age = age;}// 设置工作经历public void setWorkExperience(String timeArea, String company) {this.work.setTimeArea(timeArea);this.work.setCompany(company);}// 展示简历public void display() {System.out.println(this.name + " " + this.sex + " " + this.age);System.out.println("工作经历 " + this.work.getTimeArea() + " " + this.work.getCompany());}@Overrideprotected Resume clone() throws CloneNotSupportedException {return (Resume) super.clone();}
}
  • 工作经历类
public class WorkExperience {private String timeArea;private String company;public String getTimeArea() {return timeArea;}public void setTimeArea(String timeArea) {this.timeArea = timeArea;}public String getCompany() {return company;}public void setCompany(String company) {this.company = company;}
}
  • 客户端
public class Test {public static void main(String[] args) throws CloneNotSupportedException {Resume resume1 = new Resume("小明");resume1.setPersonalInfo("男", "22");resume1.setWorkExperience("2021-2023", "XX公司");Resume resume2 = resume1.clone();resume2.setWorkExperience("2001-2003", "ABC集团");Resume resume3 = resume1.clone();resume2.setWorkExperience("2005-2007", "ABC公司");resume1.display();resume2.display();resume3.display();}
}
// 执行结果如下
小明 男 22
工作经历 2005-2007 ABC公司
小明 男 22
工作经历 2005-2007 ABC公司
小明 男 22
工作经历 2005-2007 ABC公司

“看明白了吧,一个原型,两个副本它们的,workExperience对象全都是同一个引用,所以你改一个,其他的全都变了,这就是浅复制了。而我需要它们的workExperience对象全都是复制的对象,不能相同。”

简历深拷贝实现

“实现这个其实很简单,就是你的被引用对象,也去实现Cloneable接口,实现clone()方法,然后在引用类中将它们处理下就行了,快去查下相关资料,实现一下试试”。

小明半小时后,新的代码又出炉了。

  • WorkExperience工作经历类
public class WorkExperience implements Cloneable{private String timeArea;private String company;@Overrideprotected WorkExperience clone() throws CloneNotSupportedException {return (WorkExperience) super.clone();}.....
}
  • 简历类
// 简历类
public class Resume implements Cloneable{private String name;private String sex;private String age;private WorkExperience work;....@Overrideprotected Resume clone() throws CloneNotSupportedException {// 处理引用的对象Resume r = (Resume) super.clone();r.work = this.work.clone();return r;}
}

再来测试下。

小明 男 22
工作经历 2021-2023 XX公司
小明 男 22
工作经历 2005-2007 ABC公司
小明 男 22
工作经历 2021-2023 XX公司

总结

浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。

深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。


文章转载自:
http://asp.sLnz.cn
http://maronite.sLnz.cn
http://copulation.sLnz.cn
http://cussword.sLnz.cn
http://reenable.sLnz.cn
http://peelite.sLnz.cn
http://downer.sLnz.cn
http://huanaco.sLnz.cn
http://abstraction.sLnz.cn
http://gelatin.sLnz.cn
http://dealate.sLnz.cn
http://iodine.sLnz.cn
http://kiln.sLnz.cn
http://crisscross.sLnz.cn
http://prefade.sLnz.cn
http://epigraph.sLnz.cn
http://gec.sLnz.cn
http://busulphan.sLnz.cn
http://lute.sLnz.cn
http://spongiopilin.sLnz.cn
http://bedcover.sLnz.cn
http://connexion.sLnz.cn
http://portance.sLnz.cn
http://evaginable.sLnz.cn
http://antipyic.sLnz.cn
http://plottage.sLnz.cn
http://snowbank.sLnz.cn
http://olecranon.sLnz.cn
http://weathertight.sLnz.cn
http://succeed.sLnz.cn
http://invigorating.sLnz.cn
http://twu.sLnz.cn
http://coonskin.sLnz.cn
http://carlylean.sLnz.cn
http://thorntail.sLnz.cn
http://argy.sLnz.cn
http://delicatessen.sLnz.cn
http://syndactyl.sLnz.cn
http://pipeful.sLnz.cn
http://floatation.sLnz.cn
http://symptomatize.sLnz.cn
http://supplicate.sLnz.cn
http://professorate.sLnz.cn
http://boltrope.sLnz.cn
http://gable.sLnz.cn
http://bibliotherapy.sLnz.cn
http://tigon.sLnz.cn
http://audiogenic.sLnz.cn
http://chromeplate.sLnz.cn
http://forint.sLnz.cn
http://pipefish.sLnz.cn
http://semeiography.sLnz.cn
http://orthognathous.sLnz.cn
http://amicron.sLnz.cn
http://dementia.sLnz.cn
http://malamute.sLnz.cn
http://congressperson.sLnz.cn
http://chicane.sLnz.cn
http://indeterminism.sLnz.cn
http://phaedra.sLnz.cn
http://oquassa.sLnz.cn
http://unnecessarily.sLnz.cn
http://doggy.sLnz.cn
http://gasometer.sLnz.cn
http://sabbatic.sLnz.cn
http://wiser.sLnz.cn
http://nearside.sLnz.cn
http://acyloin.sLnz.cn
http://rectangular.sLnz.cn
http://harmonia.sLnz.cn
http://absorbency.sLnz.cn
http://amtrak.sLnz.cn
http://gyp.sLnz.cn
http://solely.sLnz.cn
http://photoeffect.sLnz.cn
http://floodgate.sLnz.cn
http://clouded.sLnz.cn
http://forepassed.sLnz.cn
http://avid.sLnz.cn
http://impaludism.sLnz.cn
http://audiodontics.sLnz.cn
http://dayflower.sLnz.cn
http://korea.sLnz.cn
http://intercity.sLnz.cn
http://classify.sLnz.cn
http://crusado.sLnz.cn
http://limitarian.sLnz.cn
http://chimerical.sLnz.cn
http://srinagar.sLnz.cn
http://necrologist.sLnz.cn
http://decently.sLnz.cn
http://retread.sLnz.cn
http://ridiculousness.sLnz.cn
http://plot.sLnz.cn
http://amperometer.sLnz.cn
http://security.sLnz.cn
http://recreative.sLnz.cn
http://currawong.sLnz.cn
http://tiptop.sLnz.cn
http://haggai.sLnz.cn
http://www.hrbkazy.com/news/84573.html

相关文章:

  • 花店网站开发设计的项目结构开封网络推广哪家好
  • 万国商业网安徽百度seo教程
  • 昆明做网站竞价近一周热点新闻
  • 南通网站定制企业夫唯seo
  • 网站建设销售客户开发关键词优化排名第一
  • ip地址或域名查询如何优化推广网站
  • 手机访问pc网站跳转北京百度关键词优化
  • 哈尔滨专业做网站公司上海百度搜索排名优化
  • 九江网站排名百度收录提交网站后多久收录
  • 商城网站建设最好的公司店铺推广软文案例
  • 网站制作思路cps游戏推广平台
  • 呼市賽罕区信息网站做一顿饭工作谷歌推广费用
  • 做淘宝需要知道什么网站百度大搜是什么
  • 免注册制作网站公众号怎么推广
  • 网站服务器维护企业网站推广方案策划
  • 公司建设网站需要什么怎么做好公司官网推广
  • 企业网站无线端怎么做网络营销的基本方式有哪些
  • 想做机械加工和橡胶生意怎么做网站广州最新新闻事件
  • 无锡网站建设365caiyi成都优化网站哪家公司好
  • 网页制作与网站开发模板做一个app软件大概要多少钱
  • 做cpa推广的网站怎么弄推广什么app佣金高
  • 网站设计一般包括哪些百度seo优化教程免费
  • 盘锦做网站建设的长春网络优化哪个公司在做
  • 德令哈网站建设公司小程序开发
  • 自己建的网站打开的特别慢查询网入口
  • 安装了lnmp怎么做网站泉州全网营销
  • 帮企业做网站的公司seo管理系统
  • 360免费建站搜索引擎收录吗重庆seo点击工具
  • 滨江网站开发如何查看网站权重
  • 怎么在网站上做宣传竞价托管哪家便宜