Android design mode-policy mode, android design mode --

Source: Internet
Author: User

Android design mode-policy mode, android design mode --

1. Definition:

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Defines a series of algorithms (these algorithms implement the same work, but implement different). They can call all algorithms in the same way to reduce the coupling between algorithm classes and algorithms.


2. Purpose:

Abstract specific algorithms and separate each algorithm to form a series of algorithm groups. The algorithms in this algorithm group can be replaced based on actual conditions.


3. Center:

The center of the Policy mode is not how to implement algorithms, but how to organize and call these algorithms, that is, decoupling, forming independent modules, and enhancing program scalability.


Write a simple policy to use

First, write a unified algorithm interface.

/*** Policy mode * unified algorithm interface * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public interface StrategyPattern {/*** calculate the number of note */public int calcLottery (int num );}

Next, write each specific implementation


Package com. example. demo;/implementation of the *** policy mode ** method; * For example, two color ball * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class StrategyPatternImp_SSQ implements StrategyPattern {@ Overridepublic int calcLottery (int num) {return 0 ;}}

Package com. example. demo;/implementation of the *** policy mode ** method; * For example, lotto * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class StrategyPatternImp_DLT implements StrategyPattern {@ Overridepublic int calcLottery (int num) {return 0 ;}}

Finally, different policies are called.

Package com. example. demo;/*** specific use * @ author qubian * @ data June 3, 2015 * @ email naibbian@163.com **/public class LotteryCount {private StrategyPattern strategyPattern; public enum LotteryEnum {SSQ, DLT, QLC;} public int getLotteryCount (LotteryEnum e, int num) {switch (e) {case SSQ: strategyPattern = new StrategyPatternImp_SSQ (); break; case DLT: strategyPattern = new StrategyPatternImp_DLT (); break; default: break;} return strategyPattern. calcLottery (num );}}

Rule modes are widely used in Android Framework;

For example, the commonly used BaseAdapter is actually a policy mode;
The adapter we wrote inherits from the BaseAdapter and implements different algorithms in getview to achieve different view return,
During external use, you can also switch the Adapter based on the data source. This is actually a policy mode;

Adapter is a top-level policy interface.

public interface Adapter {    /**     * How many items are in the data set represented by this Adapter.     *      * @return Count of items.     */    int getCount();           /**     * Get the data item associated with the specified position in the data set.     *      * @param position Position of the item whose data we want within the adapter's      * data set.     * @return The data at the specified position.     */    Object getItem(int position);
    /**     * Get a View that displays the data at the specified position in the data set. You can either     * create a View manually or inflate it from an XML layout file. When the View is inflated, the     * parent View (GridView, ListView...) will apply default layout parameters unless you use     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}     * to specify a root view and to prevent attachment to the root.     *      * @param position The position of the item within the adapter's data set of the item whose view     *        we want.     * @param convertView The old view to reuse, if possible. Note: You should check that this view     *        is non-null and of an appropriate type before using. If it is not possible to convert     *        this view to display the correct data, this method can create a new view.     *        Heterogeneous lists can specify their number of view types, so that this View is     *        always of the right type (see {@link #getViewTypeCount()} and     *        {@link #getItemViewType(int)}).     * @param parent The parent that this view will eventually be attached to     * @return A View corresponding to the data at the specified position.     */    View getView(int position, View convertView, ViewGroup parent);    static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE;        int getItemViewType(int position);    int getViewTypeCount();    static final int NO_SELECTION = Integer.MIN_VALUE;     boolean isEmpty();}

Then Abstract The BaseAdapter

public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {    private final DataSetObservable mDataSetObservable = new DataSetObservable();    public boolean hasStableIds() {        return false;    }    /**     * Notifies the attached observers that the underlying data has been changed     * and any View reflecting the data set should refresh itself.     */    public void notifyDataSetChanged() {        mDataSetObservable.notifyChanged();    }    public View getDropDownView(int position, View convertView, ViewGroup parent) {        return getView(position, convertView, parent);    }    public int getItemViewType(int position) {        return 0;    }    public int getViewTypeCount() {        return 1;    }        public boolean isEmpty() {        return getCount() == 0;    }}








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.