I. Basic Concepts:

1. Definition:

Separating the construction of a complex object from its representation allows the same construction process to create different representations.

2. Applicable Scenarios:

  • Multiple components can be assembled into the same object, but produce different results
  • The same method, different execution order, produce different event results
  • The computer contains many parts and components, which can be regarded as a complex object. The memory card, motherboard, CPU and so on are all components of the computer. If you build a computer, you do not need to know how to assemble it, but only need to provide the required parts and accessories. At this time, you can use the builder mode.

3. Advantages:

  • Good encapsulation allows users to avoid knowing the details of internal composition
  • The builder is independent and easy to expand

4. Class Diagram:

  • Director: The Director class, responsible for ordering modules and telling the Builder to start building
  • Builder: Abstract Builder class, specification of products
  • ConcreteBuilder: The constructor implements the class, implements all the methods of the abstract class, and returns a constructed object
  • Product: indicates the Product category

Ii. Examples:

1. Product entities:

public class Computer {
    private String cpu;
    private String mainBoard;
    private String ram;

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getMainBoard() {
        return mainBoard;
    }

    public void setMainBoard(String mainBoard) {
        this.mainBoard = mainBoard;
    }

    public String getRam() {
        return ram;
    }

    public void setRam(String ram) { this.ram = ram; }}Copy the code
2. Abstract Builder:
public abstract class Builder {
    public abstract void buildCpu(String cpu);
    public abstract void buildMainBoard(String mainBoard);
    public abstract void buildRam(String ram);
    public abstract Computer build();
}
Copy the code

3, Builder implementation class:

public class ComputerBuilder extends Builder {
    private Computer mComputer = new Computer();
    
    @Override
    public void buildCpu(String cpu) {
        mComputer.setCpu(cpu);
    }

    @Override
    public void buildMainBoard(String mainBoard) {
        mComputer.setMainBoard(mainBoard);
    }

    @Override
    public void buildRam(String ram) {
        mComputer.setRam(ram);
    }

    @Override
    public Computer build() {
        returnmComputer; }}Copy the code

4. Director

Play the role of encapsulation, avoid deep into the concrete implementation of the Builder. Different Builder implementation objects can be passed in.

public class ComputerDirector {
    private Builder mBuilder;

    private ComputerDirector(Builder builder) {
        this.mBuilder = builder;
    }

    public Computer createComputer(String cpu, String mainBoard, String ram) {
        mBuilder.buildCpu(cpu);
        mBuilder.buildMainBoard(mainBoard);
        mBuilder.buildRam(ram);
        returnmBuilder.build(); }}Copy the code

5. Client call:

public class ComputerTest {
    public static void main(String[] args){
        Builder builder = new ComputerBuilder();
        ComputerDirector director = new ComputerDirector(builder);
        director.createComputer("i3"."my board"."4g"); }}Copy the code

In practice, sometimes the Director is omitted, and the caller selects some parameters to set

Third, AlertDialog

1, the Builder

public static class Builder {

    private final AlertController.AlertParams P;

    public Builder(Context context) {
        this(context, resolveDialogTheme(context, 0));
    }

    public Builder(Context context, int themeResId) {
        P = new AlertController.AlertParams(new ContextThemeWrapper(
                context, resolveDialogTheme(context, themeResId)));
    }
   
    public Builder setTitle(@StringRes int titleId) {
        P.mTitle = P.mContext.getText(titleId);
        return this;
    }

    public Builder setCustomTitle(View customTitleView) {
        P.mCustomTitleView = customTitleView;
        return this;
    }

    public Builder setMessage(@StringRes int messageId) {
        P.mMessage = P.mContext.getText(messageId);
        returnthis; }... public AlertDialogcreate() {
        // Context has already been wrapped with the appropriate theme.
        final AlertDialog dialog = new AlertDialog(P.mContext, 0, false); // call the apply method 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();
        returndialog; }}Copy the code

Builder defines some set method is used to set the parameters of the Dialog, Builder within a method is to AlertController AlertParams parameter assignment

2, AlertDialog. AlertParams

public static class AlertParams { public final Context mContext; public final LayoutInflater mInflater; public int mIconId = 0; public Drawable mIcon; public int mIconAttrId = 0; public CharSequence mTitle; public View mCustomTitleView; public CharSequence mMessage; public CharSequence mPositiveButtonText; . public AlertParams(Context context) { mContext = context; mCancelable =true; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void apply(AlertController Dialog) {if(mCustomTitleView ! = null) { dialog.setCustomTitle(mCustomTitleView); }else {
            if(mTitle ! = null) { dialog.setTitle(mTitle); }if(mIcon ! = null) { dialog.setIcon(mIcon); }if(mIconId ! = 0) { dialog.setIcon(mIconId); }if (mIconAttrId != 0) {
                dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
            }
        }
        if(mMessage ! = null) { dialog.setMessage(mMessage); }... }}Copy the code

3. Basic use

AlertDialog.Builder builder = new AlertDialog.Builder(this); // Set the parameter Builder.setTitle ("")
    .setIcon(R.drawable.ic_launcher)
    .setMessage("");
builder.create().show();
Copy the code
  • Create a Builder object and set parameters through Builder
  • These parameters are passed internally to Alertdialog.alertParams
  • Alertdialog. AlertParams sets the parameter to AlertDialog when the create method is called
  • Finally, the dialog is displayed with the show method

The Builder pattern typically acts as a constructor for a configuration class to separate the construction and presentation of a configuration and is implemented through a chain of calls, making code calls more concise.