設(shè)計(jì)模式建造者模式

1.建造者模式的定義及使用場(chǎng)景
Builder模式是一步一步創(chuàng)建一個(gè)復(fù)制對(duì)象的創(chuàng)建型模式吧享,他允許用戶在不知道內(nèi)部構(gòu)建細(xì)節(jié)的情況下芬为,可以更精細(xì)地控制對(duì)象的構(gòu)造流程。改模式是為了將構(gòu)造復(fù)雜對(duì)象的過程和它的部件解耦沐鼠,使得構(gòu)建過程和部件的表示隔離開來。
1.1定義
將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過程可以創(chuàng)建同步的表示
1.2使用場(chǎng)景
相同的方法,不同的執(zhí)行順序习寸,產(chǎn)生不同的事件結(jié)果時(shí)多個(gè)部件或零件胶惰,都可以裝配到一個(gè)對(duì)象中傻工,但是產(chǎn)生的運(yùn)行結(jié)果又不相同時(shí)產(chǎn)品類非常復(fù)雜,或者產(chǎn)品類中的調(diào)用順序不同產(chǎn)生了不同的作用孵滞,這個(gè)時(shí)候使用建造者模式非常合適當(dāng)初始化一個(gè)對(duì)象特別復(fù)雜中捆,如參數(shù)多,而且很多參數(shù)都具有默認(rèn)值時(shí)

Paste_Image.png

2.建造者模式的優(yōu)缺點(diǎn)
2.1優(yōu)點(diǎn)
封裝性
建造者獨(dú)立坊饶,容易擴(kuò)展
便于控制細(xì)節(jié)風(fēng)險(xiǎn)
2.2缺點(diǎn)
會(huì)產(chǎn)生多余的Builder對(duì)象以及Director對(duì)象
3.建造者模式的實(shí)現(xiàn)方式
Product:

public class Product {  
    private  int id;  
    private  String  name;  
    public void doSomeThing(){  
        System.out.println("Product doSomeThing id:"+id+" name:"+name);  
    }  
  
  
    public void setId(int id) {  
        this.id = id;  
    }  
  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
  
    @Override  
    public String toString() {  
        return "Product{" +  
                "id=" + id +  
                ", name='" + name + '\'' +  
                '}';  
    }  
}```
AbstactBuilder:

public abstract class AbstactBulider {
abstract void setId(int id);
abstract void setName(String name);
abstract Product build();
}```
Builder:

public class Builder extends AbstactBulider {  
    Product product = new Product();  
  
  
    @Override  
    void setId(int id) {  
        product.setId(id);  
    }  
  
  
    @Override  
    void setName(String name) {  
        product.setName(name);  
    }  
  
  
    @Override  
    Product build() {  
        return product;  
    }  
}```
Director:

public class Director {
private AbstactBulider aBuilder, bBuilder;

public Product getAProduct() {  
    aBuilder = new Builder();  
    aBuilder.setId(1);  
    aBuilder.setName("allen");  
    return aBuilder.build();  
}  


public Product getBProduct() {  
    bBuilder = new Builder();  
    bBuilder.setId(2);  
    bBuilder.setName("joy");  
    return bBuilder.build();  
}  

}```
4.建造者模式在Android中的實(shí)際應(yīng)用
在Android源碼中泄伪,最常用到的Builder模式就是AlertDialog.Builder,使用該Builder來構(gòu)建復(fù)雜的AlertDialog對(duì)象匿级。在開發(fā)過程中蟋滴,我們經(jīng)常用到AlertDialog,具體示例如下:

new AlertDialog.Builder(self)
  .setTitle("列表框")
  .setItems(new String[] {"列表項(xiàng)1","列表項(xiàng)2","列表項(xiàng)3"}, null)
  .setNegativeButton("確定", null)
  .show();```
從類名就可以看出這是一個(gè)Builder模式痘绎,通過Builder對(duì)象來組裝Dialog的各個(gè)部分津函,如title、buttons孤页、items等尔苦。分析AlertDialog源碼如下:

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)));
}

public Context getContext() {
return P.mContext;
}

public Builder setTitle(@StringRes int titleId) {
P.mTitle = P.mContext.getText(titleId);
return this;
}

public Builder setTitle(CharSequence title) {
P.mTitle = title;
return this;
}

public Builder setCustomTitle(View customTitleView) {
P.mCustomTitleView = customTitleView;
return this;
}

public Builder setMessage(@StringRes int messageId) {
P.mMessage = P.mContext.getText(messageId);
return this;
}

public Builder setMessage(CharSequence message) {
P.mMessage = message;
return this;
}

public Builder setIcon(@DrawableRes int iconId) {
P.mIconId = iconId;
return this;
}

public Builder setIcon(Drawable icon) {
P.mIcon = icon;
return this;
}

public Builder setIconAttribute(@AttrRes int attrId) {
TypedValue out = new TypedValue();
P.mContext.getTheme().resolveAttribute(attrId, out, true);
P.mIconId = out.resourceId;
return this;
}

public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
P.mPositiveButtonText = P.mContext.getText(textId);
P.mPositiveButtonListener = listener;
return this;
}

public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
P.mPositiveButtonText = text;
P.mPositiveButtonListener = listener;
return this;
}

public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
P.mNegativeButtonText = P.mContext.getText(textId);
P.mNegativeButtonListener = listener;
return this;
}

public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
P.mNegativeButtonText = text;
P.mNegativeButtonListener = listener;
return this;
}

public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) {
P.mNeutralButtonText = P.mContext.getText(textId);
P.mNeutralButtonListener = listener;
return this;
}

public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
P.mNeutralButtonText = text;
P.mNeutralButtonListener = listener;
return this;
}
public Builder setCancelable(boolean cancelable) {
P.mCancelable = cancelable;
return this;
}

public Builder setOnCancelListener(OnCancelListener onCancelListener) {
P.mOnCancelListener = onCancelListener;
return this;
}
public Builder setOnDismissListener(OnDismissListener onDismissListener) {
P.mOnDismissListener = onDismissListener;
return this;
}

public Builder setOnKeyListener(OnKeyListener onKeyListener) {
P.mOnKeyListener = onKeyListener;
return this;
}

public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnClickListener = listener;
return this;
}

public Builder setItems(CharSequence[] items, final OnClickListener listener) {
P.mItems = items;
P.mOnClickListener = listener;
return this;
}

public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
P.mAdapter = adapter;
P.mOnClickListener = listener;
return this;
}

public Builder setCursor(final Cursor cursor, final OnClickListener listener,
String labelColumn) {
P.mCursor = cursor;
P.mLabelColumn = labelColumn;
P.mOnClickListener = listener;
return this;
}

public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,
final OnMultiChoiceClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnCheckboxClickListener = listener;
P.mCheckedItems = checkedItems;
P.mIsMultiChoice = true;
return this;
}

public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
final OnMultiChoiceClickListener listener) {
P.mItems = items;
P.mOnCheckboxClickListener = listener;
P.mCheckedItems = checkedItems;
P.mIsMultiChoice = true;
return this;
}

public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
final OnMultiChoiceClickListener listener) {
P.mCursor = cursor;
P.mOnCheckboxClickListener = listener;
P.mIsCheckedColumn = isCheckedColumn;
P.mLabelColumn = labelColumn;
P.mIsMultiChoice = true;
return this;
}

public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
final OnClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
return this;
}

public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
final OnClickListener listener) {
P.mCursor = cursor;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mLabelColumn = labelColumn;
P.mIsSingleChoice = true;
return this;
}

public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
P.mItems = items;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
return this;
}

public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
P.mAdapter = adapter;
P.mOnClickListener = listener;
P.mCheckedItem = checkedItem;
P.mIsSingleChoice = true;
return this;
}

public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
P.mOnItemSelectedListener = listener;
return this;
}

public Builder setView(int layoutResId) {
P.mView = null;
P.mViewLayoutResId = layoutResId;
P.mViewSpacingSpecified = false;
return this;
}

public Builder setView(View view) {
P.mView = view;
P.mViewLayoutResId = 0;
P.mViewSpacingSpecified = false;
return this;
}

@Deprecated
public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
int viewSpacingRight, int viewSpacingBottom) {
P.mView = view;
P.mViewLayoutResId = 0;
P.mViewSpacingSpecified = true;
P.mViewSpacingLeft = viewSpacingLeft;
P.mViewSpacingTop = viewSpacingTop;
P.mViewSpacingRight = viewSpacingRight;
P.mViewSpacingBottom = viewSpacingBottom;
return this;
}

@Deprecated
public Builder setInverseBackgroundForced(boolean useInverseBackground) {
P.mForceInverseBackground = useInverseBackground;
return this;
}

/**

  • @hide
    */
    public Builder setRecycleOnMeasureEnabled(boolean enabled) {
    P.mRecycleOnMeasure = enabled;
    return this;
    }

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;
}

public AlertDialog show() {
final AlertDialog dialog = create();
dialog.show();
return dialog;
}
}```
在AlertDialog的Builder模式中并沒有看到Director角色出現(xiàn),其實(shí)在很多場(chǎng)景中行施,Android并沒有完全按照經(jīng)典模式實(shí)現(xiàn)來做允坚,而是做了一些修改,使得這個(gè)模式更易于使用蛾号。

這里的AlertDialog.Builder同時(shí)扮演了上文中提到的builder稠项、ConcreteBuilder、Director的角色鲜结,簡(jiǎn)化了Builder模式的設(shè)計(jì)皿渗。當(dāng)模塊比較穩(wěn)定,不存在一些變化時(shí)轻腺,可以在經(jīng)典模式實(shí)現(xiàn)的基礎(chǔ)上做一些精簡(jiǎn)乐疆,而不是生搬硬套,使程序失去架構(gòu)之美贬养。正是由于靈活地運(yùn)用設(shè)計(jì)模式挤土,Android的源碼很值得我們深入學(xué)習(xí)。

出處:http://huangjunbin.com/page/3/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末误算,一起剝皮案震驚了整個(gè)濱河市仰美,隨后出現(xiàn)的幾起案子迷殿,更是在濱河造成了極大的恐慌,老刑警劉巖咖杂,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件庆寺,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡诉字,警方通過查閱死者的電腦和手機(jī)懦尝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來壤圃,“玉大人陵霉,你說我怎么就攤上這事∥樯” “怎么了踊挠?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長冲杀。 經(jīng)常有香客問我效床,道長,這世上最難降的妖魔是什么权谁? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任剩檀,我火速辦了婚禮,結(jié)果婚禮上闯传,老公的妹妹穿的比我還像新娘谨朝。我一直安慰自己,他們只是感情好甥绿,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布字币。 她就那樣靜靜地躺著,像睡著了一般共缕。 火紅的嫁衣襯著肌膚如雪洗出。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天图谷,我揣著相機(jī)與錄音翩活,去河邊找鬼。 笑死便贵,一個(gè)胖子當(dāng)著我的面吹牛菠镇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播承璃,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼利耍,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起隘梨,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤程癌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后轴猎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嵌莉,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年捻脖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锐峭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡郎仆,死狀恐怖只祠,靈堂內(nèi)的尸體忽然破棺而出兜蠕,到底是詐尸還是另有隱情扰肌,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布熊杨,位于F島的核電站曙旭,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏晶府。R本人自食惡果不足惜桂躏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望川陆。 院中可真熱鬧剂习,春花似錦、人聲如沸较沪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尸曼。三九已至们何,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間控轿,已是汗流浹背冤竹。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留茬射,地道東北人鹦蠕。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像在抛,于是被迫代替她去往敵國和親钟病。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容