首页 > 代码库 > 设计模式笔记2:策略模式

设计模式笔记2:策略模式

1.1  需求

  设计一个商场打折计费的软件,可以实现打折,满300送100等优惠功能。

 

1.2 类图

  

1.3  实现

  我们先把4个计算的类写出来。

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace 策略模式 8 { 9     // 核心类,定义了具体实现功能。算法的不同实现由各个类来完成。10     abstract class CashSuper11     {12         public abstract double AcceptCash(double money);13     }14 15     class CashNormal : CashSuper16     {17         public override double AcceptCash(double money)18         {19             return money;20         }21     }22 23     class CashRebate : CashSuper24     {25         private double rebate = 1.0d;26         public CashRebate(double rebate)27         {28             this.rebate = rebate;29         }30         public override double AcceptCash(double money)31         {32             return this.rebate * money;33         }34     }35 36     class CashReturn : CashSuper37     {38         private double moneyCondition;39         private double moneyReturn;40 41         public CashReturn(double moneyCondition, double moneyReturn)42         {43             this.moneyCondition = moneyCondition;44             this.moneyReturn = moneyReturn;45         }46 47         public override double AcceptCash(double money)48         {49             return money - (int)(money / this.moneyCondition) * this.moneyReturn;50         }51     }52 53 54 55 }
View Code

 

  在写负责创建对象和调用的CashCoxt类

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace 策略模式 8 { 9     class CashContext10     {11         private CashSuper cs;12         // 运用到了简单工厂,根据条件创建指定对象。可用发射优化。13         public CashContext(string type)14         {15             switch (type)16             {17                 case "不打折": cs = new CashNormal();18                     break;19                 case "九折": cs = new CashRebate(0.9d);20                     break;21                 case "五折": cs = new CashRebate(0.5d);22                     break;23                 case "满300返100": cs = new CashReturn(300, 100);24                     break;25             }26         }27 28         public double GetResult(double money)29         {30             return cs.AcceptCash(money);31         }32 33     }34 }
View Code

  

  最后调用的代码:

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace 策略模式 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             while (true)14             {15                 Console.WriteLine("请输入金额:");16                 double money = double.Parse(Console.ReadLine());17                 Console.WriteLine("请选择打折方式:1.不打折    2.九折    3.五折    4.满300返100");18                 int index = int.Parse(Console.ReadLine()) - 1;19                 string type = new String[] { "不打折", "九折", "五折", "满300返100" }[index];20                 // 计算价格21                 Console.WriteLine("你需要付款:" + new CashContext(type).GetResult(money));22                 Console.ReadKey(true);23             }24 25 26 27         }28     }29 }
View Code

 

1.4  总结

 

  我们客户端的代码只用到了CashContext类,降低了耦合。 而策略模式的精髓在于:完成同一个功能有不同的算法时,我们把他抽象到父类。然后每一个功能都由对应的子类来实现。 再用一个中间类为我们创建对象,调用方法。

  这样的2点好处:    

    1、减少了各种算法类之间的耦合。

    2、优化了单元测试。

  策略模式就是用来封装算法的,当我们实践中某一个功能有多种实现的算法。我们就可以考虑使用策略模式。

 

设计模式笔记2:策略模式