Policy mode is a method that defines a series of algorithms. From a conceptual point of view, all these algorithms perform the same job, but they only implement differently, it can call all algorithms in the same way, reducing the direct coupling between various algorithm classes and algorithm classes.
UML diagram:
According to the big talk design pattern-Chapter 2 shopping mall promotion case code, you can simply record the usage of the Strategy pattern:
/// <Summary> /// cash charge abstract class /// </Summary> public abstract class cashsuper {/// <summary> // The super class abstract method of cash charge /// </Summary> /// <Param name = "money"> original price </param> /// <returns> current price </returns> public abstract double acceptcash (double money );}
Cash charge abstract class
/// <Summary> // normal billing subclass /// </Summary> public class cashnormal: cashsuper {public override double acceptcash (double money) {return money ;}} /// <summary> /// discount billing subclass /// </Summary> public class cashrebate: cashsuper {private double moneyrebate = 1D; Public cashrebate (string moneyrebate) {// discount fee. during initialization, you must enter the discount rate. For example, the discount rate is 0.8 this. moneyrebate = double. parse (moneyrebate);} public override double acceptcash (double money) {return money * moneyrebate ;}} /// <summary> /// rebate billing subclass /// </Summary> public class cashreturn: cashsuper {private double moneycondition = 0.0d; private double moneyretrun = 0.0d; public cashreturn (string moneycondition, string moneyreturn) {// billing. You must enter the rebate condition and value during initialization. For example, if 300 is returned when the value is full, moneycondition is 100, moneyreturn is 100 this. moneycondition = double. parse (moneycondition); this. moneyretrun = double. parse (moneyreturn);} public override double acceptcash (double money) {double result = money; If (money> = moneycondition) {result = money-math. floor (money/moneycondition) * moneyretrun;} return result ;}}
Subclass of various algorithms
/// <Summary> /// context class /// </Summary> public class cashcontext {cashsuper cs = NULL; Public cashcontext (string type) {Switch (type) {Case "normal charge": cashnormal CS0 = new cashnormal (); this. cs = CS0; break; Case "Full 300 return 100": cashreturn CS1 = new cashreturn ("300", "100"); this. cs = CS1; break; Case "0.8 off": cashrebate CS2 = new cashrebate (""); this. cs = CS2; break;} public double getresult (double money) {return CS. acceptcash (money );}}
Context
Client code:
Void btnok_click (Object sender, eventargs e) {cashcontext csuper = new cashcontext (this. cbxtype. text); double totalprices = 0d; totalprices = csuper. getresult (convert. todouble (txtprice. text) * convert. todouble (txtnum. text); Total = total + totalprices; lbxlist. items. add ("unit price:" + txtprice. text + "Quantity:" + txtnum. text + "" + cbxtype. text + "Total:" + totalprices. tostring (); this. lbltotal. TEXT = total. tostring ();}
Interface: