Android 中的建造者模式

Android 中的建造者模式

概述


建造者模式(Builder Pattern)也叫生成器模式笛丙,其定義如下:
separate the construction of a complex object from its representation so that the same construction process can create different representations.將一個復雜對象的構建與它的標示分離修陡,這樣的話就可以使同樣的構建過程可以創(chuàng)建不同的表示枕赵。

我的理解:就是把一個產品(對象)表示(展示)和構建(創(chuàng)建)過程分離開來传透,這樣產品的構建流程相同卻可以有不同的產品表示机杜。

應用場景


這里舉出Android中常見的例子:

android中的AlertDialog對話框的構建過程就是建造者模式的典型應用饼疙。

UML

看一下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參數方法
    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;
    }

    
    // 這個show方法是builder對象的迅细,里面封裝了create()和show()可以直接調取創(chuàng)建并顯示對話框
    public AlertDialog show() {
        final AlertDialog dialog = create();
        dialog.show();
        return dialog;
    }
}

分析源碼可以看到內部類Builder是用來構建這個對話框的:

1巫橄、創(chuàng)建builder的時候,會把這個AlertController.AlertParams P;對象P創(chuàng)建new出來

2茵典、在對bilder設置各種參數的時湘换,這些參數都存在了對象P中

3、然后builder參數設置完畢后在調用create方法敬尺。

final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
P.apply(dialog.mAlert); // mAlert是外部類中的

這個方法中會首先調用外部類AlertDialog的構造方法枚尼,new出一個外部類對象,然后p.apply()方法會將P這個對象作為內部類的屬性賦值給AlertController的對象mAlert。這樣就完成了一次的構建砂吞。

下面是AlertDialog的部分源碼

public class AlertDialog extends Dialog implements DialogInterface {
    // 這個對象用來承接builder內部所設置的參數
    private AlertController mAlert;

    //以下幾個構造方法決定只能通過內部類builder來構建
    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());
        }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末署恍,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子蜻直,更是在濱河造成了極大的恐慌盯质,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件概而,死亡現場離奇詭異呼巷,居然都是意外死亡,警方通過查閱死者的電腦和手機赎瑰,發(fā)現死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門王悍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人餐曼,你說我怎么就攤上這事压储。” “怎么了源譬?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵集惋,是天一觀的道長。 經常有香客問我踩娘,道長刮刑,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮雷绢,結果婚禮上泛烙,老公的妹妹穿的比我還像新娘。我一直安慰自己习寸,他們只是感情好胶惰,可當我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著霞溪,像睡著了一般孵滞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鸯匹,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天坊饶,我揣著相機與錄音,去河邊找鬼殴蓬。 笑死匿级,一個胖子當著我的面吹牛,可吹牛的內容都是我干的染厅。 我是一名探鬼主播痘绎,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼肖粮!你這毒婦竟也來了孤页?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤涩馆,失蹤者是張志新(化名)和其女友劉穎行施,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體魂那,經...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡蛾号,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了涯雅。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鲜结。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖活逆,靈堂內的尸體忽然破棺而出精刷,到底是詐尸還是另有隱情,我是刑警寧澤划乖,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站挤土,受9級特大地震影響琴庵,放射性物質發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一迷殿、第九天 我趴在偏房一處隱蔽的房頂上張望儿礼。 院中可真熱鬧,春花似錦庆寺、人聲如沸蚊夫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽知纷。三九已至,卻和暖如春陵霉,著一層夾襖步出監(jiān)牢的瞬間琅轧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工踊挠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留乍桂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓效床,卻偏偏與公主長得像睹酌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子剩檀,可洞房花燭夜當晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內容