Java设计模式 —— 建造者模式(Builder)

盛建德
2023-12-01

Java设计模式 —— 建造者模式(Builder)

定义

建造模式:将一个复杂对象的构建与他的表示相分离,使得同样的构建过程可以创建不同的表示。

特征:

  1. 方便用户创建复杂的对象(不需要知道实现过程)
  2. 代码复用性 & 封装性(将对象构建过程和细节进行封装 & 复用)

代码实现

如果一个类有很多属性,此时为此类写一个Builder内部类,来辅助建造该类。

class Phone {
    private String screen;
    private String camera;
    private String cpu;
    private String battery;

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private Phone phone = new Phone();

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

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

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

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

        public Phone build() {
            return phone;
        }
    }
}

android中建造者模式

xref: /frameworks/base/core/java/android/app/AlertDialog.java

在AlertDialog.java类中多了一个Builder内部类

451    public static class Builder {
452        private final AlertController.AlertParams P;
453
454        /**
455         * Creates a builder for an alert dialog that uses the default alert
456         * dialog theme.
457         * <p>
458         * The default alert dialog theme is defined by
459         * {@link android.R.attr#alertDialogTheme} within the parent
460         * {@code context}'s theme.
461         *
462         * @param context the parent context
463         */
464        public Builder(Context context) {
465            this(context, resolveDialogTheme(context, ResourceId.ID_NULL));
466        }
467
468        /**
469         * Creates a builder for an alert dialog that uses an explicit theme
470         * resource.
471         * <p>
472         * The specified theme resource ({@code themeResId}) is applied on top
473         * of the parent {@code context}'s theme. It may be specified as a
474         * style resource containing a fully-populated theme, such as
475         * {@link android.R.style#Theme_Material_Dialog}, to replace all
476         * attributes in the parent {@code context}'s theme including primary
477         * and accent colors.
478         * <p>
479         * To preserve attributes such as primary and accent colors, the
480         * {@code themeResId} may instead be specified as an overlay theme such
481         * as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will
482         * override only the window attributes necessary to style the alert
483         * window as a dialog.
484         * <p>
485         * Alternatively, the {@code themeResId} may be specified as {@code 0}
486         * to use the parent {@code context}'s resolved value for
487         * {@link android.R.attr#alertDialogTheme}.
488         *
489         * @param context the parent context
490         * @param themeResId the resource ID of the theme against which to inflate
491         *                   this dialog, or {@code 0} to use the parent
492         *                   {@code context}'s default alert dialog theme
493         */
494        public Builder(Context context, int themeResId) {
495            P = new AlertController.AlertParams(new ContextThemeWrapper(
496                    context, resolveDialogTheme(context, themeResId)));
497        }
498
499        /**
500         * Returns a {@link Context} with the appropriate theme for dialogs created by this Builder.
501         * Applications should use this Context for obtaining LayoutInflaters for inflating views
502         * that will be used in the resulting dialogs, as it will cause views to be inflated with
503         * the correct theme.
504         *
505         * @return A Context for built Dialogs.
506         */
507        public Context getContext() {
508            return P.mContext;
509        }
510
511        /**
512         * Set the title using the given resource id.
513         *
514         * @return This Builder object to allow for chaining of calls to set methods
515         */
516        public Builder setTitle(@StringRes int titleId) {
517            P.mTitle = P.mContext.getText(titleId);
518            return this;
519        }
520
521        /**
522         * Set the title displayed in the {@link Dialog}.
523         *
524         * @return This Builder object to allow for chaining of calls to set methods
525         */
526        public Builder setTitle(CharSequence title) {
527            P.mTitle = title;
528            return this;
529        }
530
531        /**
532         * Set the title using the custom view {@code customTitleView}.
533         * <p>
534         * The methods {@link #setTitle(int)} and {@link #setIcon(int)} should
535         * be sufficient for most titles, but this is provided if the title
536         * needs more customization. Using this will replace the title and icon
537         * set via the other methods.
538         * <p>
539         * <strong>Note:</strong> To ensure consistent styling, the custom view
540         * should be inflated or constructed using the alert dialog's themed
541         * context obtained via {@link #getContext()}.
542         *
543         * @param customTitleView the custom view to use as the title
544         * @return this Builder object to allow for chaining of calls to set
545         *         methods
546         */
547        public Builder setCustomTitle(View customTitleView) {
548            P.mCustomTitleView = customTitleView;
549            return this;
550        }
551
552        /**
553         * Set the message to display using the given resource id.
554         *
555         * @return This Builder object to allow for chaining of calls to set methods
556         */
557        public Builder setMessage(@StringRes int messageId) {
558            P.mMessage = P.mContext.getText(messageId);
559            return this;
560        }
561
562        /**
563         * Set the message to display.
564          *
565         * @return This Builder object to allow for chaining of calls to set methods
566         */
567        public Builder setMessage(CharSequence message) {
568            P.mMessage = message;
569            return this;
570        }
571
572        /**
573         * Set the resource id of the {@link Drawable} to be used in the title.
574         * <p>
575         * Takes precedence over values set using {@link #setIcon(Drawable)}.
576         *
577         * @return This Builder object to allow for chaining of calls to set methods
578         */
579        public Builder setIcon(@DrawableRes int iconId) {
580            P.mIconId = iconId;
581            return this;
582        }
583
584        /**
585         * Set the {@link Drawable} to be used in the title.
586         * <p>
587         * <strong>Note:</strong> To ensure consistent styling, the drawable
588         * should be inflated or constructed using the alert dialog's themed
589         * context obtained via {@link #getContext()}.
590         *
591         * @return this Builder object to allow for chaining of calls to set
592         *         methods
593         */
594        public Builder setIcon(Drawable icon) {
595            P.mIcon = icon;
596            return this;
597        }
598
599        /**
600         * Set an icon as supplied by a theme attribute. e.g.
601         * {@link android.R.attr#alertDialogIcon}.
602         * <p>
603         * Takes precedence over values set using {@link #setIcon(int)} or
604         * {@link #setIcon(Drawable)}.
605         *
606         * @param attrId ID of a theme attribute that points to a drawable resource.
607         */
608        public Builder setIconAttribute(@AttrRes int attrId) {
609            TypedValue out = new TypedValue();
610            P.mContext.getTheme().resolveAttribute(attrId, out, true);
611            P.mIconId = out.resourceId;
612            return this;
613        }
614
615        /**
616         * Set a listener to be invoked when the positive button of the dialog is pressed.
617         * @param textId The resource id of the text to display in the positive button
618         * @param listener The {@link DialogInterface.OnClickListener} to use.
619         *
620         * @return This Builder object to allow for chaining of calls to set methods
621         */
622        public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
623            P.mPositiveButtonText = P.mContext.getText(textId);
624            P.mPositiveButtonListener = listener;
625            return this;
626        }
627
628        /**
629         * Set a listener to be invoked when the positive button of the dialog is pressed.
630         * @param text The text to display in the positive button
631         * @param listener The {@link DialogInterface.OnClickListener} to use.
632         *
633         * @return This Builder object to allow for chaining of calls to set methods
634         */
635        public Builder setPositiveButton(CharSequence text, final OnClickListener listener) {
636            P.mPositiveButtonText = text;
637            P.mPositiveButtonListener = listener;
638            return this;
639        }
640
641        /**
642         * Set a listener to be invoked when the negative button of the dialog is pressed.
643         * @param textId The resource id of the text to display in the negative button
644         * @param listener The {@link DialogInterface.OnClickListener} to use.
645         *
646         * @return This Builder object to allow for chaining of calls to set methods
647         */
648        public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
649            P.mNegativeButtonText = P.mContext.getText(textId);
650            P.mNegativeButtonListener = listener;
651            return this;
652        }
653
654        /**
655         * Set a listener to be invoked when the negative button of the dialog is pressed.
656         * @param text The text to display in the negative button
657         * @param listener The {@link DialogInterface.OnClickListener} to use.
658         *
659         * @return This Builder object to allow for chaining of calls to set methods
660         */
661        public Builder setNegativeButton(CharSequence text, final OnClickListener listener) {
662            P.mNegativeButtonText = text;
663            P.mNegativeButtonListener = listener;
664            return this;
665        }
666
667        /**
668         * Set a listener to be invoked when the neutral button of the dialog is pressed.
669         * @param textId The resource id of the text to display in the neutral button
670         * @param listener The {@link DialogInterface.OnClickListener} to use.
671         *
672         * @return This Builder object to allow for chaining of calls to set methods
673         */
674        public Builder setNeutralButton(@StringRes int textId, final OnClickListener listener) {
675            P.mNeutralButtonText = P.mContext.getText(textId);
676            P.mNeutralButtonListener = listener;
677            return this;
678        }
679
680        /**
681         * Set a listener to be invoked when the neutral button of the dialog is pressed.
682         * @param text The text to display in the neutral button
683         * @param listener The {@link DialogInterface.OnClickListener} to use.
684         *
685         * @return This Builder object to allow for chaining of calls to set methods
686         */
687        public Builder setNeutralButton(CharSequence text, final OnClickListener listener) {
688            P.mNeutralButtonText = text;
689            P.mNeutralButtonListener = listener;
690            return this;
691        }
692
693        /**
694         * Sets whether the dialog is cancelable or not.  Default is true.
695         *
696         * @return This Builder object to allow for chaining of calls to set methods
697         */
698        public Builder setCancelable(boolean cancelable) {
699            P.mCancelable = cancelable;
700            return this;
701        }
702
703        /**
704         * Sets the callback that will be called if the dialog is canceled.
705         *
706         * <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
707         * being canceled or one of the supplied choices being selected.
708         * If you are interested in listening for all cases where the dialog is dismissed
709         * and not just when it is canceled, see
710         * {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>
711         * @see #setCancelable(boolean)
712         * @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)
713         *
714         * @return This Builder object to allow for chaining of calls to set methods
715         */
716        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
717            P.mOnCancelListener = onCancelListener;
718            return this;
719        }
720
721        /**
722         * Sets the callback that will be called when the dialog is dismissed for any reason.
723         *
724         * @return This Builder object to allow for chaining of calls to set methods
725         */
726        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
727            P.mOnDismissListener = onDismissListener;
728            return this;
729        }
730
731        /**
732         * Sets the callback that will be called if a key is dispatched to the dialog.
733         *
734         * @return This Builder object to allow for chaining of calls to set methods
735         */
736        public Builder setOnKeyListener(OnKeyListener onKeyListener) {
737            P.mOnKeyListener = onKeyListener;
738            return this;
739        }
740
741        /**
742         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
743         * selected item via the supplied listener. This should be an array type i.e. R.array.foo
744         *
745         * @return This Builder object to allow for chaining of calls to set methods
746         */
747        public Builder setItems(@ArrayRes int itemsId, final OnClickListener listener) {
748            P.mItems = P.mContext.getResources().getTextArray(itemsId);
749            P.mOnClickListener = listener;
750            return this;
751        }
752
753        /**
754         * Set a list of items to be displayed in the dialog as the content, you will be notified of the
755         * selected item via the supplied listener.
756         *
757         * @return This Builder object to allow for chaining of calls to set methods
758         */
759        public Builder setItems(CharSequence[] items, final OnClickListener listener) {
760            P.mItems = items;
761            P.mOnClickListener = listener;
762            return this;
763        }
764
765        /**
766         * Set a list of items, which are supplied by the given {@link ListAdapter}, to be
767         * displayed in the dialog as the content, you will be notified of the
768         * selected item via the supplied listener.
769         *
770         * @param adapter The {@link ListAdapter} to supply the list of items
771         * @param listener The listener that will be called when an item is clicked.
772         *
773         * @return This Builder object to allow for chaining of calls to set methods
774         */
775        public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) {
776            P.mAdapter = adapter;
777            P.mOnClickListener = listener;
778            return this;
779        }
780
781        /**
782         * Set a list of items, which are supplied by the given {@link Cursor}, to be
783         * displayed in the dialog as the content, you will be notified of the
784         * selected item via the supplied listener.
785         *
786         * @param cursor The {@link Cursor} to supply the list of items
787         * @param listener The listener that will be called when an item is clicked.
788         * @param labelColumn The column name on the cursor containing the string to display
789         *          in the label.
790         *
791         * @return This Builder object to allow for chaining of calls to set methods
792         */
793        public Builder setCursor(final Cursor cursor, final OnClickListener listener,
794                String labelColumn) {
795            P.mCursor = cursor;
796            P.mLabelColumn = labelColumn;
797            P.mOnClickListener = listener;
798            return this;
799        }
800
801        /**
802         * Set a list of items to be displayed in the dialog as the content,
803         * you will be notified of the selected item via the supplied listener.
804         * This should be an array type, e.g. R.array.foo. The list will have
805         * a check mark displayed to the right of the text for each checked
806         * item. Clicking on an item in the list will not dismiss the dialog.
807         * Clicking on a button will dismiss the dialog.
808         *
809         * @param itemsId the resource id of an array i.e. R.array.foo
810         * @param checkedItems specifies which items are checked. It should be null in which case no
811         *        items are checked. If non null it must be exactly the same length as the array of
812         *        items.
813         * @param listener notified when an item on the list is clicked. The dialog will not be
814         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
815         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
816         *
817         * @return This Builder object to allow for chaining of calls to set methods
818         */
819        public Builder setMultiChoiceItems(@ArrayRes int itemsId, boolean[] checkedItems,
820                final OnMultiChoiceClickListener listener) {
821            P.mItems = P.mContext.getResources().getTextArray(itemsId);
822            P.mOnCheckboxClickListener = listener;
823            P.mCheckedItems = checkedItems;
824            P.mIsMultiChoice = true;
825            return this;
826        }
827
828        /**
829         * Set a list of items to be displayed in the dialog as the content,
830         * you will be notified of the selected item via the supplied listener.
831         * The list will have a check mark displayed to the right of the text
832         * for each checked item. Clicking on an item in the list will not
833         * dismiss the dialog. Clicking on a button will dismiss the dialog.
834         *
835         * @param items the text of the items to be displayed in the list.
836         * @param checkedItems specifies which items are checked. It should be null in which case no
837         *        items are checked. If non null it must be exactly the same length as the array of
838         *        items.
839         * @param listener notified when an item on the list is clicked. The dialog will not be
840         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
841         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
842         *
843         * @return This Builder object to allow for chaining of calls to set methods
844         */
845        public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems,
846                final OnMultiChoiceClickListener listener) {
847            P.mItems = items;
848            P.mOnCheckboxClickListener = listener;
849            P.mCheckedItems = checkedItems;
850            P.mIsMultiChoice = true;
851            return this;
852        }
853
854        /**
855         * Set a list of items to be displayed in the dialog as the content,
856         * you will be notified of the selected item via the supplied listener.
857         * The list will have a check mark displayed to the right of the text
858         * for each checked item. Clicking on an item in the list will not
859         * dismiss the dialog. Clicking on a button will dismiss the dialog.
860         *
861         * @param cursor the cursor used to provide the items.
862         * @param isCheckedColumn specifies the column name on the cursor to use to determine
863         *        whether a checkbox is checked or not. It must return an integer value where 1
864         *        means checked and 0 means unchecked.
865         * @param labelColumn The column name on the cursor containing the string to display in the
866         *        label.
867         * @param listener notified when an item on the list is clicked. The dialog will not be
868         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
869         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
870         *
871         * @return This Builder object to allow for chaining of calls to set methods
872         */
873        public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn,
874                final OnMultiChoiceClickListener listener) {
875            P.mCursor = cursor;
876            P.mOnCheckboxClickListener = listener;
877            P.mIsCheckedColumn = isCheckedColumn;
878            P.mLabelColumn = labelColumn;
879            P.mIsMultiChoice = true;
880            return this;
881        }
882
883        /**
884         * Set a list of items to be displayed in the dialog as the content, you will be notified of
885         * the selected item via the supplied listener. This should be an array type i.e.
886         * R.array.foo The list will have a check mark displayed to the right of the text for the
887         * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a
888         * button will dismiss the dialog.
889         *
890         * @param itemsId the resource id of an array i.e. R.array.foo
891         * @param checkedItem specifies which item is checked. If -1 no items are checked.
892         * @param listener notified when an item on the list is clicked. The dialog will not be
893         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
894         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
895         *
896         * @return This Builder object to allow for chaining of calls to set methods
897         */
898        public Builder setSingleChoiceItems(@ArrayRes int itemsId, int checkedItem,
899                final OnClickListener listener) {
900            P.mItems = P.mContext.getResources().getTextArray(itemsId);
901            P.mOnClickListener = listener;
902            P.mCheckedItem = checkedItem;
903            P.mIsSingleChoice = true;
904            return this;
905        }
906
907        /**
908         * Set a list of items to be displayed in the dialog as the content, you will be notified of
909         * the selected item via the supplied listener. The list will have a check mark displayed to
910         * the right of the text for the checked item. Clicking on an item in the list will not
911         * dismiss the dialog. Clicking on a button will dismiss the dialog.
912         *
913         * @param cursor the cursor to retrieve the items from.
914         * @param checkedItem specifies which item is checked. If -1 no items are checked.
915         * @param labelColumn The column name on the cursor containing the string to display in the
916         *        label.
917         * @param listener notified when an item on the list is clicked. The dialog will not be
918         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
919         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
920         *
921         * @return This Builder object to allow for chaining of calls to set methods
922         */
923        public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn,
924                final OnClickListener listener) {
925            P.mCursor = cursor;
926            P.mOnClickListener = listener;
927            P.mCheckedItem = checkedItem;
928            P.mLabelColumn = labelColumn;
929            P.mIsSingleChoice = true;
930            return this;
931        }
932
933        /**
934         * Set a list of items to be displayed in the dialog as the content, you will be notified of
935         * the selected item via the supplied listener. The list will have a check mark displayed to
936         * the right of the text for the checked item. Clicking on an item in the list will not
937         * dismiss the dialog. Clicking on a button will dismiss the dialog.
938         *
939         * @param items the items to be displayed.
940         * @param checkedItem specifies which item is checked. If -1 no items are checked.
941         * @param listener notified when an item on the list is clicked. The dialog will not be
942         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
943         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
944         *
945         * @return This Builder object to allow for chaining of calls to set methods
946         */
947        public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) {
948            P.mItems = items;
949            P.mOnClickListener = listener;
950            P.mCheckedItem = checkedItem;
951            P.mIsSingleChoice = true;
952            return this;
953        }
954
955        /**
956         * Set a list of items to be displayed in the dialog as the content, you will be notified of
957         * the selected item via the supplied listener. The list will have a check mark displayed to
958         * the right of the text for the checked item. Clicking on an item in the list will not
959         * dismiss the dialog. Clicking on a button will dismiss the dialog.
960         *
961         * @param adapter The {@link ListAdapter} to supply the list of items
962         * @param checkedItem specifies which item is checked. If -1 no items are checked.
963         * @param listener notified when an item on the list is clicked. The dialog will not be
964         *        dismissed when an item is clicked. It will only be dismissed if clicked on a
965         *        button, if no buttons are supplied it's up to the user to dismiss the dialog.
966         *
967         * @return This Builder object to allow for chaining of calls to set methods
968         */
969        public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) {
970            P.mAdapter = adapter;
971            P.mOnClickListener = listener;
972            P.mCheckedItem = checkedItem;
973            P.mIsSingleChoice = true;
974            return this;
975        }
976
977        /**
978         * Sets a listener to be invoked when an item in the list is selected.
979         *
980         * @param listener the listener to be invoked
981         * @return this Builder object to allow for chaining of calls to set methods
982         * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)
983         */
984        public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) {
985            P.mOnItemSelectedListener = listener;
986            return this;
987        }
988
989        /**
990         * Set a custom view resource to be the contents of the Dialog. The
991         * resource will be inflated, adding all top-level views to the screen.
992         *
993         * @param layoutResId Resource ID to be inflated.
994         * @return this Builder object to allow for chaining of calls to set
995         *         methods
996         */
997        public Builder setView(int layoutResId) {
998            P.mView = null;
999            P.mViewLayoutResId = layoutResId;
1000            P.mViewSpacingSpecified = false;
1001            return this;
1002        }
1003
1004        /**
1005         * Sets a custom view to be the contents of the alert dialog.
1006         * <p>
1007         * When using a pre-Holo theme, if the supplied view is an instance of
1008         * a {@link ListView} then the light background will be used.
1009         * <p>
1010         * <strong>Note:</strong> To ensure consistent styling, the custom view
1011         * should be inflated or constructed using the alert dialog's themed
1012         * context obtained via {@link #getContext()}.
1013         *
1014         * @param view the view to use as the contents of the alert dialog
1015         * @return this Builder object to allow for chaining of calls to set
1016         *         methods
1017         */
1018        public Builder setView(View view) {
1019            P.mView = view;
1020            P.mViewLayoutResId = 0;
1021            P.mViewSpacingSpecified = false;
1022            return this;
1023        }
1024
1025        /**
1026         * Sets a custom view to be the contents of the alert dialog and
1027         * specifies additional padding around that view.
1028         * <p>
1029         * When using a pre-Holo theme, if the supplied view is an instance of
1030         * a {@link ListView} then the light background will be used.
1031         * <p>
1032         * <strong>Note:</strong> To ensure consistent styling, the custom view
1033         * should be inflated or constructed using the alert dialog's themed
1034         * context obtained via {@link #getContext()}.
1035         *
1036         * @param view the view to use as the contents of the alert dialog
1037         * @param viewSpacingLeft spacing between the left edge of the view and
1038         *                        the dialog frame
1039         * @param viewSpacingTop spacing between the top edge of the view and
1040         *                       the dialog frame
1041         * @param viewSpacingRight spacing between the right edge of the view
1042         *                         and the dialog frame
1043         * @param viewSpacingBottom spacing between the bottom edge of the view
1044         *                          and the dialog frame
1045         * @return this Builder object to allow for chaining of calls to set
1046         *         methods
1047         *
1048         * @hide Remove once the framework usages have been replaced.
1049         * @deprecated Set the padding on the view itself.
1050         */
1051        @Deprecated
1052        public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
1053                int viewSpacingRight, int viewSpacingBottom) {
1054            P.mView = view;
1055            P.mViewLayoutResId = 0;
1056            P.mViewSpacingSpecified = true;
1057            P.mViewSpacingLeft = viewSpacingLeft;
1058            P.mViewSpacingTop = viewSpacingTop;
1059            P.mViewSpacingRight = viewSpacingRight;
1060            P.mViewSpacingBottom = viewSpacingBottom;
1061            return this;
1062        }
1063
1064        /**
1065         * Sets the alert dialog to use the inverse background, regardless of
1066         * what the contents is.
1067         *
1068         * @param useInverseBackground whether to use the inverse background
1069         * @return this Builder object to allow for chaining of calls to set methods
1070         * @deprecated This flag is only used for pre-Material themes. Instead,
1071         *             specify the window background using on the alert dialog
1072         *             theme.
1073         */
1074        @Deprecated
1075        public Builder setInverseBackgroundForced(boolean useInverseBackground) {
1076            P.mForceInverseBackground = useInverseBackground;
1077            return this;
1078        }
1079
1080        /**
1081         * @hide
1082         */
1083        public Builder setRecycleOnMeasureEnabled(boolean enabled) {
1084            P.mRecycleOnMeasure = enabled;
1085            return this;
1086        }
1087
1088
1089        /**
1090         * Creates an {@link AlertDialog} with the arguments supplied to this
1091         * builder.
1092         * <p>
1093         * Calling this method does not display the dialog. If no additional
1094         * processing is needed, {@link #show()} may be called instead to both
1095         * create and display the dialog.
1096         */
1097        public AlertDialog create() {
1098            // Context has already been wrapped with the appropriate theme.
1099            final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
1100            P.apply(dialog.mAlert);
1101            dialog.setCancelable(P.mCancelable);
1102            if (P.mCancelable) {
1103                dialog.setCanceledOnTouchOutside(true);
1104            }
1105            dialog.setOnCancelListener(P.mOnCancelListener);
1106            dialog.setOnDismissListener(P.mOnDismissListener);
1107            if (P.mOnKeyListener != null) {
1108                dialog.setOnKeyListener(P.mOnKeyListener);
1109            }
1110            return dialog;
1111        }
1112
1113        /**
1114         * Creates an {@link AlertDialog} with the arguments supplied to this
1115         * builder and immediately displays the dialog.
1116         * <p>
1117         * Calling this method is functionally identical to:
1118         * <pre>
1119         *     AlertDialog dialog = builder.create();
1120         *     dialog.show();
1121         * </pre>
1122         */
1123        public AlertDialog show() {
1124            final AlertDialog dialog = create();
1125            dialog.show();
1126            return dialog;
1127        }
1128    }

感谢

segmentfault: 建造者模式
菜鸟教程: 建造者模式
百度百科:建造者模式
简书:https://www.jianshu.com/p/3d1c9ffb0a28

 类似资料: