Use case diagram for policy mode
Define an algorithm abstract policy class
Abstract class strategy { //algorithm method public abstract void Algorithminterface (); }
Implementation of specific algorithms through inheritance
Specific algorithm a class Concretestrategya:strategy { //algorithm A Implementation method public override void Algorithminterface () { Console.WriteLine ("Algorithm A Implements"); } } Concrete algorithm B class Concretestrategyb:strategy { ///algorithm B implementation method public override void Algorithminterface () { Console.WriteLine ("Algorithm b implementation"); } } Specific algorithm C class Concretestrategyc:strategy { ///algorithm C implementation method public override void Algorithminterface () { Console.WriteLine ("Algorithm c Implementation"); } }
Contextual class context { strategy strategy; Public Context (Strategy strategy) { this.strategy = strategy; } The context interface public void Contextinterface () { strategy. Algorithminterface (); } }
Client implementation
Context context; Context = new Context (new Concretestrategya ()); Context. Contextinterface (); Context = new Context (new Concretestrategyb ()); Context. Contextinterface (); Context = new Context (new CONCRETESTRATEGYC ()); Context. Contextinterface (); Console.read ();
Run effect
This is the application of the basic strategy pattern
Here we solve a problem, supermarkets in the sale of goods will exist in different situations, if there are three kinds of conditions are sold/87 percent sales/500 minus 1003 marketing strategies
Then we can consider the simple factory class to realize
Factory class definition
Cash charge factory class cashfactory{ //return the corresponding object according to the condition public static Cashsuper createcashaccept (String type) { Cashsuper cs = null; Switch (type) {case "normal charge": cs = new Cashnormal (); break; Case "Full 300 return": cashreturn CR1 = new Cashreturn ("+"); cs = CR1; break; Case "hit 80 percent": cashrebate Cr2 = new Cashrebate ("0.8"); cs = Cr2; break; } return CS; }
Collect Cash Parent class
Cash charge parent Class abstract class cashsuper{ //abstract method: Collect cash, parameter is original price, return as current price public abstract double Acceptcash (double money) ;}
Inherit parent class
Normal charge, inherit Cashsuper class Cashnormal:cashsuper {public override double Acceptcash (double money) { return money; } }
Discounted charge, inherit Cashsuper class Cashrebate:cashsuper { private double moneyrebate = 1d; When initializing, you must enter a discount rate, such as 80 percent, which is 0.8 public cashrebate (string moneyrebate) { this.moneyrebate = double. Parse (moneyrebate); } public override double Acceptcash (double money) { return money * moneyrebate; } }
Rebate charge, inherit Cashsuper class Cashreturn:cashsuper { private double moneycondition = 0.0d; Private double Moneyreturn = 0.0d; The rebate condition and rebate value must be entered when initializing, such as full 300 rebate 100, then moneycondition is 300,moneyreturn to public Cashreturn (String moneycondition, String Moneyreturn) { this.moneycondition = double. Parse (moneycondition); This.moneyreturn = Double. Parse (Moneyreturn); } public override double Acceptcash (double money) { double result = money; If it is greater than the rebate condition, then the rebate value if (Money >= moneycondition) result = Money-math.floor (money/moneycondition) * Moneyr will be subtracted Eturn; return result; }
Client calls are as follows
Use the simple Factory mode to generate the corresponding object cashsuper csuper = cashfactory.createcashaccept (cbxType.SelectedItem.ToString ()) According to the drop-down selection box; Double totalprices = 0d; By polymorphism, you can get the result of a charge totalprices = Csuper.acceptcash (convert.todouble (txtprice.text) * Convert.todouble ( Txtnum.text)); Total = Total + totalprices; LBXLIST.ITEMS.ADD ("Unit Price:" + Txtprice.text + "Quantity:" + Txtnum.text + " + Cbxtype.selecteditem +" total: "+ Totalprices.tos Tring ()); Lblresult.text = total. ToString ();
There is still the problem of previous research, if the merchant has other marketing strategies, to modify to the factory class and so on
So we're thinking about using a strategy model
Implemented as follows in the policy mode
Define a billing strategy Contextclass cashcontext{ //Declare a cash charge parent object private Cashsuper cs; Set the policy behavior, parameters for the specific cash charge subclass (normal, discounted or rebate) public Cashcontext (cashsuper csuper) { This.cs = csuper; } Get Cash Promotions Calculation results (using polymorphic mechanisms, different policy behaviors result in different results) public double GetResult (double money) { return Cs.acceptcash ( Money);} }
Define an abstract class for checkout
Abstract class Cashsuper {public abstract double Acceptcash (double); }
To inherit the various preferential means after the checkout parent class
Class Cashnormal:cashsuper {public override double Acceptcash (double money) { return money; } }
class Cashrebate:cashsuper {private double moneyrebate = 1d; Public cashrebate (String moneyrebate) {this.moneyrebate = double. Parse (moneyrebate); } public override double Acceptcash (double money) {return money * moneyrebate; } }
Class Cashreturn:cashsuper { private double moneycondition = 0.0d; Private double Moneyreturn = 0.0d; Public Cashreturn (String moneycondition,string moneyreturn) { this.moneycondition = double. Parse (moneycondition); This.moneyreturn = Double. Parse (Moneyreturn); } public override double Acceptcash (double money) { double result = money; if (Money >= moneycondition) Result=money-math.floor (money/moneycondition) * Moneyreturn; return result; } }
The client invocation situation is as follows
Cashcontext cc = NULL; Switch (cbxType.SelectedItem.ToString ()) {case "normal charge": cc = new Cashcontext (new Cashnormal ()); break; Case "Full 300 return": cc = new Cashcontext (New Cashreturn ("+"); break; Case "hit 80 percent": cc = new Cashcontext (New Cashrebate ("0.8")); break; } Double totalprices = 0d; Totalprices = cc. GetResult (convert.todouble (txtprice.text) * convert.todouble (Txtnum.text)); Total = Total + totalprices;
This way, if the merchant adds an activity that can be processed at the client, the condition is placed on the client side of the call.
The combination of the policy model and the simple factory pattern--and above is just the difference between the client call and the context
Cash charge factory class cashcontext{ Cashsuper cs = null; Returns the appropriate object public Cashcontext (string type) { switch (type) {case "normal charge" depending on the condition : cashnormal cs0 = new Cashnormal (); cs = Cs0; break; Case "Full 300 return": cashreturn CR1 = new Cashreturn ("+"); cs = CR1; break; Case "hit 80 percent": cashrebate Cr2 = new Cashrebate ("0.8"); cs = Cr2; break; } } Public double GetResult (double money) { return Cs.acceptcash (Money); }}
Client Calls
Cashcontext csuper = new Cashcontext (cbxType.SelectedItem.ToString ()); Double totalprices = 0d; By polymorphism, you can get the result of a charge totalprices = csuper. GetResult (convert.todouble (txtprice.text) * convert.todouble (Txtnum.text)); Total = Total + totalprices;
Design pattern--Strategy mode