Android design mode-builder mode and android design mode --
Looking back at what I wrote, the code written on Android custom controls has a high applicability, but does not seem to have any technical content. So when learning the design model, think about whether something can be improved, for example:
Custom Dialog is required by Android applications, and the system controls are too ugly;
In the construction process, it is completely because, after the new object is complete, there is no problem in writing, and the readability is also good, that is, it does not look bad.
The following is a small part of the code snippet:
Package com. example. demo. builder;/***** @ author qubian * @ data June 10, 2015 * @ email naibbian@163.com **/public class BuilderDialog extends Dialog implements View. onClickListener {private Context mContext; private View view; private View lineView; private TextView titleTv; private TextView contentTv; private Button sureBtn; private Button cancelBtn; private String sureTitle; private String cancelTitle; priv Ate String title; private String content; private boolean sureVisible = true; private boolean cancelVisible = true; private View. onClickListener sureListener; private View. onClickListener cancelListener; public BuilderDialog (Context context, String title, String content) {super (context, R. style. base_dialog_style); this. mContext = context; this. title = title; this. content = content; view = LayoutInflater. fro M (mContext ). inflate (R. layout. dialog_normal, null); addContentView (view, Utils. getDialogLayoutParams (mContext);} public void setSureTitle (String title) {sureTitle = title;} public void setCancelTitle (String title) {cancelTitle = title;} public void setCancelVisible (boolean visible) {cancelVisible = visible;} public void setSureListener (View. onClickListener listener) {if (listener! = Null) {sureListener = listener;} public void setCancelListener (View. onClickListener listener) {}/*** is it possible to return ** @ param canBack */public void setCanBack (boolean canBack) {} // initialize private void initView () {lineView = view. findViewById (R. id. line_view); titleTv = (TextView) view. findViewById (R. id. title_ TV); contentTv = (TextView) view. findViewById (R. id. content_ TV); contentTv. setText (content. replace ("\ r "," \ R "). replace ("\ n", "\ n"); sureBtn = (Button) view. findViewById (R. id. sure_btn); cancelBtn = (Button) view. findViewById (R. id. cancel_btn) ;}@ Overridepublic void show () {initView (); titleTv. setText (title); if (sureVisible) {if (sureListener = null) {sureBtn. setOnClickListener (this);} else {sureBtn. setOnClickListener (sureListener);} if (sureTitle! = Null) {sureBtn. setText (sureTitle) ;}} else {sureBtn. setVisibility (View. GONE);} if (cancelVisible) {} else {} super. show () ;}@ Overridepublic void onClick (View v) {if (v. getId () = sureBtn. getId () {this. cancel ();} else if (v. getId () = cancelBtn. getId () {this. cancel ();}}}
It's okay to use and adapt it, and the logic is also relatively simple. So how can we optimize it?
Back to the truth:
Builder Mode
1. Definition:
Separates a complex build from its representation so that the same build has a different representation.
2. Purpose:
The builder mode refers to the encapsulation of complex internal builds. For other external members, you only need to pass the builders and build tools to get the desired ones without worrying about how to build them, and internal build process.
3. Usage:
3.1 During the building process, different building processes are allowed to generate building objects with different representations;
3.2 When a complex object is built, its complex construction algorithm should be independent of the object components or the assembly method;
4. A simple demo:
Core: Abstract builder, specific builder, entity class
Package com. example. demo. builder; import android. util. log;/*** Builder mode * @ author qubian * @ data June 10, 2015 * @ email naibbian@163.com **/public class Product {Builder mBuilder; public Builder getBuilder () {if (mBuilder = null) {mBuilder = new ProductBuilder () ;}return mBuilder ;} /*** abstract Builder * @ author qubian * @ data June 10, 2015 * @ email naibbian@163.com **/public interface Builder {public Builder buildPart1 (); public Builder buildPart2 (); public Product getProduct ();} /*** specific Builder * @ author qubian * @ data June 10, 2015 * @ email naibbian@163.com **/public class ProductBuilder implements Builder {private static final String TAG = "ProductBuilder "; product mProduct = new Product (); @ Overridepublic Builder buildPart1 () {Log. I (TAG, "buildPart1"); return this ;}@ Overridepublic Builder buildPart2 () {Log. I (TAG, "buildPart2"); return this ;}@ Overridepublic Product getProduct () {return mProduct ;}}}
Usage:
Package com. example. demo. builder;/*** use * @ author qubian * @ data June 10, 2015 * @ email naibbian@163.com **/public class UseProduct {public void use () {Product p = new Product (). getBuilder (). buildPart1 (). buildPart2 (). getProduct ();}}
5. In the Android source code, the builder mode must be indispensable;
The most representative is AlertDialog. During the build process, it is the separation of build and representation. Its internal Builder is His Builder.
public class AlertDialog extends Dialog implements DialogInterface { private AlertController mAlert; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAlert.installContent(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (mAlert.onKeyDown(keyCode, event)) return true; return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (mAlert.onKeyUp(keyCode, event)) return true; return super.onKeyUp(keyCode, event); } public static class Builder { private final AlertController.AlertParams P; private int mTheme; /** * Constructor using a context for this builder and the {@link AlertDialog} it creates. */ public Builder(Context context) { this(context, resolveDialogTheme(context, 0)); } /** * Set the title displayed in the {@link Dialog}. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setTitle(CharSequence title) { P.mTitle = title; return this; } /** * Set the message to display using the given resource id. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setMessage(int messageId) { P.mMessage = P.mContext.getText(messageId); return this; } /** * Set the message to display. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setMessage(CharSequence message) { P.mMessage = message; return this; } /** * Set the resource id of the {@link Drawable} to be used in the title. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setIcon(int iconId) { P.mIconId = iconId; return this; } /** * Set a listener to be invoked when the positive button of the dialog is pressed. * @param textId The resource id of the text to display in the positive button * @param listener The {@link DialogInterface.OnClickListener} to use. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setPositiveButton(int textId, final OnClickListener listener) { P.mPositiveButtonText = P.mContext.getText(textId); P.mPositiveButtonListener = listener; return this; } /** * Set a listener to be invoked when the positive button of the dialog is pressed. * @param text The text to display in the positive button * @param listener The {@link DialogInterface.OnClickListener} to use. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setPositiveButton(CharSequence text, final OnClickListener listener) { P.mPositiveButtonText = text; P.mPositiveButtonListener = listener; return this; } /** * Set a listener to be invoked when the negative button of the dialog is pressed. * @param textId The resource id of the text to display in the negative button * @param listener The {@link DialogInterface.OnClickListener} to use. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setNegativeButton(int textId, final OnClickListener listener) { P.mNegativeButtonText = P.mContext.getText(textId); P.mNegativeButtonListener = listener; return this; } /** * Set a listener to be invoked when the negative button of the dialog is pressed. * @param text The text to display in the negative button * @param listener The {@link DialogInterface.OnClickListener} to use. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setNegativeButton(CharSequence text, final OnClickListener listener) { P.mNegativeButtonText = text; P.mNegativeButtonListener = listener; return this; } /** * Set a listener to be invoked when the neutral button of the dialog is pressed. * @param textId The resource id of the text to display in the neutral button * @param listener The {@link DialogInterface.OnClickListener} to use. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setNeutralButton(int textId, final OnClickListener listener) { P.mNeutralButtonText = P.mContext.getText(textId); P.mNeutralButtonListener = listener; return this; } /** * Set a custom view to be the contents of the Dialog. If the supplied view is an instance * of a {@link ListView} the light background will be used. * * @param view The view to use as the contents of the Dialog. * * @return This Builder object to allow for chaining of calls to set methods */ public Builder setView(View view) { P.mView = view; P.mViewSpacingSpecified = false; return this; } /** * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not * {@link Dialog#show()} the dialog. This allows the user to do any extra processing * before displaying the dialog. Use {@link #show()} if you don't have any other processing * to do and want this to be created and displayed. */ public AlertDialog create() { final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false); 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; } /** * Creates a {@link AlertDialog} with the arguments supplied to this builder and * {@link Dialog#show()}'s the dialog. */ public AlertDialog show() { AlertDialog dialog = create(); dialog.show(); return dialog; } } }
Perhaps for its openness, AlterView also has its own construction process. In this way, the Builder of AlterView can be used to build the view, and its own objects can also be operated on.