原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。
本设计模式通过创建简历的例子进行说明,并对对象的浅复制和深复制进行说明。
浅复制模式代码如下:
package chapter9;
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 setPersonInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
public void setWorlExperience(String timeArea, String company) {
this.timeArea = timeArea;
this.company = company;
}
public void display() {
System.out.printf("%s %s %s\n", name, sex, age);
System.out.printf("工作经历: %s %s\n", timeArea, company);
}
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
package chapter9;
public class Terminal {
public static void main(String[] args) {
Resume a = new Resume("大鸟");
a.setPersonInfo("男", "29");
a.setWorlExperience("1998-2000", "xxCompany");
Resume b = (Resume)a.clone();
b.setWorlExperience("1998-2006", "yyCompany");
Resume c = (Resume)a.clone();
c.setPersonInfo("男", "24");
a.display();
b.display();
c.display();
}
}
clone()方法对于值类型的字段会对该字段进行逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象,因此原始对象及其副本引用同一对象。
深复制代码如下:
package chapter9.deapClone;
public class WorkExperience implements Cloneable {
private String workDate;
private String company;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Object clone() {
try {
return super.clone();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package chapter9.deapClone;
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();
}
private Resume(WorkExperience work) {
this.work = (WorkExperience)work.clone();
}
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
public void setWorkExperience(String workDate, String company) {
work.setWorkDate(workDate);
work.setCompany(company);
}
public void display() {
System.out.printf("%s %s %s\n", name, sex, age);
System.out.printf("工作经历: %s %s\n", work.getWorkDate(), work.getCompany());
}
public Object clone() {
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}
package chapter9.deapClone;
public class Terminal {
public static void main(String[] args) {
Resume a = new Resume("大鸟");
a.setPersonalInfo("男", "29");
a.setWorkExperience("1998-2000", "xxCompany");
Resume b = (Resume)a.clone();
b.setWorkExperience("1998-2006", "yyCompany");
Resume c = (Resume)a.clone();
c.setPersonalInfo("男", "24");
c.setWorkExperience("1998-2003", "xxCompany");
a.display();
b.display();
c.display();
}
}