<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          還在寫大量 if 來判斷?試試用一個規(guī)則執(zhí)行器來替代它

          共 5855字,需瀏覽 12分鐘

           ·

          2021-06-10 05:53

          往期熱門文章:

          1、騰訊最大股東收購了 Stack Overflow,以后“抄代碼”都要付費了么?
          2、靈隱寺招聘:沒有KPI,佛系上班……
          3、如何優(yōu)雅處理重復請求/并發(fā)請求?
          4、不用到2038年,MySQL的TIMESTAMP就能把我們系統(tǒng)搞崩!
          5、翻車!在項目中用了Arrays.asList、ArrayList的subList,被公開批評

          作者 | 老鄭
          來源 | https://juejin.cn/post/6951764927958745124

          業(yè)務場景

          近日在公司領到一個小需求,需要對之前已有的試用用戶申請規(guī)則進行拓展。我們的場景大概如下所示:
          if (是否海外用戶) { return false;}
          if (刷單用戶) { return false;}
          if (未付費用戶 && 不再服務時段) { return false}
          if (轉介紹用戶 || 付費用戶 || 內推用戶) { return true;}
          按照上述的條件我們可以得出的結論是:
          • 咱們的的主要流程主要是基于 and 或者 or 的關系。
          • 如果有一個不匹配的話,其實咱們后續(xù)的流程是不用執(zhí)行的,就是需要具備一個短路的功能。
          • 對于目前的現狀來說,我如果在原有的基礎上來改,只要稍微注意一下解決需求不是很大的問題,但是說后面可維護性非常差。
          后面進過權衡過后,我還是決定將這個部分進行重構一下。

          規(guī)則執(zhí)行器

          針對這個需求,我首先梳理了一下咱們規(guī)則執(zhí)行器大概的設計, 然后我設計了一個 V1 版本和大家一起分享一下,如果大家也有這樣的 case 可以給我分享留言,下面部分主要是設計和實現的流程和 code.

          規(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; }
          @Override public boolean execute(RuleDto dto) { return executeRule(convert(dto)); }
          protected <T> boolean executeRule(T t) { return true; }}
          // 具體規(guī)則- 例子1public class AddressRule extends AbstractRule { @Override public 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 {
          @Override protected <T> T convert(RuleDto dto) { NationalityRuleDto nationalityRuleDto = new NationalityRuleDto(); if (dto.getAddress().startsWith(MATCH_ADDRESS_START)) { nationalityRuleDto.setNationality(MATCH_NATIONALITY_START); } return (T) nationalityRuleDto; }

          @Override protected <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 關系匹配失敗一次,返回 false return false; } } // and 關系全部匹配成功,返回 true return true; }
          private boolean or(RuleDto dto, List<BaseRule> ruleList) { for (BaseRule rule : ruleList) { boolean execute = rule.execute(dto); if (execute) { // or 關系匹配到一個就返回 true return true; } } // or 關系一個都匹配不到就返回 false return false; }}

          執(zhí)行器的調用

          public class RuleServiceTest {
          @org.junit.Test public void execute() { //規(guī)則執(zhí)行器 //優(yōu)點:比較簡單,每個規(guī)則可以獨立,將規(guī)則,數據,執(zhí)行器拆分出來,調用方比較規(guī)整 //缺點:數據依賴公共傳輸對象 dto
          //1. 定義規(guī)則 init rule AgeRule ageRule = new AgeRule(); NameRule nameRule = new NameRule(); NationalityRule nationalityRule = new NationalityRule(); AddressRule addressRule = new AddressRule(); SubjectRule subjectRule = new SubjectRule();
          //2. 構造需要的數據 create dto RuleDto dto = new RuleDto(); dto.setAge(5); dto.setName("張三"); dto.setAddress("北京"); dto.setSubject("數學");;
          //3. 通過以鏈式調用構建和執(zhí)行 rule execute boolean 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); }}

          總結

          規(guī)則執(zhí)行器的優(yōu)點和缺點

          優(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ā)布,新增了這幾個超實用功能!

          瀏覽 64
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲第6页 | 久久特级毛片 | 2019天天干天天色 | 久久精品一区二区三区四区 | 色图15p |