密码校验责任链模式
密码校验规则流水线式处理密码校验
定义密码校验抽象类
public abstract class PasswordCheckHandler {
protected PasswordCheckHandler chain;
public void next(PasswordCheckHandler passwordCheckHandler) {
this.chain = passwordCheckHandler;
}
public abstract String doHandle(Map<String, Integer> map, String[] bit);
}
定义规则实现
字母
public class AlphaCheckHandler extends PasswordCheckHandler {
@Override
public String doHandle(Map<String, Integer> map, String[] bit) {
boolean alphaCheck = (map.get(PasswordRules.UPPER_ALPHA.getRule()) <= 0 && map.get(PasswordRules.LOWER_ALPHA.getRule()) <= 0);
if ("1".equals(bit[0]) && alphaCheck) {
return "至少包含一个字母";
}
chain.doHandle(map, bit);
return null;
}
}
数字
public class DigitalCheckHandler extends PasswordCheckHandler {
@Override
public String doHandle(Map<String, Integer> map, String[] bit) {
if ("1".equals(bit[1]) && (map.get(PasswordRules.DIGIT.getRule()) <= 0)) {
return "至少包含一个数字";
}
chain.doHandle(map, bit);
return null;
}
}
特殊字符
public class SpecialCharCheck extends PasswordCheckHandler {
@Override
public String doHandle(Map<String, Integer> map, String[] bit) {
if ("1".equals(bit[3]) && (map.get(PasswordRules.SPECIAL.getRule()) <= 0)) {
return "至少包含一个特殊字符";
}
chain.doHandle(map, bit);
return null;
}
}
大小写
public class UpperLowerAlphaCheckHandler extends PasswordCheckHandler {
@Override
public String doHandle(Map<String, Integer> map, String[] bit) {
boolean alphaCheck = (map.get(PasswordRules.UPPER_ALPHA.getRule()) <= 0 || map.get(PasswordRules.LOWER_ALPHA.getRule()) <= 0);
if ("1".equals(bit[2]) && alphaCheck) {
return "至少包含一个大写字母和小写字母";
}else {
return null;
}
}
}