一、介绍
Java中自带的原型模式是clone()方法。该方法是Object的方法,native类型。他的作用就是将对象的在内存的那一块内存数据一字不差地再复制一个。我们写简单类的时候只需要实现Cloneable接口,然后调用Object::clone方法就可实现克隆功能。这样实现的方式是浅拷贝。
1、深复制与浅复制:
(1)浅复制:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象;
//原型类
public class Resume implements Cloneable,Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private String age;
Resume(String name,String sex,String age){
this.name=name;
this.sex=sex;
this.age=age;
}
/**
* 设置个人信息
* @param sex
* @param age
*/
public void setPersonInfo(String sex,String age){
this.sex=sex;
this.age=age;
}
//描述
public void dispaly(){
System.out.println("姓名:"+name+"\t年龄:"+age+"\t性别:"+sex);
}
public Object clone(){
try {
Resume resume = (Resume)super.clone();
return resume;
}catch (Exception e){
return null;
}
}
}
客户端:
public class MainTest {
public static void main(String[] args){
Resume resume = new Resume("大鸟","男","25");
Resume resume2 = (Resume)resume.clone();
resume2.setPersonInfo("男","26");
resume.dispaly();
resume2.dispaly();
}
}
(2)深复制:把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
新增对象类
public class WorkExperience implements Cloneable{
private String workDate;
private String company;
public WorkExperience(String workDate,String company){
this.workDate=workDate;
this.company=company;
}
public WorkExperience(){}
/**省略set、get方法*/
public Object clone(){
try {
return super.clone();
}catch (Exception e){
return null;
}
}
}
public class Resume implements Cloneable,Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private String age;
private WorkExperience workExperience;
Resume(String name,String sex,String age){
this.name=name;
this.sex=sex;
this.age=age;
this.workExperience=new WorkExperience();
}
/**
* 添加私有构造 克隆工作经历
* @param workExperience
*/
private Resume(WorkExperience workExperience){
this.workExperience= (WorkExperience) workExperience.clone();
}
/**
* 设置个人信息
* @param sex
* @param age
*/
public void setPersonInfo(String sex,String age){
this.sex=sex;
this.age=age;
}
/**
* 设置工作经历
* @param workDate
* @param company
*/
public void setWorkExperience(String workDate, String company){
this.workExperience.setWorkDate(workDate);
this.workExperience.setCompany(company);
}
//描述
public void dispaly(){
System.out.println("姓名:"+name+"\t年龄:"+age+"\t性别:"+sex);
System.out.println("工作经历:"+workExperience.getWorkDate()+"\t"+workExperience.getCompany());
}
/**
* 重写了克隆方法
* @return
*/
public Object clone(){
try {
Resume resume = new Resume(this.workExperience);//调用私有构造器实现工作经历克隆
//给对象属性重新赋值--最终返回的是深复制的resume对象
resume.name=this.name;
resume.sex=this.sex;
resume.age=this.age;
return resume;
}catch (Exception e){
return null;
}
}
}
客户端
public class MainTest {
public static void main(String[] args){
Resume resume = new Resume("大鸟","男","25");
resume.setWorkExperience("2015-2016","南京工作");
Resume resume2 = (Resume)resume.clone();
resume2.setPersonInfo("男","26");
resume2.setWorkExperience("2016-2018","上海工作");
resume.dispaly();
resume2.dispaly();
}
}
二、应用:
1、数据集对象DataSet,有Clone()方法和Copy()方法,Clone用于复制结构而不复制数据,实现了原型模式的浅复制;Copy不但复制结构也复制数据,实现了原型模式的深复制。