Android中的構建者模式

Android開發中的構建者模式:

 

項目實例: 

import android.os.Parcel;
import android.os.Parcelable;
import android.view.Gravity;
import android.view.WindowManager;

/**
 *  Video客服統一彈窗參數
 */
public class CallDialogParam implements Parcelable {

    private String image;
    private String content;
    private String leftButton;
    private String rightButton;
    private int gravity;;
    private int dialogWidth;
    private int dialogHeight;
    private int customRes;
    private boolean isCustom;
    private boolean cancelable;

    protected CallDialogParam(Parcel in) {
        image = in.readString();
        content = in.readString();
        leftButton = in.readString();
        rightButton = in.readString();
        gravity = in.readInt();
        dialogWidth = in.readInt();
        dialogHeight = in.readInt();
        customRes = in.readInt();
        isCustom = in.readByte() != 0;
        cancelable = in.readByte() != 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(image);
        dest.writeString(content);
        dest.writeString(leftButton);
        dest.writeString(rightButton);
        dest.writeInt(gravity);
        dest.writeInt(dialogWidth);
        dest.writeInt(dialogHeight);
        dest.writeInt(customRes);
        dest.writeByte((byte) (isCustom ? 1 : 0));
        dest.writeByte((byte) (cancelable ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<CallDialogParam> CREATOR = new Creator<CallDialogParam>() {
        @Override
        public CallDialogParam createFromParcel(Parcel in) {
            return new CallDialogParam(in);
        }

        @Override
        public CallDialogParam[] newArray(int size) {
            return new CallDialogParam[size];
        }
    };

    public CallDialogParam() {
        this(new Builder());
    }

    CallDialogParam(Builder builder) {
        this.image = builder.image;
        this.content = builder.content;
        this.leftButton = builder.leftButton;
        this.rightButton = builder.rightButton;
        this.gravity = builder.gravity;
        this.dialogWidth = builder.dialogWidth;
        this.dialogHeight = builder.dialogHeight;
        this.customRes = builder.customRes;
        this.isCustom = builder.isCustom;
        this.cancelable = builder.cancelable;
    }

    public String image() {
        return image;
    }

    public String content() {
        return content;
    }

    public String leftButton() {
        return leftButton;
    }

    public String rightButton() {
        return rightButton;
    }

    public int gravity() {
        return gravity;
    }

    public int width() {
        return dialogWidth;
    }

    public int height() {
        return dialogHeight;
    }

    public int customRes() {
        return customRes;
    }

    public boolean custom() {
        return isCustom;
    }

    public boolean cancelable() {
        return cancelable;
    }

    public static final class Builder {
        String image;
        String content;
        String leftButton;
        String rightButton;
        int gravity;;
        int dialogWidth;
        int dialogHeight;
        int customRes;
        boolean isCustom;
        boolean cancelable;

        public Builder() {
            this.image = "";
            this.content = "";
            this.leftButton = "";
            this.rightButton = "";
            this.gravity = Gravity.CENTER;
            this.dialogWidth = WindowManager.LayoutParams.WRAP_CONTENT;
            this.dialogHeight = WindowManager.LayoutParams.WRAP_CONTENT;
            this.customRes = 0;
            this.isCustom = false;
            this.cancelable = true;
        }

        Builder(CallDialogParam param) {
            this.image = param.image;
            this.content = param.content;
            this.leftButton = param.leftButton;
            this.rightButton = param.rightButton;
            this.gravity = param.gravity;
            this.dialogWidth = param.dialogWidth;
            this.dialogHeight = param.dialogHeight;
            this.customRes = param.customRes;
            this.isCustom = param.isCustom;
            this.cancelable = param.cancelable;
        }

        public Builder image(String image) {
            this.image = image;
            return this;
        }

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

        public Builder leftButton(String leftButton) {
            this.leftButton = leftButton;
            return this;
        }

        public Builder rightButton(String rightButton) {
            this.rightButton = rightButton;
            return this;
        }

        public Builder gravity(int gravity) {
            this.gravity = gravity;
            return this;
        }

        public Builder width(int dialogWidth) {
            this.dialogWidth = dialogWidth;
            return this;
        }

        public Builder height(int dialogHeight) {
            this.dialogHeight = dialogHeight;
            return this;
        }

        public Builder customRes(int customRes) {
            this.customRes = customRes;
            return this;
        }

        public Builder custom(boolean custom) {
            isCustom = custom;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public CallDialogParam build() {
            return new CallDialogParam(this);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章