现有一只羊,姓名:tom,年龄:1,颜色:白色。克隆10只属性完全相同的羊
Sheep
package com.weirdo.prototype;
public class Sheep {
private String name;
private int age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
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 getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Client
package com.weirdo.prototype;
import com.alibaba.fastjson.JSONObject;
public class Client {
public static void main(String[] args) {
//传统的方法
Sheep sheep1 = new Sheep("tom",1,"白色");
Sheep sheep2 = new Sheep(sheep1.getName(),sheep1.getAge(),sheep1.getColor());
Sheep sheep3 = new Sheep(sheep1.getName(),sheep1.getAge(),sheep1.getColor());
Sheep sheep4 = new Sheep(sheep1.getName(),sheep1.getAge(),sheep1.getColor());
Sheep sheep5 = new Sheep(sheep1.getName(),sheep1.getAge(),sheep1.getColor());
//....
System.out.println("sheep1:"+JSONObject.toJSONString(sheep1));
System.out.println("sheep2:"+JSONObject.toJSONString(sheep2));
System.out.println("sheep3:"+JSONObject.toJSONString(sheep3));
System.out.println("sheep4:"+JSONObject.toJSONString(sheep4));
System.out.println("sheep5:"+JSONObject.toJSONString(sheep5));
//...
}
}
优点
缺点
改进思路
Sheep
package com.weirdo.prototype.improve;
public class Sheep implements Cloneable{
private String name;
private Integer age;
private String color;
public Sheep(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
@Override
protected Object clone() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Client
package com.weirdo.prototype.improve;
import com.alibaba.fastjson.JSONObject;
public class Client {
public static void main(String[] args) {
System.out.println("原型模式完成对象的创建");
Sheep sheep1 = new Sheep("tom", 1, "白色");
//克隆
Sheep sheep2 = (Sheep)sheep1.clone();
Sheep sheep3 = (Sheep)sheep1.clone();
Sheep sheep4 = (Sheep)sheep1.clone();
Sheep sheep5 = (Sheep)sheep1.clone();
System.out.println("sheep1=" + JSONObject.toJSONString(sheep1));
System.out.println("sheep2=" + JSONObject.toJSONString(sheep2));
System.out.println("sheep3=" + JSONObject.toJSONString(sheep3));
System.out.println("sheep4=" + JSONObject.toJSONString(sheep4));
System.out.println("sheep5=" + JSONObject.toJSONString(sheep5));
}
}
clone()
方法来实现的,即sheep=(Sheep)super.clone();
以上克隆羊的实例就是浅拷贝,但是不适合论证浅拷贝拷贝的是引用地址。
因为Sheep中的属性,age拷贝的是值,改变其大小不回影响拷贝后属性,name和color都是引用类型,无法改变其内容。
以下将详细论证浅拷贝,对于基本类型来说拷贝的是值,对于其他类型的数据来说拷贝的是引用地址。
Sheep
package com.weirdo.prototype.shallowClone;
public class Sheep implements Cloneable{
private String name;
private int age;
private String color;
private SheepWeight sheepWeight;
public Sheep(String name, int age, String color,SheepWeight sheepWeight) {
this.name = name;
this.age = age;
this.color = color;
this.sheepWeight = sheepWeight;
}
@Override
protected Object clone() {
Object obj = null;
try {
obj = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return obj;
}
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 getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public SheepWeight getSheepWeight() {
return sheepWeight;
}
public void setSheepWeight(SheepWeight sheepWeight) {
this.sheepWeight = sheepWeight;
}
}
SheepWeight
package com.weirdo.prototype.shallowClone;
public class SheepWeight {
private int sheepWeight;
public SheepWeight (int sheepWeight){
this.sheepWeight = sheepWeight;
}
public int getSheepWeight() {
return sheepWeight;
}
public void setSheepWeight(int sheepWeight) {
this.sheepWeight = sheepWeight;
}
}
Client
package com.weirdo.prototype.shallowClone;
import com.alibaba.fastjson.JSONObject;
public class Client {
public static void main(String[] args) {
System.out.println("浅拷贝实例");
//tom的体重
SheepWeight sheepWeight = new SheepWeight(20);
//new一个羊,并设置它的体重和其它属性
Sheep sheep1 = new Sheep("tom", 1, "白色",sheepWeight);
//开始拷贝
Sheep sheep2 = (Sheep)sheep1.clone();
Sheep sheep3 = (Sheep)sheep1.clone();
Sheep sheep4 = (Sheep)sheep1.clone();
Sheep sheep5 = (Sheep)sheep1.clone();
System.out.println("sheep1=" + JSONObject.toJSONString(sheep1));
System.out.println("sheep2=" + JSONObject.toJSONString(sheep2));
System.out.println("sheep3=" + JSONObject.toJSONString(sheep3));
System.out.println("sheep4=" + JSONObject.toJSONString(sheep4));
System.out.println("sheep5=" + JSONObject.toJSONString(sheep5));
System.out.println("去了一年非洲,tom吃胖了,颜色也变了");
//改变sheepWeight,sheepWeight属性在SheepWeight类中是一个基本类型,但在Sheep类中则是一个引用地址
//此时改变SheepWeight中的sheepWeight属性值,对于Sheep类来说引用地址不变但值变了,因此拷贝之后的属性也将跟着改变
sheepWeight.setSheepWeight(40);
//浅拷贝对于基本类型是直接拷贝的值,因此拷贝之后的属性不会跟着改变
sheep1.setAge(2);
//此处表面上看是改变了原型的color属性值,但实际上是改变了原型color的引用地址(无法改变String类型的内容)
//由于浅拷贝对于String类型是拷贝的引用地址,因此改变原型的color的引用地址,拷贝后的color引用地址不会变,也即拷贝后的color属性仍是指向“白色”的地址
sheep1.setColor("黑色");
//如果用一下方式改变原型的sheepWeight属性值,则拷贝后的数据不会受到影响,因为以下方式是改变了Sheep中对于sheepWeight的引用地址
//sheep1.setSheepWeight(new SheepWeight(45));
System.out.println("sheep1=" + JSONObject.toJSONString(sheep1));
System.out.println("sheep2=" + JSONObject.toJSONString(sheep2));
System.out.println("sheep3=" + JSONObject.toJSONString(sheep3));
System.out.println("sheep4=" + JSONObject.toJSONString(sheep4));
System.out.println("sheep5=" + JSONObject.toJSONString(sheep5));
}
}
运行结果
Person
package com.weirdo.prototype.deepClone;
public class Person implements Cloneable {
private String name;
private int age;
private String sex;
private Weight weight;
public Person(String name, int age, String sex, Weight weight) {
this.name = name;
this.age = age;
this.sex = sex;
this.weight = weight;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Weight getWeight() {
return weight;
}
public void setWeight(Weight weight) {
this.weight = weight;
}
/**
* 深拷贝 - 方式1 通过重写clone方法来实现
* @return
*/
@Override
protected Object clone() {
Person person = null;
try {
person = (Person)super.clone();
//对于引用类型单独处理
person.setWeight((Weight) weight.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return person;
}
}
Weight
package com.weirdo.prototype.deepClone;
import java.io.Serializable;
public class Weight implements Cloneable, Serializable {
private int weight;
public Weight(int weight){
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
protected Object clone() {
Weight weight = null;
try {
weight = (Weight)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return weight;
}
}
Client
package com.weirdo.prototype.deepClone;
import com.alibaba.fastjson.JSONObject;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//方式1:重写clone方法来实现深拷贝
Weight personWeight = new Weight(120);
Person person1 = new Person("小明",18,"男",personWeight);
Person person2 = (Person) person1.clone();
System.out.println("方式1:重写clone方法来实现深拷贝");
System.out.println("person1:"+ JSONObject.toJSONString(person1));
System.out.println("person2:"+ JSONObject.toJSONString(person2));
System.out.println("小明去了一趟泰国,一年后...");
person1.setAge(19);
person1.setSex("女");
personWeight.setWeight(95);
System.out.println("person1:"+ JSONObject.toJSONString(person1));
System.out.println("person2:"+ JSONObject.toJSONString(person2)+"------------>深拷贝,拷贝后的数据不受原来属性变更影响");
}
}
运行结果
Dog
package com.weirdo.prototype.deepClone;
import java.io.*;
public class Dog implements Serializable {
private String name;
private int age;
private String sex;
private Weight weight;
public Dog(String name,int age,String sex,Weight weight){
this.name = name;
this.age = age;
this.sex = sex;
this.weight = weight;
}
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Weight getWeight() {
return weight;
}
public void setWeight(Weight weight) {
this.weight = weight;
}
/**
* 深拷贝 - 方式2 通过对象的序列化实现 (推荐)
* @return
*/
public Dog serializableClone() {
//创建流对象
ByteArrayOutputStream bos = null;
ObjectOutputStream oos = null;
ByteArrayInputStream bis = null;
ObjectInputStream ois = null;
try {
//序列化
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
//当前这个对象以对象流的方式输出
oos.writeObject(this);
//反序列化
bis = new ByteArrayInputStream(bos.toByteArray());
ois = new ObjectInputStream(bis);
Dog copyObj = (Dog)ois.readObject();
return copyObj;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
//关闭流
try {
bos.close();
oos.close();
bis.close();
ois.close();
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
}
}
}
Weight
package com.weirdo.prototype.deepClone;
import java.io.Serializable;
public class Weight implements Cloneable, Serializable {
private int weight;
public Weight(int weight){
this.weight = weight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
protected Object clone() {
Weight weight = null;
try {
weight = (Weight)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return weight;
}
}
Client
package com.weirdo.prototype.deepClone;
import com.alibaba.fastjson.JSONObject;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//方式2:通过序列化来实现深拷贝
Weight dogWeight = new Weight(40);
Dog dog1 = new Dog("大黄",1,"公",dogWeight);
Dog dog2 = dog1.serializableClone();
System.out.println("方式2:通过序列化来实现深拷贝");
System.out.println("dog1:"+ JSONObject.toJSONString(dog1));
System.out.println("dog2:"+ JSONObject.toJSONString(dog2));
System.out.println("大黄去了一趟泰国,一年后...");
dog1.setAge(2);
dog1.setSex("母");
dogWeight.setWeight(35);
System.out.println("dog1:"+ JSONObject.toJSONString(dog1));
System.out.println("dog2:"+ JSONObject.toJSONString(dog2)+"------------>深拷贝,拷贝后的数据不受原来属性变更影响");
}
}
运行结果
简化对象的创建过程
,同时也能提高效率
缺点
:需要为每一个类配备一个克隆方法,这对全新的类来说不是很难。但对已有的类进行改造时,需要修改其源代码,违背了开闭原则(OCP)。