概念

建造者模式是较为简单的创立型模式,将组件和组件的组件过程离开,而后一步一步建造一个简单的对象。所以建造者模式又叫生成器模式。它容许用户在不晓得外部构建细节的状况下,十分精密地管制对象构建流程。该模式是为了将构建过程非常复杂的对象进行拆分,让它与它的部件解耦,晋升代码的可读性以及扩展性。

应用场景:

结构一个对象须要很多参数的时候,并且参数的个数或者类型不固定的时候

UML结构图

例:

//创立简单对象Productpublic class Product {    private String partA;    private String partB;    private String partC;    public String getPartA() {        return partA;    }    public void setPartA(String partA) {        this.partA = partA;    }    public String getPartB() {        return partB;    }    public void setPartB(String partB) {        this.partB = partB;    }    public String getPartC() {        return partC;    }    public void setPartC(String partC) {        this.partC = partC;    }}//创立抽象类Builderpublic abstract class Builder {    protected Product product = new Product();    public abstract void builderPartA();    public abstract void builderPartB();    public abstract void builderPartC();    public Product getResult() {        return product;    }}//创立实现类ConcreateBuilderpublic class ConcreateBuilder extends Builder {    @Override    public void builderPartA() {    }    @Override    public void builderPartB() {    }    @Override    public void builderPartC() {    }}//创立组装对象Directorpublic class Director {    private Builder builder;    public Director(Builder builder){        this.builder = builder;    }    public void setBuilder(Builder builder) {        this.builder = builder;    }    public Product constract(){        builder.builderPartA();        builder.builderPartB();        builder.builderPartC();        return builder.getResult();    }}

//运行

Builder builder = new ConcreateBuilder();Director director = new Director(builder);Product product = director.constract();

Builder:他为创立一个创立Product对象的各个部件指定形象接口
ConcreateBuilder:它实现了builder接口,实现各个部件的具体结构和拆卸办法。
Product:他是被构建的简单对象,包好多个组成部件。
Director:指挥者又称为导演类,负责安顿简单对象的建造秩序,指挥者与形象建造者之间存在关联关系。

长处:

涣散耦合:生成其模式能够用同一个结构算法构建出体现上齐全不同的产品,实现产品构建和产品体现上的拆散。
很容易扭转产品外部示意。
更好的复用性:生成器模式很好的实现了构建算法和具体产品实现的拆散。

毛病:

会产生多余的Builder对象,Director对象,耗费内存。
对象构建过程裸露。

android SDK源码剖析

先看应用

//实例化一个AlertDialog对象:new AlertDialog.Builder(this)                .setTitle("题目")                .setMessage("内容")                .show();

AlertDialog.Builder 构造方法如下

public Builder(Context context, int themeResId) {            P = new AlertController.AlertParams(new ContextThemeWrapper(                    context, resolveDialogTheme(context, themeResId)));        }

AlertParams就是咱们理论传递的设置

//而后调用show办法public AlertDialog show() {            final AlertDialog dialog = create();            dialog.show();            return dialog;        }                //create        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;        }                //apply        public void apply(AlertController dialog) {            if (mCustomTitleView != null) {                dialog.setCustomTitle(mCustomTitleView);            } else {                if (mTitle != null) {                    dialog.setTitle(mTitle);                }                if (mIcon != null) {                    dialog.setIcon(mIcon);                }                if (mIconId != 0) {                    dialog.setIcon(mIconId);                }                if (mIconAttrId != 0) {                    dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));                }            }            if (mMessage != null) {                dialog.setMessage(mMessage);            }            if (mPositiveButtonText != null) {                dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,                        mPositiveButtonListener, null);            }            if (mNegativeButtonText != null) {                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,                        mNegativeButtonListener, null);            }            if (mNeutralButtonText != null) {                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,                        mNeutralButtonListener, null);            }            if (mForceInverseBackground) {                dialog.setInverseBackgroundForced(true);            }            // For a list, the client can either supply an array of items or an            // adapter or a cursor            if ((mItems != null) || (mCursor != null) || (mAdapter != null)) {                createListView(dialog);            }            if (mView != null) {                if (mViewSpacingSpecified) {                    dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,                            mViewSpacingBottom);                } else {                    dialog.setView(mView);                }            } else if (mViewLayoutResId != 0) {                dialog.setView(mViewLayoutResId);            }            /*            dialog.setCancelable(mCancelable);            dialog.setOnCancelListener(mOnCancelListener);            if (mOnKeyListener != null) {                dialog.setOnKeyListener(mOnKeyListener);            }            */        }                //dialog的show办法        public void show() {        if (mShowing) {            if (mDecor != null) {                if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {                    mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);                }                mDecor.setVisibility(View.VISIBLE);            }            return;        }        mCanceled = false;        if (!mCreated) {            dispatchOnCreate(null);        } else {            // Fill the DecorView in on any configuration changes that            // may have occured while it was removed from the WindowManager.            final Configuration config = mContext.getResources().getConfiguration();            mWindow.getDecorView().dispatchConfigurationChanged(config);        }        onStart();        mDecor = mWindow.getDecorView();        if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {            final ApplicationInfo info = mContext.getApplicationInfo();            mWindow.setDefaultIcon(info.icon);            mWindow.setDefaultLogo(info.logo);            mActionBar = new WindowDecorActionBar(this);        }        WindowManager.LayoutParams l = mWindow.getAttributes();        if ((l.softInputMode                & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {            WindowManager.LayoutParams nl = new WindowManager.LayoutParams();            nl.copyFrom(l);            nl.softInputMode |=                    WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;            l = nl;        }        mWindowManager.addView(mDecor, l);        mShowing = true;        sendShowMessage();    }

其实这里是调用父类Dialog的show办法,能够看到调用了dispatchOnCreate办法,点击进去:

void dispatchOnCreate(Bundle savedInstanceState) {        if (!mCreated) {            onCreate(savedInstanceState);            mCreated = true;        }    }

调用了onCreate办法,点击进去,Dialog类没有任何实现,这时候咱们想到它的子类AlertDialog,去这个类找onCreate的实现:

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mAlert.installContent();    }

调用了AlertController对象的installContent办法,点击进去:

public void installContent() {

    int contentView = selectContentView();    mWindow.setContentView(contentView);    setupView();}

能够看到这里调用了setupView办法,点击进去:

private void setupView() {        final View parentPanel = mWindow.findViewById(R.id.parentPanel);        final View defaultTopPanel = parentPanel.findViewById(R.id.topPanel);        final View defaultContentPanel = parentPanel.findViewById(R.id.contentPanel);        final View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);        // Install custom content before setting up the title or buttons so        // that we can handle panel overrides.        final ViewGroup customPanel = (ViewGroup) parentPanel.findViewById(R.id.customPanel);        setupCustomContent(customPanel);        final View customTopPanel = customPanel.findViewById(R.id.topPanel);        final View customContentPanel = customPanel.findViewById(R.id.contentPanel);        final View customButtonPanel = customPanel.findViewById(R.id.buttonPanel);        // Resolve the correct panels and remove the defaults, if needed.        final ViewGroup topPanel = resolvePanel(customTopPanel, defaultTopPanel);        final ViewGroup contentPanel = resolvePanel(customContentPanel, defaultContentPanel);        final ViewGroup buttonPanel = resolvePanel(customButtonPanel, defaultButtonPanel);        setupContent(contentPanel);        setupButtons(buttonPanel);        setupTitle(topPanel);}

原来真正的布局与设置都是在这里

而你仅仅只设置了 message 和title,其余的你一概不知