浅拷贝:实例的基本数据类型(8种),重新clone一个数据一样的实例对象,引用类型clone地址值,不重新生成引用对象副本引用类型不独立
public class Person implements Cloneable {
private String name;
private int age;
private String sex;
private List<String> friends;
public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", friends=" + friends +
'}';
}
/* @Override//单纯的引用地址值
protected Person clone(){
try {
return (Person) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}*/
@Override
protected Person clone() {
try {
Person person = (Person) super.clone();
List<String> friends = new ArrayList<>();
for (String friend : this.friends) {
friends.add(friend);
}
person.setFriends(friends);
return person;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return null;
}
}
深拷贝:实例的任何类型都clone一份,副本完全独立
public class MainClass {
public static void main(String[] args) {
Person person = new Person();
person.setAge(1);
person.setName("张三");
person.setSex("男");
/* Person person2=new Person();
person2.setAge(1);
person2.setName("李四");
person2.setSex("男");*/
/*Person person2 = person.clone();
person2.setName("李四");
System.out.println(person);
System.out.println(person2);*/
ArrayList<String> friends = new ArrayList<>();
friends.add("kobe");
friends.add("t-mac");
person.setFriends(friends);
Person person1 = person.clone();
friends.add("Iverson");
System.out.println(person);
System.out.println(person1);
}
}
浅拷贝单纯继承了Object的clone()方法,而深拷贝继承的同时也将引用类型的实例对象依次克隆。
小结:原型模式主要将需要创建的实例对象实现Cloneable接口,重写clone()方法,倘若需要主副分离就深拷贝,那样就互不影响,倘若为了节省在资源或者仅仅改变基本数据类型,那么浅拷贝也是不错的。