Policy Mode : Defines the algorithm family, individually encapsulated, so that they can be replaced with each other, this pattern allows the algorithm to change independently of the customer using the algorithm.
Understand:
The code is similar, but there are different parts of the algorithm, extracted, define an excuse, different algorithms to carry out different implementations, but the call algorithm customers can make a unified call, because the implementation of common excuses, customers do not need to know the specific implementation, but know how to use, that is, for interface programming, And not for implementation programming.
Summary:
Find out where the code might need to be transformed, separate them out, and don't mix with the code that doesn't need to change.
Example Private Textclickinterface mclickinterface; Adopt strategy mode, package change, realize click Processing
Customer
public interface Textclickinterface{<span style= "White-space:pre" ></span>public void click ();//click events, Perform corresponding actions, such as: appointment, <span style= "White-space:pre" ></span>public void Changetext (); Modify the text display effect after click} (define Interface)
(implement a) public class Yuyuetext implements Textclickinterface{private context context; Private Markinfo info; Information private TextView text; Textviewpublic Yuyuetext (Context context,markinfo info, TextView text) {This.context = Context;this.info = info; This.text = text;} @Overridepublic void Click () {//Implement appointment} @Overridepublic void Changetext () {//change font color and content Text.settext (r.string.mark_yuyue_ Success); Text.settextcolor (Context.getresources () GetColor (R.color.mark_font_white)); Text.setbackgroundcolor ( Context.getresources (). GetColor (R.color.mark_blue_already)); Toast.maketext (Context, "booked successfully", Toast.length_short). Show ();;}}
(Implementation II) 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;t His.text = text;} @Overridepublic void Click () {//chip logic} @Overridepublic void Changetext () {//Change text color, text 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, "Request for tablet sent", Toast.length_short). Show ();
Design Patterns in Android--policy mode (the part of the algorithm that encapsulates the change, the interface is not implementation-oriented)