简单模拟Android中AlertDialog的Builder设计模式

     在Android中经常要使用AlertDialog来显示一些简单的窗口,通常的写法类似下面的代码 : 

        AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("提示")
                .setMessage("确定退出?").create();
        dialog.show();
可以看到dialog是通过Builder创建的,那么Builder是如何同AlertDialog协同工作的呢?其实AlertDialog的创建就是使用Builder模式,废话不多说,直接上代码吧。


package com.umeng.dp.builder;

/**
 * @Copyright: Umeng.com, Ltd. Copyright 2011-2015, All rights reserved
 * @Title: UmengDialog.java
 * @Package com.umeng.dp.builder
 * @Description: Builder模式
 * @author Honghui He
 * @version V1.0
 */

public class UmengDialog {

    private String mTitle = "";
    private String mMessage = "";
    private int mIcon = -1;

    /**
     * @Title: UmengDialog
     * @Description: UmengDialog Constructor
     * @param builder 将dialog参数传递给builder,够将好的builder传递给Dialog
     */
    private UmengDialog(Builder builder) {
        mTitle = builder.getTitle();
        mMessage = builder.getMessage();
        mIcon = builder.getIcon();
    }

    /**
     * @Title: show
     * @Description: show dialog
     * @throws
     */
    public void show() {
        System.out.println("显示窗口 : " + this);
    }

    /**
     * (非 Javadoc)
     * 
     * @Title: toString
     * @Description:
     * @return
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "UmengDialog [mTitle=" + mTitle + ", mMessage=" + mMessage + ", mIcon=" + mIcon
                + "]";
    }

    /**
     * @ClassName: Builder
     * @Description: Builder模式, 用于构建复杂的对象
     * @author Honghui He
     */
    public static class Builder {
        private String mDlgTitle = "";
        private String mDlgMessage = "";
        private int mDlgIcon = -1;

        /**
         * @Title: Builder
         * @Description: Builder Constructor
         */
        public Builder() {
        }

        /**
         * 获取 mDlgTitle
         * 
         * @return 返回 mDlgTitle
         */
        public String getTitle() {
            return mDlgTitle;
        }

        /**
         * 设置 mDlgTitle
         * 
         * @param 对mDlgTitle进行赋值
         */
        public Builder setTitle(String title) {
            this.mDlgTitle = title;
            return this;
        }

        /**
         * 获取 mDlgMessage
         * 
         * @return 返回 mDlgMessage
         */
        public String getMessage() {
            return mDlgMessage;
        }

        /**
         * 设置 mDlgMessage
         * 
         * @param 对mDlgMessage进行赋值
         */
        public Builder setMessage(String msg) {
            this.mDlgMessage = msg;
            return this;
        }

        /**
         * 获取 mDlgIcon
         * 
         * @return 返回 mDlgIcon
         */
        public int getIcon() {
            return mDlgIcon;
        }

        /**
         * 设置 mDlgIcon
         * 
         * @param 对mDlgIcon进行赋值
         */
        public Builder setIcon(int resId) {
            this.mDlgIcon = resId;
            return this;
        }

        /**
         * @Title: create
         * @Description: 创建窗口
         * @throws
         */
        public UmengDialog create() {
            return new UmengDialog(this);
        }
    }

}

输出结果 : 

显示窗口 : UmengDialog [mTitle=Alert Dialg, mMessage=Builder, mIcon=123]

你可能感兴趣的:(简单模拟Android中AlertDialog的Builder设计模式)