Android开发框架/库学习,设计模式之建造者模式

来源:互联网 发布:内部类java 编辑:程序博客网 时间:2024/05/22 04:35

Builder建造者模式

建造者模式是一个一步步创建出一个复杂对象的创建型模式。当一个对象含有复杂的业务功能,并且这些功能是可以组装的,我们可以将它一步步分离出来,逐步创建出这个对象。例如,安卓的AlerDialog类,提供了各种set的方法,最后通过show方法创建出一个自己定义的对话框。

常见的:

AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("标题")        .setMessage("提示")        .setPositiveButton("确定", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {            }        })        .setNegativeButton("取消", null)        .show();

那么怎么去写出可以这样调用的代码呢?

在Android开发中,Builder常常为链式调用,关键点在于每一个set方法都返回类本身,打开到AlerDialog类源码,可以看到,能够形成链式调用的代码为:

    /**     * Set the title displayed in the {@link Dialog}.     *     * @return This Builder object to allow for chaining of calls to set methods     */    public Builder setTitle(CharSequence title) {        P.mTitle = title;        return this;    }    /**     * Set the message to display.     *     * @return This Builder object to allow for chaining of calls to set methods     */    public Builder setMessage(CharSequence message) {        P.mMessage = message;        return this;    }    /**     * Set a listener to be invoked when the positive button of the dialog is pressed.     * @param textId The resource id of the text to display in the positive button     * @param listener The {@link DialogInterface.OnClickListener} to use.     *     * @return This Builder object to allow for chaining of calls to set methods     */    public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {        P.mPositiveButtonText = P.mContext.getText(textId);        P.mPositiveButtonListener = listener;        return this;    }    ....

它们都有一个共同的特点,即是每个方法并不是void,而是以Builder这个内部类作为返回类型,定义完方法里的代码最后,再return this,返回这个类。

通过这种方式,可以更为方便地对复杂对象进行组装,调用结构也变得更为简单。

Android Demo

笔者之前写过【Android轻松完成支付宝支付教程】里,就是对支付宝的支付过程进行一个Builder设计模式封装。链接:http://blog.csdn.net/u013003052/article/details/51838224

在AliPay这个类中,使用Builder内部类,对AliPay的参数进行设置。

...public Builder build(Activity activity) {    payData = new Builder(activity);    payData.setPARTNER(PARTNER);    payData.setSELLER(SELLER);    payData.setRSA_PRIVATE(RSA_PRIVATE);    payData.setRSA_PUBLIC(RSA_PUBLIC);    payData.setOrderTitle(orderTitle);    payData.setSubTitle(subTitle);    payData.setPrice(price);    payData.setNotifyURL(notifyURL);    return payData;}public AliPay setPARTNER(String PARTNER) {    this.PARTNER = PARTNER;    return this;}public AliPay setSELLER(String SELLER) {    this.SELLER = SELLER;    return this;}public AliPay setRSA_PRIVATE(String RSA_PRIVATE) {    this.RSA_PRIVATE = RSA_PRIVATE;    return this;}public AliPay setRSA_PUBLIC(String RSA_PUBLIC) {    this.RSA_PUBLIC = RSA_PUBLIC;    return this;}public AliPay setOrderTitle(String orderTitle) {    this.orderTitle = orderTitle;    return this;}public AliPay setSubTitle(String subTitle) {    this.subTitle = subTitle;    return this;}public AliPay setPrice(String price) {    this.price = price;    return this;}public AliPay setNotifyURL(String notifyURL) {    this.notifyURL = notifyURL;    return this;}public void pay() {    if (payData != null) {        payData.pay();    }}...

以Builder这个内部类作为返回类型,定义完方法里的代码最后,再return this,返回这个类。

0 0
原创粉丝点击