一、定义:
原型模式,用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
二、特点:
一般在初始化信息不发生变化的情况下,克隆是最好的方法。这既隐藏了对象创建的细节,又对性能是大大的提高。可以不用重新初始化对象,而是动态地获得对象运行时的状态。
浅复制:只复制它本身和其中包含的值的类型的成员变量,而引用类型的成员变量并没有被复制。所有的对其他对象的引用仍然指向原来的对象。
深复制:复制它本身和其中包含的所有成员变量。
三、示例代码:
浅复制:
//原型类,简历
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private WorkExperience work;
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
public Resume(String name) {
this.name = name;
work = new WorkExperience();
}
public void setWork(String workDate, String workCompany) {
work.setWorkCompany(workCompany);
work.setWorkDate(workDate);
}
public void display() {
System.out.println("name:"+name+" age:"+age+" sex:"+sex);
System.out.println("workExperience:"+work.getWorkCompany()+" "+work.getWorkDate());
}
public Resume clone(){
Object object=null;
try {
object = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return (Resume) object;
}
}
//引用对象,工作经历类
public class WorkExperience {
private String workDate;
private String workCompany;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getWorkCompany() {
return workCompany;
}
public void setWorkCompany(String workCompany) {
this.workCompany = workCompany;
}
}
//客户端类
public class Client {
public static void main(String[] args) {
Resume resume = new Resume("小鹏");
resume.setPersonalInfo("male", "20");
resume.setWork("2017-2018", "tencent");
Resume resume1 = resume.clone();
Resume resume2 = resume.clone();
//一个对象的引用类型发生变化,三个对象都发生了变化。值类型不变
resume1.setWork("23", "2334");
resume1.setPersonalInfo("23", "2334");
resume.display();
resume1.display();
resume2.display();
}
}
深复制:
//原型类
public class Resume implements Cloneable {
private String name;
private String sex;
private String age;
private WorkExperience work;
public void setPersonalInfo(String sex, String age) {
this.sex = sex;
this.age = age;
}
public Resume(String name) {
this.name = name;
work = new WorkExperience();
}
public void setWork(String workDate, String workCompany) {
work.setWorkCompany(workCompany);
work.setWorkDate(workDate);
}
public void display() {
System.out.println("name:" + name + " age:" + age + " sex:" + sex);
System.out.println("workExperience:" + work.getWorkCompany() + " " + work.getWorkDate());
}
public Resume clone() {
Object object = null;
try {
object = super.clone();
//引用类型和对象本身都需要克隆
Resume resume = (Resume) object;
resume.work = work.clone();
return (Resume) object;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
//工作经历类,引用类也需要实现cloneable接口
public class WorkExperience implements Cloneable {
private String workDate;
private String workCompany;
public String getWorkDate() {
return workDate;
}
public void setWorkDate(String workDate) {
this.workDate = workDate;
}
public String getWorkCompany() {
return workCompany;
}
public void setWorkCompany(String workCompany) {
this.workCompany = workCompany;
}
public WorkExperience clone(){
Object object=null;
try {
object=super.clone();
return (WorkExperience) object;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
//客户端
public class Client {
public static void main(String[] args) {
Resume resume = new Resume("小鹏");
resume.setPersonalInfo("male", "20");
resume.setWork("2017-2018", "tencent");
//深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象
Resume resume1 = resume.clone();
Resume resume2 = resume.clone();
resume1.setWork("23", "2334");
resume1.setPersonalInfo("23", "2334");
resume.display();
resume1.display();
resume2.display();
}
}