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