原型模式(Prototype):用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
一、浅拷贝:被复制对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
package prototype.bitwisecopy;
/**
* 原型模式(Prototype):浅拷贝
* 工作经历类
*/
public class WorkExperience {
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;
}
}
package prototype.bitwisecopy;
/**
* 原型模式(Prototype):浅拷贝
* 简历类
*/
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public Resume(String name) {
this.name = name;
workExperience = new WorkExperience();
}
// 设置个人信息
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
// 设置工作经历
public void setWorkExperience(String workDate, String company) {
workExperience.setWorkDate(workDate);
workExperience.setCompany(company);
}
// 显示
public void display() {
System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age);
System.out.println("工作经历:" + workExperience.getWorkDate() + "," + workExperience.getCompany());
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package prototype.bitwisecopy;
/**
* 原型模式(Prototype):浅拷贝
* 客户端main方法
*/
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Resume r0 = new Resume("大鸟");
r0.setPersonalInfo("男", "29");
r0.setWorkExperience("1998-2000", "xx公司");
Resume r1 = (Resume) r0.clone();
r1.setWorkExperience("1998-2006", "yy企业");
Resume r2 = (Resume) r0.clone();
r2.setPersonalInfo("女", "18");
r2.setWorkExperience("1998-2003", "zz企业");
// 浅拷贝中工作经历的显示结果都是最后一次设置的值
r0.display();
r1.display();
r2.display();
}
}
输出结果:
姓名:大鸟,性别:男,年龄:29
工作经历:1998-2003,zz企业
姓名:大鸟,性别:男,年龄:29
工作经历:1998-2003,zz企业
姓名:大鸟,性别:女,年龄:18
工作经历:1998-2003,zz企业
二、深拷贝:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
package prototype.memberwisecopy;
/**
* 原型模式(Prototype):深拷贝
* 工作经历类
*/
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;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package prototype.memberwisecopy;
/**
* 原型模式(Prototype):深拷贝
* 简历类
*/
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
public Resume(String name) {
this.name = name;
this.workExperience = new WorkExperience();
}
public Resume(WorkExperience workExperience) throws CloneNotSupportedException {
this.workExperience = (WorkExperience) workExperience.clone();
}
// 设置个人信息
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
// 设置工作经历
public void setWorkExperience(String workDate, String company) {
workExperience.setWorkDate(workDate);
workExperience.setCompany(company);
}
// 显示
public void display() {
System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age);
System.out.println("工作经历:" + workExperience.getWorkDate() + "," + workExperience.getCompany());
}
@Override
public Object clone() throws CloneNotSupportedException {
Resume resume = new Resume(this.workExperience);
resume.name = this.name;
resume.sex = this.sex;
resume.age = this.age;
return resume;
}
}
package prototype.memberwisecopy;
/**
* 原型模式(Prototype):深拷贝
* 客户端main方法
*/
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
Resume r0 = new Resume("大鸟");
r0.setPersonalInfo("男", "29");
r0.setWorkExperience("1998-2000", "xx公司");
Resume r1 = (Resume) r0.clone();
r1.setWorkExperience("1998-2006", "yy企业");
Resume r2 = (Resume) r0.clone();
r2.setPersonalInfo("女", "18");
r2.setWorkExperience("1998-2003", "zz企业");
// 深拷贝中工作经历的显示结果各不相同
r0.display();
r1.display();
r2.display();
}
}
输出结果:
姓名:大鸟,性别:男,年龄:29
工作经历:1998-2000,xx公司
姓名:大鸟,性别:男,年龄:29
工作经历:1998-2006,yy企业
姓名:大鸟,性别:女,年龄:18
工作经历:1998-2003,zz企业