還在寫大量 if 來判斷?試試用一個(gè)規(guī)則執(zhí)行器來替代它
往期熱門文章:
3、MySQL究竟是怎么執(zhí)行的?看完終于不糾結(jié)了
5、如何優(yōu)雅處理重復(fù)請求/并發(fā)請求?
業(yè)務(wù)場景
if (是否海外用戶) {return false;}if (刷單用戶) {return false;}if (未付費(fèi)用戶 && 不再服務(wù)時(shí)段) {return false}if (轉(zhuǎn)介紹用戶 || 付費(fèi)用戶 || 內(nèi)推用戶) {return true;}
咱們的的主要流程主要是基于 and 或者 or 的關(guān)系。 如果有一個(gè)不匹配的話,其實(shí)咱們后續(xù)的流程是不用執(zhí)行的,就是需要具備一個(gè)短路的功能。 對于目前的現(xiàn)狀來說,我如果在原有的基礎(chǔ)上來改,只要稍微注意一下解決需求不是很大的問題,但是說后面可維護(hù)性非常差。
規(guī)則執(zhí)行器
規(guī)則執(zhí)行器的設(shè)計(jì)

對于規(guī)則的抽象并實(shí)現(xiàn)規(guī)則
// 業(yè)務(wù)數(shù)據(jù)@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í)行器構(gòu)建
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 關(guān)系,同步執(zhí)行System.out.println("execute key = " + 1);if (!and(dto, ruleList)) {return false;}break;case OR:// 如果是 or 關(guān)系,并行執(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 關(guān)系匹配失敗一次,返回 falsereturn false;}}// and 關(guān)系全部匹配成功,返回 truereturn true;}private boolean or(RuleDto dto, List<BaseRule> ruleList) {for (BaseRule rule : ruleList) {boolean execute = rule.execute(dto);if (execute) {// or 關(guān)系匹配到一個(gè)就返回 truereturn true;}}// or 關(guān)系一個(gè)都匹配不到就返回 falsereturn false;}}
執(zhí)行器的調(diào)用
public class RuleServiceTest {@org.junit.Testpublic void execute() {//規(guī)則執(zhí)行器//優(yōu)點(diǎn):比較簡單,每個(gè)規(guī)則可以獨(dú)立,將規(guī)則,數(shù)據(jù),執(zhí)行器拆分出來,調(diào)用方比較規(guī)整//缺點(diǎn):數(shù)據(jù)依賴公共傳輸對象 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. 構(gòu)造需要的數(shù)據(jù) create dtoRuleDto dto = new RuleDto();dto.setAge(5);dto.setName("張三");dto.setAddress("北京");dto.setSubject("數(shù)學(xué)");;//3. 通過以鏈?zhǔn)秸{(diào)用構(gòu)建和執(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);}}
總結(jié)
優(yōu)點(diǎn):
比較簡單,每個(gè)規(guī)則可以獨(dú)立,將規(guī)則,數(shù)據(jù),執(zhí)行器拆分出來,調(diào)用方比較規(guī)整; 我在 Rule 模板類中定義 convert 方法做參數(shù)的轉(zhuǎn)換這樣可以能夠,為特定 rule 需要的場景數(shù)據(jù)提供拓展。
缺點(diǎn):
上下 rule 有數(shù)據(jù)依賴性,如果直接修改公共傳輸對象 dto 這樣設(shè)計(jì)不是很合理,建議提前構(gòu)建數(shù)據(jù)。
最近熱文閱讀:
2、MySQL究竟是怎么執(zhí)行的?看完終于不糾結(jié)了
4、如何優(yōu)雅處理重復(fù)請求/并發(fā)請求?
5、使用 Redis 實(shí)現(xiàn)一個(gè)輕量級的搜索引擎
6、比MySQL快801倍,OLAP兩大新秀ClickHouse和Doris到底怎么選?
8、巧用 Stream API 優(yōu)化 Java 代碼
10、一次線上 JVM 調(diào)優(yōu)實(shí)踐,F(xiàn)ullGC 40 次/天到 10 天一次的優(yōu)化過程
關(guān)注公眾號,你想要的Java都在這里
評論
圖片
表情
