这篇文件介绍使用设计模式中的策略模式和委托来解决多个IfElse判断语句和Switch语句,这种替换方式在其他语言也一样可以做到,比如PHP、JavaScript、Python或者Java等。
这里以C#为例进行演示。
需要为一个程序编写计算方法,根据标签名称来决定不同的计算,大体意思如下:
if(标签名=="值1"){return sum(标签数据,附加标签数据);
}
if(标签名=="值2"){return max(标签数据,附加标签数据);
}
if(标签名=="值3"){return min(标签数据,附加标签数据);
}
if(标签名=="值4"){return 标签数据*blc+附加标签数据;
}
if(标签名=="值5"){return 标签数据+附加标签数据*blc;
}
......
if(标签名=="值21"){return 计算方法21;
}
或者使用Switch语句:
switch(标签名){case "值1":return sum(标签数据,附加标签数据);break;case "值2":return max(标签数据,附加标签数据);break;case "值3":return min(标签数据,附加标签数据);break;......case "值21":return 计算方法21;break;default:return 0;break;
}
因为设备很多,有很多没有列出的标签,后面可能增加的标签最多的时候有20多种,那么这个地方使用这样的方式就很不合适了。
先使用策略模式进行重构,再使用委托进行更加简明的重构。
针对这种问题容易想到的是设计模式中的策略模式,策略模式比较简单,我们可以将这部分计算封装在一个DLL文件中,在程序中调用,这样增加算法可以不必须改动主程序代码。
1、使用策略模式进行改写
策略模式就是封装算法,让算法独立于客户的使用,一般是在确定算法时直接调用对应的算法,我们可以进行改进,生产一个算法列表供选择。
主要是策略模式加算法列表,代码如下:
算法类:
public abstract class YQCYFormula{protected List InterListA;protected List AddonListA;protected double RatioA;public string FormulaNameA;public void SetData(List InterList, List AddonList, double Ratio){this.InterListA = InterList;this.AddonListA = AddonList;this.RatioA = Ratio;}public abstract double GetResult();}public class GetMax:YQCYFormula{public GetMax(){base.FormulaNameA= "最大值";}public override double GetResult(){double DResult = (this.InterListA.Union(AddonListA).ToList()).Max();DResult = Math.Round(DResult, 2);return DResult;}}public class GetMin : YQCYFormula{public GetMin(){base.FormulaNameA = "最小值";}public override double GetResult(){double DResult = (this.InterListA.Union(AddonListA).ToList()).Min();DResult=Math.Round(DResult,2);return DResult;}}public class GetSum : YQCYFormula{public GetSum(){base.FormulaNameA = "总和";}public override double GetResult(){double DResult = this.InterListA.Sum() + this.AddonListA.Sum();DResult = Convert.ToDouble(DResult.ToString("#0.00"));return DResult;}}public class GetAverage : YQCYFormula{public GetAverage(){base.FormulaNameA = "平均值";}public override double GetResult(){double DResult = (this.InterListA.Sum() + this.AddonListA.Sum()) / (this.InterListA.Count + this.AddonListA.Count);DResult = Convert.ToDouble(DResult.ToString("#0.00"));return DResult;}}
实际应用:
ArrayList DictFormula = new ArrayList();//算法列表YQCYFormula MyFormula;//算法实例//注册算法MyFormula = new GetMax();DictFormula.Add(MyFormula);MyFormula = new GetMin();DictFormula.Add(MyFormula);MyFormula = new GetSum();DictFormula.Add(MyFormula);MyFormula = new GetAverage();DictFormula.Add(MyFormula);
使用:
double Ratio = 0.1;List InterList = new List();List AddonList = new List();Ratio = 0.1;InterList = new List { 1.1, 2.2, 3, 3, 4.4, 5.5 };AddonList = new List { 1.5, 2.5, 3.5, 4.5 };textBox1.Text += comboBox1.Text+":"+ GetResult(comboBox1.Text, InterList,AddonList,Ratio).ToString()+Environment.NewLine;
因为算法是在独立的,这样如果有了新的算法,只需要增加算法并注册,调用程序并不需要改变,结果如下。

2、使用委托来改写。
主要使用算法字典和委托进行,代码如下:
定义算法字典:
Dictionary DictCalculation = new Dictionary();
定义算法:
public delegate double FormulaYQCY(List InterList, List AddonList, double Ratio);public FormulaYQCY MyFormula;private double FormulaMax(List InterList,List AddonList,double Ratio){double DResult = (InterList.Union(AddonList).ToList()).Max();DResult = Math.Round(DResult, 2);return DResult;}private double FormulaMin(List InterList, List AddonList,double Ratio){double DResult = (InterList.Union(AddonList).ToList()).Min();DResult = Math.Round(DResult, 2);return DResult;}private double FormulaSum(List InterList, List AddonList, double Ratio){double DResult = InterList.Sum() + AddonList.Sum();DResult = Convert.ToDouble(DResult.ToString("#0.00"));return DResult;}private double FormulaAverage(List InterList, List AddonList, double Ratio){double DResult = (InterList.Sum() + AddonList.Sum()) / (InterList.Count + AddonList.Count);DResult = Convert.ToDouble(DResult.ToString("#0.00"));return DResult;}
注册算法:
MyFormula = FormulaMax;DictCalculation.Add("最大值", MyFormula);MyFormula = FormulaMin;DictCalculation.Add("最小值", MyFormula);MyFormula = FormulaSum;DictCalculation.Add("总和", MyFormula);MyFormula = FormulaAverage;DictCalculation.Add("平均值", MyFormula);
使用:
double DoubleReslut = 0.0;double Ratio = 0.1;List InterList = new List { 1.1, 2.2, 3, 3, 4.4, 5.5 };List AddonList = new List { 1.5, 2.5, 3.5, 4.5 };foreach (var item in DictCalculation){if (item.Key == comboBox1.Text){MyFormula = item.Value as FormulaYQCY;DoubleReslut = MyFormula(InterList, AddonList, Ratio);textBox1.Text += comboBox1.Text+":" + DoubleReslut.ToString()+Environment.NewLine;}}
实际输出:

在实际编写代码过程中,有了字典或者列表,一般情况下我们已经可以不再使用多If...else语句或者Switch语句来进行分支判断输出,代码和程序的灵活性也明显增加了。
上一篇:MySQL常用高级语句