Strategy mode of design mode

Keywords: C# Programming

Strategy pattern: it defines the algorithm family and encapsulates them separately so that they can be replaced with each other. This pattern makes the algorithm change without affecting the customers who use the algorithm.

Today, let's talk about the promotion cases of shopping malls through a case. What are the activities in the general mall? In summary, there are generally three kinds of promotion activities: 1. Normal charge; 2. Discount; 3. How much to return for the full amount

The object-oriented programming is not that more classes are better, the division of classes is for encapsulation, but the basis of classification is abstraction, and the abstract set with the same properties and functions is the class.

Abstract class of cash charge:

1     abstract class CashSupper //Abstract class of cash charge
2     {
3         public abstract double acceptCash(double money);//Abstract method of cash collection class, cash collection, parameter is original price, return is current price
4     }

Normal charge subclass:

1     class CashNormal : CashSupper //Normal charge subclass
2     {
3         public override double acceptCash(double money)
4         {
5             return money;
6         }
7     }

Discount charge sub category:

 1     class CashRebate : CashSupper //Discount charge subclass
 2     {
 3         private double moneryRebate = 1d;
 4         public CashRebate(string moneryRebate)
 5         {
 6             this.moneryRebate = double.Parse(moneryRebate); //Discount charge. When initializing, you must enter a discount rate, such as 20%, which is 0.8
 7         }
 8         public override double acceptCash(double money)
 9         {
10             return money * moneryRebate;
11         }
12     }

Rebate charge sub category:

 1  class CashReturn : CashSupper
 2     {
 3         private double moneryCondition = 0.0d;
 4         private double MoneryReturn = 0.0d;
 5         public CashReturn(string moneryCondition,string moneryReturn) //For rebate charging, you must enter rebate conditions and rebate values when initializing. For example, if the rebate reaches 300, 100 will be returned moneryCondition=300´╝îmoneryReturn=100
 6         {
 7             this.moneryCondition =double.Parse(moneryCondition);
 8             this.MoneryReturn = double.Parse(moneryReturn);
 9         }
10         public override double acceptCash(double money)
11         {
12             double result = money;
13             if (money>=moneryCondition) //If it is greater than the rebate condition, the rebate value needs to be subtracted
14             {
15                 result = money - Math.Floor(money / moneryCondition) * MoneryReturn;
16             }
17             return result;
18         }
19     }

Cash charging factory:

 1 class CashFactory
 2     {
 3         public static CashSupper createCashAccept(string type)
 4         {
 5             CashSupper cs = null;
 6             switch (type)
 7             {
 8                 case "Normal charge":
 9                     cs = new CashNormal();
10                     break;
11                 case "Full 300 counter 100":
12                     CashReturn cr1 = new CashReturn("300","100");
13                     cs = cr1;
14                     break;
15                 case "Hit 20 percent off":
16                     CashRebate cr2 = new CashRebate("0.8");
17                     cs = cr2;
18                     break;
19             }
20             return cs;
21         }
22     }

CashContext class:

 1  class CashContext
 2     {
 3         private CashSupper cs=null; //Declare a CashSuper
 4         public CashContext(string type) //Indicates the type of charge
 5         {
 6             switch (type)
 7             {
 8                 case "Normal charge":
 9                     CashNormal cs0 = new CashNormal();
10                     cs = cs0;
11                     break;
12                 case "300 full back to 100":
13                     CashReturn cr1 = new CashReturn("300","100");
14                     cs = cr1;
15                     break;
16                 case "Hit 20 percent off":
17                     CashRebate cr2 = new CashRebate("0.8"); //Transfer the process of instantiating specific policies from client to Context Class. References to simple factories
18                     cs = cr2;
19                     break;
20             }
21         }
22         public double GetResult(double Money)
23         {
24             return cs.acceptCash(Money); //According to different charging strategies, the calculation results are obtained
25         }
26     }

 

Interface:

Call:

 1  double total = 0.0d; //User aggregate
 2         private void btnOk_Click(object sender, EventArgs e)
 3         {
 4             CashContext cc = new CashContext(cmbType.SelectedItem.ToString());
 5             double totalPrices = 0d;
 6             totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text)*Convert.ToDouble(txtNumber.Text)); //Through right Context Of GetResult Method can get the result of charging, and isolate the specific algorithm from the customer
 7             total = total + totalPrices;
 8             listBox1.Items.Add("Unit Price:"+txtPrice.Text+"Number:"+txtNumber.Text+" "+cmbType.SelectedItem+"Total:"+totalPrices.ToString());
 9             label5.Text = total.ToString();
10         }

Posted by dgh1981 on Sun, 01 Dec 2019 06:06:54 -0800