原型模式(Prototype):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式分为浅复制、深复制
一、浅复制
ShallowWorkExperience.java类
package prototype; public class ShallowWorkExperience { 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; } }
ShallowResume.java类
package prototype; /** * 浅复制 * 字段是值类型的,则该字段执行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象。 * 被复制对象的所有变量都含有与原来的对象相同的值,而所有的其他对象的引用都依旧指向同一个对象 * @author 煞笔 * */ public class ShallowResume implements Cloneable { private String name; private String sex; private int age; private ShallowWorkExperience work; public ShallowResume(String name) { this.name = name; this.work = new ShallowWorkExperience(); } public void setPersonInfo(int age,String sex){ this.age = age; this.sex = sex; } public void setWorkExperience(String timeArea,String company){ work.setCompany(company); work.setTimeArea(timeArea); } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } public void display(){ System.out.println("姓名:"+name+",年龄:"+age+",性别:"+sex); System.out.println("工作经验:"+work.getTimeArea()); System.out.println("工作公司:"+work.getCompany()); } }
ShallowBusiness.java类
package prototype; /** * * @author 煞笔 *显示结果:姓名:思思,年龄:22,性别:男 工作经验:2017-06-03-------目前 工作公司:公司3 姓名:思思,年龄:22,性别:男 工作经验:2017-06-03-------目前 工作公司:公司3 姓名:思思,年龄:22,性别:男 工作经验:2017-06-03-------目前 工作公司:公司3 */ public class ShallowBusiness { public static void main(String[] args) { try { ShallowResume re = new ShallowResume("思思"); re.setPersonInfo(22, "男"); re.setWorkExperience("2011-06-01-------2013-09-01", "公司1"); ShallowResume re1 = (ShallowResume)re.clone(); re1.setWorkExperience("2014-06-02-------2016-09-02", "公司2"); ShallowResume re2 = (ShallowResume)re.clone(); re2.setWorkExperience("2017-06-03-------目前", "公司3"); re.display(); re1.display(); re2.display(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
二、深复制
DeepWorkExperience.java类
package prototype; public class DeepWorkExperience implements Cloneable{ 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; } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } }
DeepResume.java类
package prototype; /** * 深复制:把需要复制的对象所引用的对象都复制一遍,即 把引用对象的变量都指向新复制的新对象, * @author 煞笔 * */ public class DeepResume implements Cloneable { private String name; private String sex; private int age; private DeepWorkExperience work; public DeepResume(String name) { this.name = name; this.work = new DeepWorkExperience(); } public DeepResume(DeepWorkExperience work) { super(); try { this.work = (DeepWorkExperience)work.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } public void setPersonInfo(int age,String sex){ this.age = age; this.sex =sex; } public void setWorkExperience(String timeArea,String company){ work.setCompany(company); work.setTimeArea(timeArea); } public void display(){ System.out.println("姓名:"+name+",年龄;"+age+"性别:"+sex); System.out.println("工作经验:"+work.getTimeArea()); System.out.println("工作公司:"+work.getCompany()); } public Object clone(){ DeepResume obj = new DeepResume(this.work); obj.name = this.name; obj.age = this.age; obj.sex = this.sex; return obj; } }
DeepBusiness.java类
package prototype; /** * 显示结果: * 姓名:思思,年龄;22性别:男 工作经验:2011-06-02-------2013-09-02 工作公司:公司1 姓名:思思,年龄;22性别:男 工作经验:2014-06-01-------2016-09-01 工作公司:公司2 姓名:思思,年龄;22性别:男 工作经验:2017-06-01-------目前 工作公司:公司3 * @author 煞笔 * */ public class DeepBusiness { public static void main(String[] args) { DeepResume re = new DeepResume("思思"); re.setPersonInfo(22, "男"); re.setWorkExperience("2011-06-02-------2013-09-02", "公司1"); DeepResume re1 = (DeepResume)re.clone(); re1.setWorkExperience("2014-06-01-------2016-09-01", "公司2"); DeepResume re2 = (DeepResume)re.clone(); re2.setWorkExperience("2017-06-01-------目前", "公司3"); re.display(); re1.display(); re2.display(); } }