干掉 if 语句,一个不留的那种!
?作者:烏塔卡
juejin.cn/post/6951764927958745124
業務場景
近日在公司領到一個小需求,需要對之前已有的試用用戶申請規則進行拓展。我們的場景大概如下所示:
if?(是否海外用戶)?{return?false; }if?(刷單用戶)?{return?false; }if?(未付費用戶?&&?不再服務時段)?{return?false }if?(轉介紹用戶?||?付費用戶?||?內推用戶)?{return?true; }按照上述的條件我們可以得出的結論是:
咱們的的主要流程主要是基于 and 或者 or 的關系。
如果有一個不匹配的話,其實咱們后續的流程是不用執行的,就是需要具備一個短路的功能。
對于目前的現狀來說,我如果在原有的基礎上來改,只要稍微注意一下解決需求不是很大的問題,但是說后面可維護性非常差。
后面進過權衡過后,我還是決定將這個部分進行重構一下。
規則執行器
針對這個需求,我首先梳理了一下咱們規則執行器大概的設計, 然后我設計了一個 V1 版本和大家一起分享一下,如果大家也有這樣的 case 可以給我分享留言,下面部分主要是設計和實現的流程和 code.
規則執行器的設計
對于規則的抽象并實現規則
//?業務數據 @Data public?class?RuleDto?{private?String?address;private?int?age; }//?規則抽象 public?interface?BaseRule?{boolean?execute(RuleDto?dto); }//?規則模板 public?abstract?class?AbstractRule?implements?BaseRule?{protected?<T>?T?convert(RuleDto?dto)?{return?(T)?dto;}@Overridepublic?boolean?execute(RuleDto?dto)?{return?executeRule(convert(dto));}protected?<T>?boolean?executeRule(T?t)?{return?true;} }//?具體規則-?例子1 public?class?AddressRule?extends?AbstractRule?{@Overridepublic?boolean?execute(RuleDto?dto)?{System.out.println("AddressRule?invoke!");if?(dto.getAddress().startsWith(MATCH_ADDRESS_START))?{return?true;}return?false;} }//?具體規則-?例子2 public?class?NationalityRule?extends?AbstractRule?{@Overrideprotected?<T>?T?convert(RuleDto?dto)?{NationalityRuleDto?nationalityRuleDto?=?new?NationalityRuleDto();if?(dto.getAddress().startsWith(MATCH_ADDRESS_START))?{nationalityRuleDto.setNationality(MATCH_NATIONALITY_START);}return?(T)?nationalityRuleDto;}@Overrideprotected?<T>?boolean?executeRule(T?t)?{System.out.println("NationalityRule?invoke!");NationalityRuleDto?nationalityRuleDto?=?(NationalityRuleDto)?t;if?(nationalityRuleDto.getNationality().startsWith(MATCH_NATIONALITY_START))?{return?true;}return?false;} }//?常量定義 public?class?RuleConstant?{public?static?final?String?MATCH_ADDRESS_START=?"北京";public?static?final?String?MATCH_NATIONALITY_START=?"中國"; }執行器構建
public?class?RuleService?{private?Map<Integer,?List<BaseRule>>?hashMap?=?new?HashMap<>();private?static?final?int?AND?=?1;private?static?final?int?OR?=?0;public?static?RuleService?create()?{return?new?RuleService();}public?RuleService?and(List<BaseRule>?ruleList)?{hashMap.put(AND,?ruleList);return?this;}public?RuleService?or(List<BaseRule>?ruleList)?{hashMap.put(OR,?ruleList);return?this;}public?boolean?execute(RuleDto?dto)?{for?(Map.Entry<Integer,?List<BaseRule>>?item?:?hashMap.entrySet())?{List<BaseRule>?ruleList?=?item.getValue();switch?(item.getKey())?{case?AND://?如果是?and?關系,同步執行System.out.println("execute?key?=?"?+?1);if?(!and(dto,?ruleList))?{return?false;}break;case?OR://?如果是?or?關系,并行執行System.out.println("execute?key?=?"?+?0);if?(!or(dto,?ruleList))?{return?false;}break;default:break;}}return?true;}private?boolean?and(RuleDto?dto,?List<BaseRule>?ruleList)?{for?(BaseRule?rule?:?ruleList)?{boolean?execute?=?rule.execute(dto);if?(!execute)?{//?and?關系匹配失敗一次,返回?falsereturn?false;}}//?and?關系全部匹配成功,返回?truereturn?true;}private?boolean?or(RuleDto?dto,?List<BaseRule>?ruleList)?{for?(BaseRule?rule?:?ruleList)?{boolean?execute?=?rule.execute(dto);if?(execute)?{//?or?關系匹配到一個就返回?truereturn?true;}}//?or?關系一個都匹配不到就返回?falsereturn?false;} }執行器的調用
public?class?RuleServiceTest?{@org.junit.Testpublic?void?execute()?{//規則執行器//優點:比較簡單,每個規則可以獨立,將規則,數據,執行器拆分出來,調用方比較規整//缺點:數據依賴公共傳輸對象 dto//1.?定義規則??init?ruleAgeRule?ageRule?=?new?AgeRule();NameRule?nameRule?=?new?NameRule();NationalityRule?nationalityRule?=?new?NationalityRule();AddressRule?addressRule?=?new?AddressRule();SubjectRule?subjectRule?=?new?SubjectRule();//2.?構造需要的數據?create?dtoRuleDto?dto?=?new?RuleDto();dto.setAge(5);dto.setName("張三");dto.setAddress("北京");dto.setSubject("數學");;//3.?通過以鏈式調用構建和執行?rule?executeboolean?ruleResult?=?RuleService.create().and(Arrays.asList(nationalityRule,?nameRule,?addressRule)).or(Arrays.asList(ageRule,?subjectRule)).execute(dto);System.out.println("this?student?rule?execute?result?:"?+?ruleResult);} }總結
規則執行器的優點和缺點
優點:
比較簡單,每個規則可以獨立,將規則,數據,執行器拆分出來,調用方比較規整;
我在 Rule 模板類中定義 convert 方法做參數的轉換這樣可以能夠,為特定 rule 需要的場景數據提供拓展。
缺點:
上下 rule 有數據依賴性,如果直接修改公共傳輸對象 dto 這樣設計不是很合理,建議提前構建數據。
2021 最新版 Spring Boot 速記教程
2W 字你全面認識 Nginx
47K Star 的SpringBoot+MyBatis+docker電商項目,附帶超詳細的文檔!
寫博客能月入10K?
一款基于 Spring Boot 的現代化社區(論壇/問答/社交網絡/博客)
這或許是最美的Vue+Element開源后臺管理UI
推薦一款高顏值的 Spring Boot 快速開發框架
一款基于 Spring Boot 的現代化社區(論壇/問答/社交網絡/博客)
13K點贊都基于 Vue+Spring 前后端分離管理系統ELAdmin,大愛
想接私活時薪再翻一倍,建議根據這幾個開源的SpringBoot項目
總結
以上是生活随笔為你收集整理的干掉 if 语句,一个不留的那种!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot+Vue 完整的外卖
- 下一篇: 推荐两个项目!