原型模式——浅复制与深复制

原型模式涉及一个浅复制和深复制的概念。原型模式可以简单理解为“复制”,但这个复制不是代码的复制。对同一个类,我们可以实例化new三次来“复制”,但如果在初始化的时候构造函数的执行很长,多次实例化就显得效率很低效了。那我们能否只实例化一次,然后“复制”呢?

Test test1 = new Test();
Test test2 = test1;
Test test3 = test1;

这样写吗?注意这是引用的复制,这实际上还是只有test1一个实例,test2、test3只是复制了其引用而已,如果修改了一个对象则会影响到其他的对象。这样是不符合我们要求的。这就会引出我们Java的clone方法浅复制和深复制了。

我们先来看浅复制是什么。对于要实现克隆(我们后面将浅复制和深复制统称为克隆),必须实现Cloneable接口,尽管clone方法在Object类里,但我们还是得实现Cloneable接口不然会抛出CloneNotSupportedException错误。

定义一个Resume类实现Cloneable接口,并实现Object类中的clone方法。

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Resume implements Cloneable {
 9     private String name;
10     private String sex;
11     
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public String getSex() {
21         return sex;
22     }
23 
24     public void setSex(String sex) {
25         this.sex = sex;
26     }
27 
28     @Override
29     protected Object clone() throws CloneNotSupportedException {
30         Resume resume = (Resume)super.clone();
31         return resume;
32     }
33 
34 }

客户端测试代码:

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Main {
 9 
10     /**
11      * @param args
12      * @throws CloneNotSupportedException 
13      */
14     public static void main(String[] args) throws CloneNotSupportedException {
15         Resume resume1 = new Resume();
16         Resume resume2 = (Resume)resume1.clone();
17         System.out.println(resume1.hashCode() + " " + resume2.hashCode());
18     }
19 
20 }

我们可以看看输出结果,resume2是否只是复制了resume1的引用。

看来并不是。并且我们也成功地克隆了一个对象,并不是简单地复制了引用。我们的Resume类里只有两个String基本类型,我们来看看如果有其他引用会是什么样子的。

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Resume implements Cloneable {
 9     private String name;
10     private String sex;
11     private Test test = new Test(); 
12     
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public String getSex() {
22         return sex;
23     }
24 
25     public void setSex(String sex) {
26         this.sex = sex;
27     }
28 
29     public Test getTest() {
30         return test;
31     }
32 
33     public void setTest(Test test) {
34         this.test = test;
35     }
36 
37     @Override
38     protected Object clone() throws CloneNotSupportedException {
39         Resume resume = (Resume)super.clone();
40         return resume;
41     }
42 
43 }

此时我们增加了一个Test类的引用,其他代码不变,Test类里什么都没有。看看客户端代码:

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Main {
 9 
10     /**
11      * @param args
12      * @throws CloneNotSupportedException 
13      */
14     public static void main(String[] args) throws CloneNotSupportedException {
15         Resume resume1 = new Resume();
16         Resume resume2 = (Resume)resume1.clone();
17         System.out.println(resume1.hashCode() + " " + resume2.hashCode());
18         System.out.println(resume1.getTest().hashCode() + " " + resume2.getTest().hashCode());
19     }
20 
21 }

这个时候对第18行的输出结果会是什么样的呢?

我们看到虽然我们对resume1进行了克隆,resume2确实也是新的引用,但由于Resume类中有了对另外一个类的引用,所以resume1和resume2对Test对象的引用还是同一个,这就是浅复制。那么如何做到连同Test对象一起克隆,而不是只复制一个引用呢?这就是深复制的概念。

我们首先对在Resume中被引用的类Test做一点改变,让它实现Cloneable接口,实现clone方法:

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Test implements Cloneable{
 9     private String test;
10 
11     public String getTest() {
12         return test;
13     }
14 
15     public void setTest(String test) {
16         this.test = test;
17     }
18 
19     @Override
20     protected Object clone() throws CloneNotSupportedException {
21         return super.clone();
22     }
23     
24 }

同样在Resume类中做一点微小的改变:

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Resume implements Cloneable {
 9     private String name;
10     private String sex;
11     private Test test = new Test(); 
12     
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public String getSex() {
22         return sex;
23     }
24 
25     public void setSex(String sex) {
26         this.sex = sex;
27     }
28 
29     public Test getTest() {
30         return test;
31     }
32 
33     public void setTest(Test test) {
34         this.test = test;
35     }
36 
37     @Override
38     protected Object clone() throws CloneNotSupportedException {
39         Resume resume = (Resume)super.clone();
40         resume.test = (Test)test.clone();
41         return resume;
42     }
43 
44 }

客户端测试代码不变:

 1 package day_12_prototype;
 2 
 3 /**
 4  * @author turbo
 5  *
 6  * 2016年9月17日
 7  */
 8 public class Main {
 9 
10     /**
11      * @param args
12      * @throws CloneNotSupportedException 
13      */
14     public static void main(String[] args) throws CloneNotSupportedException {
15         Resume resume1 = new Resume();
16         Resume resume2 = (Resume)resume1.clone();
17         System.out.println(resume1.hashCode() + " " + resume2.hashCode());
18         System.out.println(resume1.getTest().hashCode() + " " + resume2.getTest().hashCode());
19     }
20 
21 }

输出结果:

这样我们就实现了对象的深复制。

说完浅复制与深复制,其实我们也就讲完了原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。为什么要通过克隆的方式来创建新的对象,也即是我们在上面提到的,每new一次都需要执行一次构造函数,如果构造函数的执行时间很长,那么多次执行这个初始化操作就实在是太低效了。

 

posted @ 2016-09-17 22:06  OKevin  阅读(5110)  评论(0编辑  收藏  举报