還在寫(xiě)大量 if 來(lái)判斷?試試用一個(gè)規(guī)則執(zhí)行器來(lái)替代它
業(yè)務(wù)場(chǎng)景
if (是否海外用戶(hù)) {return false;}if (刷單用戶(hù)) {return false;}if (未付費(fèi)用戶(hù) && 不再服務(wù)時(shí)段) {return false}if (轉(zhuǎn)介紹用戶(hù) || 付費(fèi)用戶(hù) || 內(nèi)推用戶(hù)) {return true;}
咱們的的主要流程主要是基于 and 或者 or 的關(guān)系。 如果有一個(gè)不匹配的話(huà),其實(shí)咱們后續(xù)的流程是不用執(zhí)行的,就是需要具備一個(gè)短路的功能。 對(duì)于目前的現(xiàn)狀來(lái)說(shuō),我如果在原有的基礎(chǔ)上來(lái)改,只要稍微注意一下解決需求不是很大的問(wèn)題,但是說(shuō)后面可維護(hù)性非常差。
規(guī)則執(zhí)行器
規(guī)則執(zhí)行器的設(shè)計(jì)

對(duì)于規(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= "中國(guó)";}
執(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):比較簡(jiǎn)單,每個(gè)規(guī)則可以獨(dú)立,將規(guī)則,數(shù)據(jù),執(zhí)行器拆分出來(lái),調(diào)用方比較規(guī)整//缺點(diǎn):數(shù)據(jù)依賴(lài)公共傳輸對(duì)象 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. 通過(guò)以鏈?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):
比較簡(jiǎn)單,每個(gè)規(guī)則可以獨(dú)立,將規(guī)則,數(shù)據(jù),執(zhí)行器拆分出來(lái),調(diào)用方比較規(guī)整; 我在 Rule 模板類(lèi)中定義 convert 方法做參數(shù)的轉(zhuǎn)換這樣可以能夠,為特定 rule 需要的場(chǎng)景數(shù)據(jù)提供拓展。
缺點(diǎn):
上下 rule 有數(shù)據(jù)依賴(lài)性,如果直接修改公共傳輸對(duì)象 dto 這樣設(shè)計(jì)不是很合理,建議提前構(gòu)建數(shù)據(jù)。
——————END—————— 歡迎關(guān)注“Java引導(dǎo)者”,我們分享最有價(jià)值的Java的干貨文章,助力您成為有思想的Java開(kāi)發(fā)工程師!
評(píng)論
圖片
表情
