一.提出问题
1.问题描述(克隆羊问题)
现在有一只羊tom。姓名为tom,年龄为1,颜色为白色。请编写程序创建和tom羊属性完全相同的10只羊。
2.UML类图
3.具体代码
Sheep类
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
public class Client {
public static void main(String[] args) {
Sheep sheep = new Sheep("tom",1,"白色");
Sheep sheep1 = new Sheep(sheep.getName(), sheep.getAge(), sheep.getColor());
/*
一直到10
*/
}
}
4.优缺点分析
优点
比较好理解,简单易操作
缺点
在创建新的对象时,总是需要重新获取原始对象的属性,如果创建的对象比较复杂时,效率很低
总是需要重新初始化对象,而不是动态地获得对象运行时的状态,不够灵活。
5.改进思路
Java中Object类是所有类的基类,Object类提供了一个clone方法,该方法可以将一个Java对象复制一份,但是需要实现clone的Java类必须要实现一个接口Cloneable。该接口表示该类能够复制且具有复制的能力。------------------------------------------>原型模式
二.原型模式(浅拷贝)
用原型实例指定创建对象的种类,并且通过拷贝这些原型,创建新的对象。使得程序具有更好的可扩展性。
原型模式是一种创建型设计模式,允许一个对象再创建另外一个可定制的对象,无需指导如何创建的细节
工作原理
通过将一个原型对象穿个那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建,即对象.clone()
(类似于孙悟空拔出猴毛变出其他孙大圣)
1.原型模式UML类图
2.改进后具体代码
Sheep
public class Sheep implements Cloneable{
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;
}
//使用默认的克隆方法来完成
@Override
protected Object clone(){
Sheep sheep = null;
try{
sheep = (Sheep)super.clone();
}catch (Exception e){
e.printStackTrace();
}
return sheep;
}
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
public class Client {
public static void main(String[] args){
Sheep sheep = new Sheep("tom",1,"白色");
Sheep sheep1 = (Sheep)sheep.clone();
}
}
3.浅拷贝介绍
浅拷贝就是使用默认的clone方法来实现,前面的克隆羊的例子就是浅拷贝
对于数据类型是基本数据类型的成员变量
比如int以及String(虽然不是基本数据类型)这些,浅拷贝会直接进行值传递,也就是将该属性值复制一份给新的对象
对于数据类型是引用数据类型的成员变量
比如成员变量是某个数组、某个类的对象等,那么浅拷贝会进行引用传递,也就是只是将该成员变量的引用值(内存地址)复制一份给新的对象。所以实际上两个对象的该成员变量都指向同一个实例。在这种情况下,在一个对象中修改该成员变量会影响到另一个对象的该成员变量值。
比如上面的Sheep类里面增加一条这样的属性
Sheep friend;
那么某个Sheep对象,经过clone方法得到的新对象,新旧对象的friend的内存地址都是同一个,也就是hashCode值都是一样的。
这种就是浅拷贝。
三.原型模式(深拷贝)
1.深拷贝介绍
复制对象的所有基本数据类型的成员变量值
为所有引用数据类型的成员变量申请空间,并复制每个引用数据类型成员变量所引用的对象。
也就是说,对象进行深拷贝要对整个对象进行拷贝
2.深拷贝实现方式
(1)重写clone方法来实现深拷贝
(2)通过对象序列化实现深拷贝
方式二推荐使用。
3.深拷贝实现方式一具体代码
DeepCloneableTarget
public class DeepCloneableTarget implements Serializable,Cloneable {
private static final long serialVersionUID = 1L;
private String cloneName;
private String cloneClass;
//构造器
public DeepCloneableTarget(String cloneName, String cloneClass) {
this.cloneName = cloneName;
this.cloneClass = cloneClass;
}
//因为该类的属性都是String,所以我们用默认的clone完成即可
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
DeepProtoType
public class DeepProtoType implements Serializable,Cloneable {
public String name;
public DeepCloneableTarget deepCloneableTarget;
DeepProtoType(){
super();
}
//深拷贝方式一:使用clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
Object deep = null;
deep = super.clone();//这里完成对基本数据类型和String类型的克隆
//关于引用类型的克隆,需要进行单独处理
DeepProtoType deepProtoType = (DeepProtoType)deep;
deepProtoType.deepCloneableTarget = (DeepCloneableTarget)deepCloneableTarget.clone();
return deepProtoType;
}
}
Client
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
DeepProtoType deepProtoType = new DeepProtoType();
deepProtoType.name = "帅哥";
deepProtoType.deepCloneableTarget = new DeepCloneableTarget("大牛","大牛类");
//方式1
DeepProtoType clone = (DeepProtoType)deepProtoType.clone();
System.out.println(deepProtoType.deepCloneableTarget.hashCode());
System.out.println(clone.deepCloneableTarget.hashCode());
/*
668386784
1329552164
*/
}
}
4.深拷贝实现方式二具体代码
在DeepProtoType类中
//深拷贝方式二:使用序列化
public Object deepClone(){
//创建流对象
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);
return ois.readObject();
}catch (Exception e){
e.printStackTrace();
return null;
}finally {
if(bos != null && oos != null && bis != null && ois!= null){
try {
oos.close();
ois.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Client
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
DeepProtoType deepProtoType = new DeepProtoType();
deepProtoType.name = "帅哥";
deepProtoType.deepCloneableTarget = new DeepCloneableTarget("大牛","大牛类");
//方式2
DeepProtoType copy = (DeepProtoType)deepProtoType.deepClone();
System.out.println(deepProtoType.deepCloneableTarget.hashCode());
System.out.println(copy.deepCloneableTarget.hashCode());
/*
1598924227
1067938912
*/
}
}
四.原型模式总结
创建新的对象比较复杂时,可以利用原型模式简化对象的创建过程,同时也能够提高效率
优点
①不用初始化对象,而是动态地获得对象运行时的状态
②如果原始对象发生变化(增加或者减少属性),其他克隆对象也会自动发生相应的变化,无需修改代码
缺点
需要为一个类配备一个克隆方法,这对全新的类来说不是很难,但对已有的类进行改造时,需要修改其源代码,违背了ocp原则。