Android design mode--strategy mode

Source: Internet
Author: User

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.

Define a series of algorithms (these algorithms achieve the same work, just implement the difference), it can already call all the algorithms in the same way, reduce the coupling between the algorithm class and the algorithm.


2. Purpose:

The specific algorithm is abstracted, each algorithm is independent, forming a series of algorithm groups, the algorithm can be replaced according to the actual situation.


3. Center:

The center of the policy pattern is not how to implement the algorithm, but how to organize and invoke these algorithms, namely: decoupling, forming the independent module, enhancing the program extensibility.


Wrote a simple strategy to use

First, write a unified algorithm interface

/** * Policy mode * Unified Algorithm interface * @author Qubian * @data June 3, 2015 * @email [EMAIL protected] * */public interface Strategypattern {/ * * Calculates the note number */public int calclottery (int num);}

Next, write each specific implementation


Package com.example.demo;/** * Strategy Mode * specific method implementation; * For example Color ball * @author Qubian * @data June 3, 2015 * @email [EMAIL protected] * */ public class STRATEGYPATTERNIMP_SSQ implements Strategypattern {@Overridepublic int calclottery (int num) {return 0;}}

Package com.example.demo;/** * Strategy Mode * specific method implementation; * For example Lotto * @author Qubian * @data June 3, 2015 * @email [EMAIL protected] * */ public class STRATEGYPATTERNIMP_DLT implements strategypattern{@Overridepublic int calclottery (int num) {return 0;}}

Finally, different calls to the policy

Package com.example.demo;/** * Specific Use * @author Qubian * @data June 3, 2015 * @email [EMAIL protected] * */public class Lotter Ycount {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);}}

The strategy model is widely used in the Android Framework;

For example, the baseadapter that we often use is actually a strategic model;
The adapters we write inherit from Baseadapter, implementing different algorithms in the GetView to implement different view returns,
External use can also be based on the data source, switch adapter, such use is actually a strategy mode;

Adapter is a top-level policy interface

Public interface Adapter {    /**     * How many items is 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 on the specified position in the data set. can either * Create a View manually or inflate it from an XML layout file. When the view is inflated, the * parent view (GridView, ListView ...) would apply default layout parameters unless U SE * {@link android.view.layoutinflater#inflate (int, Android.view.ViewGroup, Boolean)} * To specify a root view an     D 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 wan T. * @param convertview The old view to reuse, if possible. Note:you should check the This view * is non-null and of a appropriate type before using.     If It is not possible to convert * This view to display of the correct data, this method can create a new view. * Heterogeneous lists can specify their number of view types, so ' this view is * always of the RI Ght type (See {@link #getViewTypeCount ()} and * {@link #getItemViewType (int)}).  * @param parent The parent that this view would 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 ();}

And then to the abstraction of 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 have 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;}    }








Android design mode--strategy mode

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.