JAVA策略模式(3)之解决具体遇到的一个问题

来源:互联网 发布:js下载文件进度条 编辑:程序博客网 时间:2024/05/18 00:36
 
 if(temporary_drvCalcType.trim().equals("D")&&temporary_ttlCalcMethodCode.trim().equals("H")){
            OnHireDate_Days =DateUtility.daysBetweenTwoDate(bookingDate, OnHireDate);
            resultDVCalculate =replacementValue-(replacementValue *temporary_annualDvPcnt*(OnHireDate_Days)/365/100);
         }
         
         //按天计算方式: DV = Replacement Value - (ReplacementValue * Annual DV* (Declare date - Manufacture Date)/ 365 )
         if(temporary_drvCalcType.trim().equals("D")&& temporary_ttlCalcMethodCode.trim().equals("M")) {
            ManufacturedDate_Days =DateUtility.daysBetweenTwoDate(bookingDate,ManufacturedDate);
            resultDVCalculate = replacementValue-
                  (replacementValue *temporary_annualDvPcnt*(ManufacturedDate_Days)/365/100);
         }
         
         //按月计算方式: DV = Replacement Value - (ReplacementValue * Annual DV* (Declare Month - On Hire Month)/ 12 )
         if(temporary_drvCalcType.trim().equals("M")&& temporary_ttlCalcMethodCode.trim().equals("H")) {
            OnHireDate_Months =DateUtility.getMonthsBetween(bookingDate, OnHireDate);
            resultDVCalculate = replacementValue-
                  (replacementValue *temporary_annualDvPcnt*(OnHireDate_Months)/12/100);
         }
         
         //按月计算方式: DV = Replacement Value - (ReplacementValue * Annual DV* (Declare Month - Manufacture Month)/ 12 )
         if(temporary_drvCalcType.trim().equals("M")&& temporary_ttlCalcMethodCode.trim().equals("M")) {
            ManufacturedDate_Months =DateUtility.getMonthsBetween(bookingDate, ManufacturedDate);
            resultDVCalculate = replacementValue-
                  (replacementValue *temporary_annualDvPcnt*(ManufacturedDate_Months)/12/100);
         }

JAVA策略模式的核心是对算法的封装,上面几种方式,我们选择要传入的参数,通过算法接口执行不同的计算,而我们仅仅需要给出
接口的实现即可,万一以后按月计算方式变化了,我们仅仅需要更改按月计算的实现即可,不需要对主体程序进行变更,你瞧瞧,我们是不是提供
了对程序的可维护性呢。总之到底还是面向接口编程,灵活处理。
比较大小的规则我们接口化了,打折方式我们接口了,这里计算方式我们也接口化了,最后要做的就是提供些具体的策略即可
原创粉丝点击