定义:
是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节。(例:大师兄用自己的猴毛变成小猴子)
工作原理:
通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。
浅拷贝:复制对象时,基本数据类型进行值传递,引用数据类型引用传递。
public class mom implements Cloneable, Serializable{
String name;
int age;
public mom(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "mom{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class sheep implements Cloneable{
String name;
int age;
mom Mom;
public sheep(String name, int age, mom mom) {
this.name = name;
this.age = age;
Mom = mom;
}
@Override
public String toString() {
return "sheep{" +
"name='" + name + '\'' +
", age=" + age +
", Mom=" + Mom +
'}';
}
@Override
protected Object clone(){
sheep s=null;
try {
s=(sheep) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.getMessage());
}
return s;
}
public static void main(String[] args){
sheep lily = new sheep("lily", 1,new mom("moli",44));
sheep lili=(sheep)lily.clone();
System.out.println(lily.hashCode()+"-----"+lily.Mom.hashCode());
System.out.println(lili.hashCode()+"-----"+lili.Mom.hashCode());
}
}
运行结果:
lily和lili哈希值不同,说明是两个不同对象,他们的Mom哈希值相同,是同一对象。
深拷贝:复制对象时,基本数据类型进行值传递,为所有引用数据类型申请存储空间。
实现方式有两种:
- 重写clone()实现深拷贝。
- 通过对象序列化实现深拷贝。
public class deepSheep implements Cloneable{//重写clone()实现深拷贝。
String name;
int age;
mom Mom;
public deepSheep(String name, int age, mom mom) {
this.name = name;
this.age = age;
Mom = mom;
}
@Override
public String toString() {
return "deepSheep{" +
"name='" + name + '\'' +
", age=" + age +
", Mom=" + Mom +
'}';
}
@Override
protected Object clone() {
deepSheep sheep=null;
try {
sheep=(deepSheep)super.clone();
sheep.Mom=(mom)Mom.clone();
}catch (Exception e){
System.out.println(e.getMessage());
}
return sheep;
}
public static void main(String[] args) {
mom mama = new mom("mama", 44);
deepSheep yiyi = new deepSheep("yiyi", 1, mama);
deepSheep dsheep = (deepSheep)yiyi.clone();
System.out.println("yiyi.hashCode()"+yiyi.hashCode()+"----"+"yiyi.Mom.hashCode()"+yiyi.Mom.hashCode());
System.out.println("dsheep.hashCode()"+dsheep.hashCode()+"----"+"dsheep.Mom.hashCode()"+dsheep.Mom.hashCode());
}
}
运行结果:
import java.io.*;
public class deepSheep2 implements Serializable {//通过对象序列化实现深拷贝。
String name;
int age;
mom Mom;
public deepSheep2(String name, int age, mom mom) {
this.name = name;
this.age = age;
Mom = mom;
}
@Override
public String toString() {
return "deepSheep{" +
"name='" + name + '\'' +
", age=" + age +
", Mom=" + Mom +
'}';
}
public deepSheep2 copy(){
ByteArrayOutputStream aout = new ByteArrayOutputStream();
ObjectOutputStream outs= null;
ByteArrayInputStream ain=null;
ObjectInputStream ins=null;
try {
outs = new ObjectOutputStream(aout);
outs.writeObject(this);
ain=new ByteArrayInputStream(aout.toByteArray());
ins=new ObjectInputStream(ain);
deepSheep2 sheep2=(deepSheep2)ins.readObject();
return sheep2;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
aout.close();
outs.close();
ain.close();
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
mom mama = new mom("mama", 55);
deepSheep2 yiyi = new deepSheep2("yiyi", 1, mama);
deepSheep2 dsheep = (deepSheep2)yiyi.copy();
System.out.println("yiyi.hashCode()"+yiyi.hashCode()+"----"+"yiyi.Mom.hashCode()"+yiyi.Mom.hashCode());
System.out.println("dsheep.hashCode()"+dsheep.hashCode()+"----"+"dsheep.Mom.hashCode()"+dsheep.Mom.hashCode());
}
}
运行结果: