欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Builder模式构建一个通用Dialog

程序员文章站 2024-03-14 12:15:46
...

Builder模式的定义
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示;

适用场景
1、相同的方法,不同的执行顺序,产生不同的事件结果
2、多个部件或零件,都可以装配到同一个对象中,但是产生的运行结果又不相同
3、产品类非常复杂,或者产品中的调用顺序不同产生了不同的作用

经典的Builder模式
Builder模式构建一个通用Dialog
Product产品类:产品的抽象类
Builder:抽象类,规范产品的组建,一般由子类实现具体的组建过程
ConcreteBuilder:具体的构建器
Director:统一组装过程

上面概念还是比较抽象,不太容易理解,参考下我们日常开发中用的较多的AlertDialog,使用时不是通过直接的new出来的对象,而是通过AlertDialog.Builder builder = new AlertDialog.Builder(context)先创建一个AlertDialog.Builder对象,然后通过这个Builder对象才能创建AlertDialog的一个实例,简单从AlertDialog的源码来看一下:

protected AlertDialog(@NonNull Context context) {
    this(context, 0);
}

protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
    super(context, resolveDialogTheme(context, themeResId));
    mAlert = new AlertController(getContext(), this, getWindow());
}

protected AlertDialog(@NonNull Context context, boolean cancelable,
        @Nullable OnCancelListener cancelListener) {
    this(context, 0);
    setCancelable(cancelable);
    setOnCancelListener(cancelListener);
}

可以看到有三个Proctected修饰的构造方法,意味着除了AlertDialog的子类外,其他类是无法访问这个方法的;
在看下面有一个静态内部类Builder:

public static class Builder {
    private final AlertController.AlertParams P;
    private final int mTheme;
public Builder(@NonNull Context context) {
    this(context, resolveDialogTheme(context, 0));
}
public Builder(@NonNull Context context, @StyleRes int themeResId) {
    P = new AlertController.AlertParams(new ContextThemeWrapper(
            context, resolveDialogTheme(context, themeResId)));
    mTheme = 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 AlertDialog create() {
    // We can't use Dialog's 3-arg constructor with the createThemeContextWrapper param,
    // so we always have to re-set the theme
    final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
    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;
    }
}

可以看到final类型的属性P,以及一系列的set方法,用于设置AlertDialog的属性,这些方法都吧参数直接传递给了P这个实例,而且每一个方法都返回Builder类自身,这样就方便链式调用每一个方法;
最后通过create方法完成了AlertDialog的创建。
这里可以看出实际上是一个简化了的Builder设计模式,Builder同时扮演了builder,ConcreteBuilder,Director的角色。
在实际开发中,系统提供的Dialog往往不能满足我们的需求,更多的时候还是需要我们自定义Dialog,参照上面的实现,自己来实现一个通用的Dialog:
BaseDailog.java

public class BaseDialog extends Dialog {

    private Context mContext;

    private boolean canTouchOutside = false;

    private LinearLayout lyTitle;
    private TextView tvTitle;
    private TextView tvContent;
    private FrameLayout flContent;
    private LinearLayout lyBottom;
    private TextView tvCancel;
    private TextView tvConfirm;
    private TextView tvOk;

    private String mTitleTxt;
    private String mContentTxt;
    private String mCancelTxt;
    private String mConfirmTxt;
    private String mOkTxt;
    private View mView;

    private View.OnClickListener delaultClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    };

    private View.OnClickListener cancelListener = delaultClickListener;
    private View.OnClickListener comfirmListener = delaultClickListener;
    private View.OnClickListener okListener = delaultClickListener;



    protected BaseDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    protected BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        setContentView(R.layout.dailog_default);

        WindowManager wm = ((Activity)mContext).getWindowManager();
        Display display = wm.getDefaultDisplay();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = display.getWidth() * 4/5;
        window.setAttributes(lp);

        lyTitle = findViewById(R.id.ly_title);
        tvTitle = findViewById(R.id.tv_title);
        tvContent = findViewById(R.id.tv_content);
        flContent = findViewById(R.id.fl_content);
        lyBottom = findViewById(R.id.ly_bottom);
        tvCancel = findViewById(R.id.tv_cancel);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvOk = findViewById(R.id.tv_ok);
    }

    @Override
    public void show() {
        super.show();
        show(this);
    }

    protected void show(BaseDialog dialog){

        tvOk.setVisibility(View.GONE);
        dialog.setCanceledOnTouchOutside(dialog.canTouchOutside);

        if (null != dialog.mView){
            lyTitle.setVisibility(View.GONE);
            tvContent.setVisibility(View.GONE);
            lyBottom.setVisibility(View.GONE);
            flContent.setVisibility(View.VISIBLE);
            flContent.addView(mView);
        } else {
            lyTitle.setVisibility(View.VISIBLE);
            tvContent.setVisibility(View.VISIBLE);
            lyBottom.setVisibility(View.VISIBLE);
            flContent.setVisibility(View.GONE);
            tvContent.setText(dialog.mContentTxt);

            if (!TextUtils.isEmpty(dialog.mTitleTxt)){
                lyTitle.setVisibility(View.VISIBLE);
                tvTitle.setText(dialog.mTitleTxt);
            } else {
                lyTitle.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(dialog.mContentTxt)){
                tvContent.setText(dialog.mContentTxt);
            }

            if (!TextUtils.isEmpty(dialog.mCancelTxt)){
                tvCancel.setText(dialog.mCancelTxt);
            }

            if (!TextUtils.isEmpty(dialog.mConfirmTxt)){
                tvConfirm.setText(dialog.mConfirmTxt);
            }

            if (!TextUtils.isEmpty(dialog.mOkTxt)){
                tvOk.setVisibility(View.VISIBLE);
                lyBottom.setVisibility(View.GONE);
                tvOk.setText(dialog.mOkTxt);
            }

            if (null != dialog.comfirmListener){
                tvConfirm.setOnClickListener(dialog.comfirmListener);
            }

            if (null != dialog.cancelListener){
                tvCancel.setOnClickListener(dialog.cancelListener);
            }

            if (null != dialog.okListener){
                tvOk.setOnClickListener(dialog.okListener);
            }

        }
    }

    public static class Builder{
        private Context context;
        private BaseDialog baseDialog;

        public Builder(Context context) {
            this.context = context;
            baseDialog = new BaseDialog(context,R.style.baseDialog);
        }

        public Builder setTitle(String title){
            baseDialog.mTitleTxt = title;
            return this;
        }

        public Builder setContent(String content){
            baseDialog.mContentTxt = content;
            return this;
        }

        public Builder setCancelTxt(String cancelTxt){
            baseDialog.mCancelTxt = cancelTxt;
            return this;
        }

        public Builder setConfirmTxt(String confirmTxt){
            baseDialog.mConfirmTxt = confirmTxt;
            return this;
        }

        public Builder setOkTxt(String okTxt){
            baseDialog.mOkTxt = okTxt;
            return this;
        }

        public Builder setView(View view){
            baseDialog.mView = view;
            return this;
        }

        public Builder setCanOutsideTouch(boolean canOutsideTouch){
            baseDialog.canTouchOutside = canOutsideTouch;
            return this;
        }

        public Builder setCancelClickListener(View.OnClickListener listener){
            baseDialog.cancelListener = listener;
            return this;
        }

        public Builder setConfirmClickListener(View.OnClickListener listener){
            baseDialog.comfirmListener = listener;
            return this;
        }

        public Builder setOkClickListener(View.OnClickListener listener){
            baseDialog.okListener = listener;
            return this;
        }

        public BaseDialog create(){
            return baseDialog;
        }

    }
}

dailog_default.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg_conner_white"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ly_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:gravity="center"
            android:padding="8dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>

    </LinearLayout>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:gravity="center"
        android:text="我是内容ewwerwerewrewrewrewrewrewrewrewrewrewrew"
        android:padding="12dp" />

    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:visibility="gone">

    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/ly_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="8dp"
            android:background="@drawable/bg_bottom_of_left_white"
            android:text="取消"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />

        <TextView
            android:id="@+id/tv_confirm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/bg_bottom_of_right_white"
            android:padding="8dp"
            android:text="确定"/>

    </LinearLayout>

    <TextView
        android:id="@+id/tv_ok"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="8dp"
        android:text="确定"/>

</LinearLayout>

style

<style name="baseDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

bg_conner_white.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="8dp"/>
    <solid android:color="@color/bg_white"/>
</shape>

bg_bottom_of_left_white.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/bg_white"/>
    <corners android:bottomLeftRadius="8dp"/>
</shape>

bg_bottom_of_right_white.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/bg_white"/>
    <corners android:bottomRightRadius="8dp"/>
</shape>

另外源码已提交到github:https://github.com/zouqingliang/CommonDialog.git
如果有发现问题或更好的建议,欢迎随时留言