Android面試Intent采用了什么設(shè)計(jì)模式解析
正文
答案是采用了原型模式
原型模式的好處在于方便地拷貝某個(gè)實(shí)例的屬性進(jìn)行使用、又不會(huì)對(duì)原實(shí)例造成影響,其邏輯在于對(duì) Cloneable 接口的實(shí)現(xiàn)。
Intent 的關(guān)鍵源碼
// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {
...
private static final int COPY_MODE_ALL = 0;
private static final int COPY_MODE_FILTER = 1;
private static final int COPY_MODE_HISTORY = 2;
?
@Override
public Object clone() {
return new Intent(this);
}
?
public Intent(Intent o) {
this(o, COPY_MODE_ALL);
}
?
private Intent(Intent o, @CopyMode int copyMode) {
this.mAction = o.mAction;
this.mData = o.mData;
this.mType = o.mType;
this.mIdentifier = o.mIdentifier;
this.mPackage = o.mPackage;
this.mComponent = o.mComponent;
this.mOriginalIntent = o.mOriginalIntent;
...
?
if (copyMode != COPY_MODE_FILTER) {
...
if (copyMode != COPY_MODE_HISTORY) {
...
}
}
}
...
}
可以看到 Intent 實(shí)現(xiàn)的 clone() 邏輯是直接調(diào)用了 new 并傳入了自身實(shí)例,而非調(diào)用 super.clone() 進(jìn)行拷貝。
默認(rèn)的拷貝策略是 COPY_MODE_ALL,顧名思義,將完整拷貝源實(shí)例的所有屬性進(jìn)行構(gòu)造。其他的拷貝策略是 COPY_MODE_FILTER 指的是只拷貝跟 Intent-filter 相關(guān)的屬性,即用來(lái)判斷啟動(dòng)目標(biāo)組件的 action、data、type、component、category 等必備信息。
無(wú)視啟動(dòng) flag、bundle 等數(shù)據(jù)
// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {
...
public @NonNull Intent cloneFilter() {
return new Intent(this, COPY_MODE_FILTER);
}
?
private Intent(Intent o, @CopyMode int copyMode) {
this.mAction = o.mAction;
...
?
if (copyMode != COPY_MODE_FILTER) {
this.mFlags = o.mFlags;
this.mContentUserHint = o.mContentUserHint;
this.mLaunchToken = o.mLaunchToken;
...
}
}
}
中拷貝策略是 COPY_MODE_HISTORY
不需要 bundle 等歷史數(shù)據(jù),保留 action 等基本信息和啟動(dòng) flag 等數(shù)據(jù)。
// frameworks/base/core/java/android/content/Intent.java
public class Intent implements Parcelable, Cloneable {
...
public Intent maybeStripForHistory() {
if (!canStripForHistory()) {
return this;
}
return new Intent(this, COPY_MODE_HISTORY);
}
?
private Intent(Intent o, @CopyMode int copyMode) {
this.mAction = o.mAction;
...
?
if (copyMode != COPY_MODE_FILTER) {
...
if (copyMode != COPY_MODE_HISTORY) {
if (o.mExtras != null) {
this.mExtras = new Bundle(o.mExtras);
}
if (o.mClipData != null) {
this.mClipData = new ClipData(o.mClipData);
}
} else {
if (o.mExtras != null && !o.mExtras.isDefinitelyEmpty()) {
this.mExtras = Bundle.STRIPPED;
}
}
}
}
}
總結(jié)起來(lái):
| Copy Mode | action 等數(shù)據(jù) | flags 等數(shù)據(jù) | bundle 等歷史 |
|---|---|---|---|
| COPY_MODE_ALL | YES | YES | YES |
| COPY_MODE_FILTER | YES | NO | NO |
| COPY_MODE_HISTORY | YES | YES | NO |
Android 源碼中還有很多地方采用了原型模式
除了 Intent,Android 源碼中還有很多地方采用了原型模式。
Bundle 也實(shí)現(xiàn)了 clone(),提供了 new Bundle(this) 的處理:
public final class Bundle extends BaseBundle implements Cloneable, Parcelable {
...
@Override
public Object clone() {
return new Bundle(this);
}
}
組件信息類 ComponentName 也在 clone() 中提供了類似的實(shí)現(xiàn):
public final class ComponentName implements Parcelable, Cloneable, Comparable<ComponentName> {
...
public ComponentName clone() {
return new ComponentName(mPackage, mClass);
}
}
工具類 IntArray 亦是如此:
public class IntArray implements Cloneable {
...
@Override
public IntArray clone() {
return new IntArray(mValues.clone(), mSize);
}
}
原型模式也不一定非得實(shí)現(xiàn) Cloneable,提供了類似的實(shí)現(xiàn)即可。比如:
Bitmap 沒(méi)有實(shí)現(xiàn)該接口但提供了 copy(),內(nèi)部將傳遞原始 Bitmap 在 native 中的對(duì)象指針并伴隨目標(biāo)配置進(jìn)行新實(shí)例的創(chuàng)建:
public final class ComponentName implements Parcelable, Cloneable, Comparable<ComponentName> {
...
public Bitmap copy(Config config, boolean isMutable) {
...
noteHardwareBitmapSlowCall();
Bitmap b = nativeCopy(mNativePtr, config.nativeInt, isMutable);
if (b != null) {
b.setPremultiplied(mRequestPremultiplied);
b.mDensity = mDensity;
}
return b;
}
}以上就是Android面試Intent采用了什么設(shè)計(jì)模式解析的詳細(xì)內(nèi)容,更多關(guān)于Android面試Intent設(shè)計(jì)模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開(kāi)發(fā)之Kotlin委托的原理與使用詳解
我們常用的委托模式怎么使用?在?Java?語(yǔ)言中需要我們手動(dòng)的實(shí)現(xiàn),而在?Kotlin?語(yǔ)言中直接通過(guò)關(guān)鍵字?by?就可以實(shí)現(xiàn)委托,下面我們就一起看看不同種類的委托使用以及在?Android?常見(jiàn)的一些場(chǎng)景中的使用2023-03-03
Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕的具體代碼,感興趣的小伙伴們可以參考一下2016-05-05
Android實(shí)現(xiàn)IOS相機(jī)滑動(dòng)控件
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)IOS相機(jī)滑動(dòng)控件的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android開(kāi)發(fā)Retrofit源碼分析
這篇文章主要為大家介紹了Android開(kāi)發(fā)Retrofit源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Android車載多媒體開(kāi)發(fā)MediaSession框架示例詳解
這篇文章主要為大家介紹了Android車載多媒體開(kāi)發(fā)MediaSession框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09
一款不錯(cuò)的android6.0、7.0權(quán)限管理器推薦
下面小編就為大家分享一篇一款不錯(cuò)的android6.0、7.0權(quán)限管理器推薦,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

