Android design mode-builder mode

Source: Internet
Author: User



Looking back at what you wrote, when it comes to Android custom controls, the code is more adaptable, but it doesn't seem to have any technical content, so when you're learning design patterns, think about something that might improve, like:



Custom dialog is a must for Android apps, and the system's controls are really ugly;



In the construction, it is completely, after new end of the object, what needs to build what, so write no problem, readability is OK, is not what it seems.






Here are a few pieces of code:




package com.example.demo.Builder;

/**
 *
 * @author qubian
 * @data June 10, 2015
 * @email [email protected]
 *
 */
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;
private 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.from (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) {

}
       //initialization
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);
}

@Override
public 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 ();
}

@Override
public void onClick (View v) {
if (v.getId () == sureBtn.getId ()) {
this.cancel ();
} else if (v.getId () == cancelBtn.getId ()) {
this.cancel ();
}
}

}

The use, and the application are no problem, and the logic is relatively simple, then how to optimize it?








Anyway



Builder mode






1. Definition:



Separating a complex construct from its representation makes the same build a different representation.



2. Purpose:



The builder pattern is that complex internal builds are encapsulated internally, and for other external members, only the builders and build tools need to be passed to get what they need without caring about how they are built, and the internal build process.



3. Use:



3.1, in the process of construction, allow different construction process, to produce different representations of the construction object;



3.2, in complex objects, its complex construction algorithm should be independent of the object components, or is independent of the assembly mode;



4, a simple demo:



Core: Abstract builder, concrete builder, entity class




package com.example.demo.Builder;

Import android.util.Log;
/**
 * Builder mode
 * @author qubian
 * @data June 10, 2015
 * @email [email protected]
 *
 */
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 [email protected]
*
*/
public interface Builder
{
public Builder buildPart1 ();

public Builder buildPart2 ();

public Product getProduct ();
}
/**
* Specific builders
* @author qubian
* @data June 10, 2015
* @email [email protected]
*
*/
public class ProductBuilder implements Builder
{
private static final String TAG = "ProductBuilder";
Product mProduct = new Product ();
@Override
public Builder buildPart1 () {
Log.i (TAG, "buildPart1");
return this;

}

@Override
public Builder buildPart2 () {
Log.i (TAG, "buildPart2");
return this;

}

@Override
public Product getProduct () {

return mProduct;
}

}

}

Use:






package com.example.demo.Builder;

/**
  * Use
  * @author qubian
  * @data June 10, 2015
  * @email [email protected]
  *
  * /
public class UseProduct {

public void use ()
{
Product p = new Product (). GetBuilder (). BuildPart1 (). BuildPart2 (). GetProduct ();
}
Ranch
}

5, in the Android source code, the builder mode, certainly is essential;





The most representative is Alertdialog, in its construction process, is the separation of construction and representation. the builder of the interior 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 build process, so that the builder of Alterview can build a view, and his own object can manipulate itself.





Android design mode-builder mode


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.