Design Pattern in Android-policy pattern (encapsulate the changed algorithm part, and the interface-oriented is not implemented), android Design Pattern

Source: Internet
Author: User

Design Pattern in Android-policy pattern (encapsulate the changed algorithm part, and the interface-oriented is not implemented), android Design Pattern

Policy mode: defines algorithm families, which are encapsulated separately so that they can be replaced with each other. This mode allows algorithm changes to be independent of algorithm customers.


Understanding:

Extract the similar but different parts of the code and define an excuse for different algorithms for different implementations. However, customers who call the algorithms can make unified calls, because of the common excuse, the customer does not need to know the specific implementation, but how to use it, that is, programming for the interface, rather than programming for the implementation.


Summary:

Find out the possible changes in the Code and separate them. Do not mix them with the code that does not need to be changed.


Example: private TextClickInterface mClickInterface; // uses the policy mode to encapsulate changes for click Processing
(Customer)
 
Public interface TextClickInterface {<span style = "white-space: pre"> </span> public void click (); // click an event to execute the action, for example: <span style = "white-space: pre"> </span> public void changeText (); // modify the Text display result after clicking it} (define interface)
(Implementation 1) public class YuyueText implements TextClickInterface {private Context context; private MarkInfo info; // information private TextView text; // textviewpublic yuetext (Context context, MarkInfo info, TextView text) {this. context = context; this.info = info; this. text = text;} @ Overridepublic void click () {// implement reservation} @ Overridepublic void changeText () {// change the font color and content text. setText (R. string. mark_yue_success); text. setTextColor (context. getResources (). getColor (R. color. mark_font_white); text. setBackgroundColor (context. getResources (). getColor (R. color. mark_blue_already); Toast. makeText (context, "Reservation successful", Toast. LENGTH_SHORT ). show ();;}}
(Implementation 2) public class QiupianText implements TextClickInterface {private Context context; private MarkInfo info; private TextView text; public QiupianText (Context context, MarkInfo info, TextView text) {this. context = context; this.info = info; this. text = text ;}@ Overridepublic void click () {// seek piece logic} @ Overridepublic void changeText () {// change the text color and text. setText (R. string. mark_qiupian_success); text. setTextColor (context. getResources (). getColor (R. color. mark_font_white); text. setBackgroundColor (context. getResources (). getColor (R. color. mark_blue_already); Toast. makeText (context, "slice request sent", Toast. LENGTH_SHORT ). show ();}


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.