還在寫(xiě)大量 if 來(lái)判斷?試試用一個(gè)規(guī)則執(zhí)行器來(lái)替代它
點(diǎn)擊上方“碼農(nóng)突圍”,馬上關(guān)注 這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專(zhuān)屬大禮包 真愛(ài),請(qǐng)?jiān)O(shè)置“星標(biāo)”或點(diǎn)個(gè)“在看

業(yè)務(wù)場(chǎng)景
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è)短路的功能。 對(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ù)
@Data
public 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ī)則- 例子1
public 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ī)則- 例子2
public 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= "中國(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)系匹配失敗一次,返回 false
return false;
}
}
// and 關(guān)系全部匹配成功,返回 true
return true;
}
private boolean or(RuleDto dto, List<BaseRule> ruleList) {
for (BaseRule rule : ruleList) {
boolean execute = rule.execute(dto);
if (execute) {
// or 關(guān)系匹配到一個(gè)就返回 true
return true;
}
}
// or 關(guān)系一個(gè)都匹配不到就返回 false
return false;
}
}
執(zhí)行器的調(diào)用
public class RuleServiceTest {
@org.junit.Test
public 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ù)依賴公共傳輸對(duì)象 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. 構(gòu)造需要的數(shù)據(jù) create dto
RuleDto dto = new RuleDto();
dto.setAge(5);
dto.setName("張三");
dto.setAddress("北京");
dto.setSubject("數(shù)學(xué)");;
//3. 通過(guò)以鏈?zhǔn)秸{(diào)用構(gòu)建和執(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);
}
}
總結(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ù)依賴性,如果直接修改公共傳輸對(duì)象 dto 這樣設(shè)計(jì)不是很合理,建議提前構(gòu)建數(shù)據(jù)。
- END - 最近熱文
? 面試官扎心一問(wèn): 為什么 ConcurrentHashMap 的讀操作不需要加鎖? ? Java這個(gè)高級(jí)特性,很多人還沒(méi)用過(guò)! ? 如何優(yōu)雅記錄 http 請(qǐng)求/ 響應(yīng)數(shù)據(jù)? ? 如何寫(xiě)出讓同事無(wú)法維護(hù)的代碼?
評(píng)論
圖片
表情
