经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式实践---策略模式实现对大量计算公式的处理
来源:cnblogs  作者:守护月亮的星星  时间:2023/1/6 9:59:31  对本文有异议

业务流程:

1.用户根据需要选择的实验方案,每个实验方案对应一种计算公式,计算公式例如下面这种

 

 2.将带有实验数据的PDF文件上传到特定位置,对PDF文件进行解析后将数据数据保存到数据库。

3.遍历所有方案,对每种方案使用特定的公式对数据库中的数据进行

重构前实现:

遍历方案,使用IF语句对使用的公式进行判断,而后在IF块里对数据进行处理

  1. IF(Formula=='F1'){
  2. //F1的处理...
  3. }
  4. IF(Formula=='F2'){
  5. //F2的处理...
  6. }
  7. IF(Formula=='F3'){
  8. //F2的处理...
  9. }

这样实现的问题就是程序太过庞大,八十多个公式就要有八十多个判断语句,并且判断内部的处理现也是很庞大的,这就导致了这个程序可读性很差,维护以及调试都十分困难。

重构

这里考虑使用策略模式进行重构

策略模式(Strategy Pattern):定义一系列算法,将每一个算法封装起来,并让它们可以相互替换。策略模式让算法独立于使用它的客户而变化。

使用策略模式重构后的程序结构:

定义一个AbstractFormula抽象类,在抽象类中定义一个Calculation计算方法,这个方法返回经过计算的结果的集合,在各个实现类(即具体公式)中实现Calculation方法

定义上下文类,在上下文类中指定要使用的公式,定义Caclute方法用于返回结果。

实现代码

  1. /// <summary>
  2. /// 简易的方案信息
  3. /// </summary>
  4. internal class Schemeinformation
  5. {
  6. //方案ID
  7. public string SchemeID { get; set; }
  8. //方案名称
  9. public string SchemeName { get; set; }
  10. //公式
  11. public string Formula { get; set; }
  12. }
  13. /// <summary>
  14. /// 单个结果
  15. /// </summary>
  16. internal class Result
  17. {
  18. public Result(Schemeinformation schemeinformation, string
  19. Result)
  20. {
  21. Schemeinformation = schemeinformation;
  22. FinalResult=Result;
  23. }
  24. public string FinalResult { get; set; }
  25. public Schemeinformation Schemeinformation { get; set; }
  26. }
  1. //抽象的公式类
  2. internal abstract class AbstractFormula
  3. {
  4. //公式计算后的结果集
  5. public List<string>? tempResultList;
  6. public abstract List<string> Caclution(Schemeinformation
  7. schemeinformation);
  8. }
  1. //具体实现
  2. internal class Formula1 : AbstractFormula
  3. {
  4. public override List<string> Caclution(Schemeinformation schemeinformation)
  5. {
  6. tempResultList = new List<string>();
  7. //计算过程... 对tempResultList赋值
  8. return tempResultList;
  9. }
  10. }
  11. internal class Formula2 : AbstractFormula
  12. {
  13. public override List<string> Caclution(Schemeinformation schemeinformation)
  14. {
  15. tempResultList = new List<string>();
  16. //计算过程...其中需要使用到Schemeinformation中的信息
  17. //对tempResultList赋值
  18. return tempResultList;
  19. }
  20. }
  1. /// <summary>
  2. /// 上下文类 使用公式,并管理结果集
  3. /// </summary>
  4. internal class Context
  5. {
  6. AbstractFormula formula;
  7. public void SetFromula(AbstractFormula formula)
  8. {
  9. this.formula = formula;
  10. }
  11. public List<string> Caclute(Schemeinformation schemeinformation)
  12. {
  13. return formula.Caclution(schemeinformation);
  14. }
  15. }

 

  1. /// <summary>
  2. /// 策略模式实现计算公式重构
  3. /// </summary>
  4. internal class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Context context= new Context();
  9. //获取所有方案信息
  10. List<Schemeinformation> schemeinformationList = new List<Schemeinformation>();
  11. //总结果集
  12. List<Result> results= new List<Result>();
  13. foreach(Schemeinformation schemeinformation in schemeinformationList)
  14. {
  15. //使用反射动态创建公式实例,这里需要保证方案中公式名和类名一致或者自定义匹配规则
  16. context.SetFromula((AbstractFormula)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(schemeinformation.Formula));
  17. //获取当前公式的计算结果集
  18. List<string>tempResults=context.Caclute(schemeinformation);
  19. //遍历结果,将所有结果放入到总结果集中
  20. foreach(string tempResult in tempResults)
  21. {
  22. results.Add(new Result(schemeinformation,tempResult));
  23. }
  24. }
  25. //下面就可以输出总结果集了
  26. Console.WriteLine(results.Count);
  27. }
  28. }

 

原文链接:https://www.cnblogs.com/budongdong/p/17027452.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号