深入理解Android中的建造者模式
前言
在Android開(kāi)發(fā)過(guò)程中,我發(fā)現(xiàn)很多安卓源代碼里應(yīng)用了設(shè)計(jì)模式,比較常用的有適配器模式(各種adapter),建造者模式(Alert Dialog的構(gòu)建)等等。雖然我們對(duì)大多數(shù)設(shè)計(jì)模式都有所了解,但是在應(yīng)用設(shè)計(jì)模式的這個(gè)方面,感覺(jué)很多人在這方面有所不足。所以這篇文章我們一起深入的理解Android中的建造者模式。
建造者模式(Builder Pattern)也叫生成器模式,其定義如下:
separate the construction of a complex object from its representation so that the same construction process can create different representations.將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的標(biāo)示分離,這樣的話就可以使同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。
我的理解:就是把一個(gè)產(chǎn)品(對(duì)象)表示(展示)和構(gòu)建(創(chuàng)建)過(guò)程分離開(kāi)來(lái),這樣產(chǎn)品的構(gòu)建流程相同卻可以有不同的產(chǎn)品表示。
應(yīng)用場(chǎng)景
這里舉出Android中常見(jiàn)的例子:
android中的AlertDialog對(duì)話框的構(gòu)建過(guò)程就是建造者模式的典型應(yīng)用。

看一下Builder源碼
public static class Builder {
private final AlertController.AlertParams P;
public Builder(Context context) {
this(context, resolveDialogTheme(context, 0));
}
public Builder(Context context, int themeResId) {
P = new AlertController.AlertParams(new ContextThemeWrapper(
context, resolveDialogTheme(context, themeResId)));
}
//各種set參數(shù)方法
setTitle()
...
...
...
/**
* Creates an {@link AlertDialog} with the arguments supplied to this
* builder.
* <p>
* Calling this method does not display the dialog. If no additional
* processing is needed, {@link #show()} may be called instead to both
* create and display the dialog.
*/
public AlertDialog create() {
// Context has already been wrapped with the appropriate theme.
final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
dialog.setOnDismissListener(P.mOnDismissListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
// 這個(gè)show方法是builder對(duì)象的,里面封裝了create()和show()可以直接調(diào)取創(chuàng)建并顯示對(duì)話框
public AlertDialog show() {
final AlertDialog dialog = create();
dialog.show();
return dialog;
}
}
分析源碼可以看到內(nèi)部類(lèi)Builder是用來(lái)構(gòu)建這個(gè)對(duì)話框的:
1、創(chuàng)建builder的時(shí)候,會(huì)把這個(gè)AlertController.AlertParams P;對(duì)象P創(chuàng)建new出來(lái)
2、在對(duì)bilder設(shè)置各種參數(shù)的時(shí),這些參數(shù)都存在了對(duì)象P中
3、然后builder參數(shù)設(shè)置完畢后在調(diào)用create方法。
final AlertDialog dialog = new AlertDialog(P.mContext, 0, false); P.apply(dialog.mAlert); // mAlert是外部類(lèi)中的
這個(gè)方法中會(huì)首先調(diào)用外部類(lèi)AlertDialog的構(gòu)造方法,new出一個(gè)外部類(lèi)對(duì)象,然后p.apply()方法會(huì)將P這個(gè)對(duì)象作為內(nèi)部類(lèi)的屬性賦值給AlertController的對(duì)象mAlert。這樣就完成了一次的構(gòu)建。
下面是AlertDialog的部分源碼
public class AlertDialog extends Dialog implements DialogInterface {
// 這個(gè)對(duì)象用來(lái)承接builder內(nèi)部所設(shè)置的參數(shù)
private AlertController mAlert;
//以下幾個(gè)構(gòu)造方法決定只能通過(guò)內(nèi)部類(lèi)builder來(lái)構(gòu)建
protected AlertDialog(Context context) {
this(context, 0);
}
protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
this(context, 0);
setCancelable(cancelable);
setOnCancelListener(cancelListener);
}
protected AlertDialog(Context context, @StyleRes int themeResId) {
this(context, themeResId, true);
}
AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
createContextThemeWrapper);
mWindow.alwaysReadCloseOnTouchAttr();
mAlert = new AlertController(getContext(), this, getWindow());
}
}
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)各位Android開(kāi)發(fā)者們能有所幫助,如果有疑問(wèn)大家可以留言交流。
- java設(shè)計(jì)模式之建造者模式學(xué)習(xí)
- php設(shè)計(jì)模式 Builder(建造者模式)
- C++設(shè)計(jì)模式之建造者模式
- JavaScript設(shè)計(jì)模式之建造者模式介紹
- Java運(yùn)用設(shè)計(jì)模式中的建造者模式構(gòu)建項(xiàng)目的實(shí)例解析
- JS建造者模式基本用法實(shí)例分析
- 深入理解JavaScript系列(27):設(shè)計(jì)模式之建造者模式詳解
- Java設(shè)計(jì)模式之建造者模式(Builder模式)介紹
- 深入解析Python設(shè)計(jì)模式編程中建造者模式的使用
- iOS App設(shè)計(jì)模式開(kāi)發(fā)中對(duì)建造者模式的運(yùn)用實(shí)例
相關(guān)文章
Kotlin與Java相互調(diào)用的完整實(shí)例
Kotlin的設(shè)計(jì)過(guò)程中就考慮到了與Java的互操作性,在Kotlin中可以直接調(diào)用既有的Java代碼,反過(guò)來(lái)在Java中也可以很流暢地使用Kotlin代碼,這篇文章主要給大家介紹了關(guān)于Kotlin與Java相互調(diào)用的相關(guān)資料,需要的朋友可以參考下2021-12-12
Android 圖片處理避免出現(xiàn)oom的方法詳解
本篇文章主要介紹了Android 圖片處理避免出現(xiàn)oom的方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android 模擬器(emulator-5554...)出現(xiàn)錯(cuò)誤解決辦法
這篇文章主要介紹了Android 模擬器出現(xiàn)錯(cuò)誤解決辦法的相關(guān)資料,如:Unable to get view server version from device,F(xiàn)ailed to install helloworld.apk on device 'emulator-5554': timeout,這種常見(jiàn)錯(cuò)誤,解決辦法,需要的朋友可以參考下2016-11-11
通過(guò)案例分析Android WindowManager解析與騙取QQ密碼的過(guò)程
Windows Manager是一款窗口管理終端,可以遠(yuǎn)程連接到Linux的X桌面進(jìn)行管理,與服務(wù)器端產(chǎn)生一個(gè)session相互通信,通過(guò)本文給大家分享Android WindowManager解析與騙取QQ密碼的過(guò)程,需要的朋友參考下2016-01-01
Android 顯示和隱藏輸入法實(shí)現(xiàn)代碼
本文所要介紹的這個(gè)方法可以轉(zhuǎn)換軟件輸入法在窗體中的顯示狀態(tài),具體實(shí)現(xiàn)代碼如下,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03
Android添加指紋解鎖功能的實(shí)現(xiàn)代碼
當(dāng)開(kāi)發(fā)的APP需要加密驗(yàn)證時(shí)可以考慮添加系統(tǒng)指紋解鎖功能。這篇文章主要介紹了Android添加指紋解鎖功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-07-07
Android開(kāi)發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶(hù)端數(shù)據(jù)的交互功能示例
這篇文章主要介紹了Android開(kāi)發(fā)使用json實(shí)現(xiàn)服務(wù)器與客戶(hù)端數(shù)據(jù)的交互功能,結(jié)合具體實(shí)例形式分析了Android使用json格式數(shù)據(jù)在服務(wù)器與客戶(hù)端傳遞實(shí)現(xiàn)數(shù)據(jù)庫(kù)查詢(xún)功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android Map新用法:MapFragment應(yīng)用介紹
MapView ,MapActivity 這種的局限在于,必須要繼承MapActivity,否則無(wú)法使用MapView,但是,MapFragment 這種的局限在于,必須要安裝Google Play Service ,也就是說(shuō)必須是原生rom。而且sdk要在12以上2013-01-01

