自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android設(shè)計(jì)模式系列--原型模式

移動(dòng)開發(fā) Android
CV一族,應(yīng)該很容易理解原型模式的原理,復(fù)制,粘貼完后看具體情況是否修改,其實(shí)這就是原型模式。

CV一族,應(yīng)該很容易理解原型模式的原理,復(fù)制,粘貼完后看具體情況是否修改,其實(shí)這就是原型模式。
從java的角度看,一般使用原型模式有個(gè)明顯的特點(diǎn),就是實(shí)現(xiàn)cloneable的clone()方法。
原型模式,能快速克隆出一個(gè)與已經(jīng)存在對(duì)象類似的另外一個(gè)我們想要的新對(duì)象。

1.意圖
用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過拷貝這些原型創(chuàng)建新的對(duì)象。
熱門詞匯:克隆 深拷貝 淺拷貝

2.結(jié)構(gòu)圖和代碼
它的結(jié)構(gòu)圖非常簡(jiǎn)單,我們以Intent為例子:


Intent的clone方法非常簡(jiǎn)單:

  1. @Override 
  2. public Object clone() {  
  3.     return new Intent(this);  
  4. }  

返回一個(gè)新的Intent對(duì)象。
克隆操作分深拷貝和淺拷貝,淺拷貝說白了就是把原對(duì)象所有的值和引用直接賦給新對(duì)象。深拷貝則不僅把原對(duì)象的值賦給新對(duì)象,而且會(huì)把原對(duì)象的引用對(duì)象也重新創(chuàng)建一遍再賦給新對(duì)象。
我們具體分析一下Intent是淺拷貝還是深拷貝吧:

  1. public Intent(Intent o) {  
  2.     this.mAction = o.mAction;  
  3.     this.mData = o.mData;  
  4.     this.mType = o.mType;  
  5.     this.mPackage = o.mPackage;  
  6.     this.mComponent = o.mComponent;  
  7.     this.mFlags = o.mFlags;  
  8.     //下面幾個(gè)是引用對(duì)象被重新創(chuàng)建了,是深拷貝  
  9.     if (o.mCategories != null) {  
  10.         this.mCategories = new HashSet<String>(o.mCategories);  
  11.     }  
  12.     if (o.mExtras != null) {  
  13.         this.mExtras = new Bundle(o.mExtras);  
  14.     }  
  15.     if (o.mSourceBounds != null) {  
  16.         this.mSourceBounds = new Rect(o.mSourceBounds);  
  17.     }  
  18. }  

這里我們?yōu)槭裁碔ntent要重寫Object的clone方法,就與深拷貝有關(guān)。
其實(shí)我們查看Object的clone()方法源碼和注釋,默認(rèn)的super.clone()用的就是淺拷貝:

  1. /**  
  2.  * Creates and returns a copy of this {@code Object}. The default  
  3.  * implementation returns a so-called "shallow" copy: It creates a new  
  4.  * instance of the same class and then copies the field values (including  
  5.  * object references) from this instance to the new instance. A "deep" copy,  
  6.  * in contrast, would also recursively clone nested objects. A subclass that  
  7.  * needs to implement this kind of cloning should call {@code super.clone()}  
  8.  * to create the new instance and then create deep copies of the nested,  
  9.  * mutable objects.  
  10.  */ 
  11. protected Object clone() throws CloneNotSupportedException {  
  12.     if (!(this instanceof Cloneable)) {  
  13.         throw new CloneNotSupportedException("Class doesn't implement Cloneable");  
  14.     }  
  15.   
  16.     return internalClone((Cloneable) this);  
  17. }  

這種形式屬于簡(jiǎn)單形式的原型模式,如果需要?jiǎng)?chuàng)建的原型數(shù)目不固定,可以創(chuàng)建一個(gè)原型管理器,在復(fù)制原型對(duì)象之前,客戶端先在原型管理器中查看
是否存在滿足條件的原型對(duì)象,如果有,則直接使用,如果沒有,克隆一個(gè),這種稱作登記形式的原型模式。
適用原型模式可以對(duì)客戶隱藏產(chǎn)品的具體類,因此減少了客戶知道的名字的數(shù)目,此外是客戶無需改變
原型模式的缺陷是每個(gè)原型的子類都必須實(shí)現(xiàn)Cloneable接口,這個(gè)實(shí)現(xiàn)起來有時(shí)候比較困難。

3.效果
(1).創(chuàng)建型模式
(2).運(yùn)行時(shí)刻增加和刪除產(chǎn)品
(3).改變只以指定新對(duì)象(ctrl+v,然后修改)
(4).改變結(jié)構(gòu)以指定新對(duì)象。(類似2,實(shí)現(xiàn)不同而已)
(5).減少子類的構(gòu)造

責(zé)任編輯:張葉青 來源: 博客園
相關(guān)推薦

2021-10-28 19:09:09

模式原型Java

2020-10-21 14:29:15

原型模式

2021-05-18 08:52:31

Prototype 原型模式設(shè)計(jì)模式

2022-09-21 08:47:05

項(xiàng)目多線程對(duì)象

2013-11-26 15:48:53

Android設(shè)計(jì)模式SDK

2023-08-08 20:13:36

設(shè)計(jì)模式原型模式

2013-11-26 17:15:13

Android設(shè)計(jì)模式

2013-11-26 16:20:26

Android設(shè)計(jì)模式

2021-06-07 09:51:22

原型模式序列化

2020-06-08 08:04:49

設(shè)計(jì)模式結(jié)構(gòu)型接口

2013-11-26 16:29:22

Android設(shè)計(jì)模式

2013-11-26 16:39:21

Android設(shè)計(jì)模式

2013-11-26 17:09:57

Android設(shè)計(jì)模式

2015-06-08 09:05:10

Java原型模式

2020-10-23 09:40:26

設(shè)計(jì)模式

2020-11-03 13:05:18

命令模式

2020-11-04 08:54:54

狀態(tài)模式

2022-01-12 13:33:25

工廠模式設(shè)計(jì)

2020-11-09 08:20:33

解釋器模式

2020-10-20 13:33:00

建造者模式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)