先创建好存起来,然后通过key去取
利用容器加载机制
环境:spring-boot
@Autowired(required = false)
Map<String, RuleMatch> references;
//Db2RuleMatch,MssqlRuleMatch,MysqlRuleMatch,OracleRuleMatch,PgsqlRuleMatch ... 继承实现RuleMatch
void method(Bean b1,...){
if (references == null || references.isEmpty()) return;
RuleMatch ruleMatch = references.get(b1.getDbms_type() + "RuleMatch");
ruleMatch.matchRule(...);
}
利用java8的函数接口
final Map<Integer, Function<String, Boolean>> regx = new HashMap<>();
{
regx.put(1, v -> identityCard.match(v));
regx.put(2, this::matchCommonEmail);
regx.put(3, v -> bankCard.match(v));
regx.put(4, this::matchMobilePhone);
//...
}
boolean matchImpl(int[] rules,...) {
//...
for (int rid : rules) {
if (regex.containsKey(rid)) {
if (regex.get(rid).apply(value)) {
return true;
} else {
//...
}
}
}