還在寫大量 if 來判斷?試試用一個規(guī)則執(zhí)行器來替代它
1、騰訊最大股東收購了 Stack Overflow,以后“抄代碼”都要付費了么? 2、靈隱寺招聘:沒有KPI,佛系上班…… 3、如何優(yōu)雅處理重復請求/并發(fā)請求? 4、不用到2038年,MySQL的TIMESTAMP就能把我們系統(tǒng)搞崩! 5、翻車!在項目中用了Arrays.asList、ArrayList的subList,被公開批評
業(yè)務場景
if (是否海外用戶) {return false;}if (刷單用戶) {return false;}if (未付費用戶 && 不再服務時段) {return false}if (轉介紹用戶 || 付費用戶 || 內推用戶) {return true;}
咱們的的主要流程主要是基于 and 或者 or 的關系。 如果有一個不匹配的話,其實咱們后續(xù)的流程是不用執(zhí)行的,就是需要具備一個短路的功能。 對于目前的現狀來說,我如果在原有的基礎上來改,只要稍微注意一下解決需求不是很大的問題,但是說后面可維護性非常差。
規(guī)則執(zhí)行器
規(guī)則執(zhí)行器的設計

對于規(guī)則的抽象并實現規(guī)則
// 業(yè)務數據@Datapublic class RuleDto {private String address;private int age;}// 規(guī)則抽象public interface BaseRule {boolean execute(RuleDto dto);}// 規(guī)則模板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;}}// 具體規(guī)則- 例子1public 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;}}// 具體規(guī)則- 例子2public 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= "中國";}
執(zhí)行器構建
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 關系,同步執(zhí)行System.out.println("execute key = " + 1);if (!and(dto, ruleList)) {return false;}break;case OR:// 如果是 or 關系,并行執(zhí)行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;}}
執(zhí)行器的調用
public class RuleServiceTest {@org.junit.Testpublic void execute() {//規(guī)則執(zhí)行器//優(yōu)點:比較簡單,每個規(guī)則可以獨立,將規(guī)則,數據,執(zhí)行器拆分出來,調用方比較規(guī)整//缺點:數據依賴公共傳輸對象 dto//1. 定義規(guī)則 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. 通過以鏈式調用構建和執(zhí)行 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);}}
總結
優(yōu)點:
比較簡單,每個規(guī)則可以獨立,將規(guī)則,數據,執(zhí)行器拆分出來,調用方比較規(guī)整; 我在 Rule 模板類中定義 convert 方法做參數的轉換這樣可以能夠,為特定 rule 需要的場景數據提供拓展。
缺點:
上下 rule 有數據依賴性,如果直接修改公共傳輸對象 dto 這樣設計不是很合理,建議提前構建數據。
往期熱門文章:
1、《歷史文章分類導讀列表!精選優(yōu)秀博文都在這里了!》
2、為什么不建議你用a.equals(b)判斷對象相等 3、為什么 Java 后端開發(fā)沒有大規(guī)模采用 Kotlin? 4、為什么不推薦使用BeanUtils屬性轉換工具 5、Top 16 的 Java 工具類,你用過幾個? 6、分享幾個酷炫的 IDEA 主題 7、Intellij IDEA 這樣配置注釋模板,讓你瞬間高出一個逼格!
8、【建議收藏】面試官會問的位運算奇淫技巧 9、到底可不可以用 kill -9 關閉程序? 10、IDEA 2021首個大版本發(fā)布,新增了這幾個超實用功能!
評論
圖片
表情
