原型模式——淺復制與深復制


原型模式涉及一個淺復制和深復制的概念。原型模式可以簡單理解為“復制”,但這個復制不是代碼的復制。對同一個類,我們可以實例化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一次都需要執行一次構造函數,如果構造函數的執行時間很長,那么多次執行這個初始化操作就實在是太低效了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM