考虑类似以下的类
public class MyClass {
private Integer myField;
private Result result;
// more global variables
public MyResult check(Integer myParameter) {
init(myParameter);
if (myField < 0) {
result.setErrorMessage("My error message");
return result;
}
// a lot more 'checks' like above where something may be written
// to the error message and the result gets returned.
}
private void init(Integer myParameter) {
result = new Result();
result.setExistsAnnouncement(/*search a certain object via crudService with myParameter*/);
// initialize other global variables including myField
}
}
问题是上面的check
方法太长并且有很多return
个语句。我想到了一些重构,但我仍然不确定该怎么做。我在考虑像连锁模式这样的东西。然后我会实现几个检查器类,它们可以调用链中的下一个检查器,也可以使用相应的result
返回errorMessage
。
但后来我有一个更好的想法(至少我是这么认为):为什么不表现得像java 8?我想过使用像Try
- Success
- Failure
- Pattern这样的东西。但我不知道如何实现这一点。我想的是:
entrancePoint.check(firstChecker)
.check(secondChecker)
.check // and so on
这个想法是:当check
失败时,它的行为就像Optional.map()
,并返回类似Optional.EMPTY
的内容(或者在此上下文中类似于Failure
)。 check
成功后,应该继续进行下一次检查(返回Success
)。
你有做这样事的经验吗?
答案 0 :(得分:4)
当我们考虑验证时,它通常是复合模式。它大致描述为:
如果此项有效,请执行某些操作。
正如您所强调的那样,您希望将链中的多个检查器链接起来以在其所在区域中执行验证,您可以实施责任链模式。
考虑一下:
您可以拥有一个Result
对象,该对象可以包含有关失败的消息以及简单的真/假。
您可以拥有一个Validator
对象,该对象可以执行所需的任何验证并返回Result
的实例。
public interface Result {
public boolean isOk();
public String getMessage();
}
// We make it genric so that we can use it to validate
// any type of Object that we want.
public interface Validator<T> {
public Result validate(T value);
}
现在,当您说要使用多个检查器验证“X”时,您实施了一个验证规则,它只是一个Validator
个对象的集合,同时也是一个实例Validator
本身。话虽这么说,您不能再使用Result
对象来检查规则的验证结果。您将需要一个复合Result
对象,可以将结果保持为{Validator=Result}
。它看起来不像HashMap<Validator, Result>
的实现吗?是的,因为它是。
现在,您可以将Rule
和CompositeResult
实施为:
public class Rule extends ArrayList<Validator> implements Validator {
public Rule(Validator<?> ... chain) {
addAll(Arrays.asList(chain));
}
public Object validate(Object target) {
CompositeResult result = new CompositeResult(size());
for (Validator rule : this) {
Result tempResult = rule.validate(value);
if (!tempResult.isOk())
result.put(rule, tempResult);
}
return result;
}
}
public class CompositeResult extends HashMap<Validator, Result> implements
Result {
private Integer appliedCount;
public CompositeResult(Integer appliedCount) {
this.appliedCount = appliedCount;
}
@Override
public boolean isOk() {
boolean isOk = true;
for (Result r : values()) {
isOk = r.isOk();
if (!isOk)
break;
}
return isOk;
}
@Override
public String getMessage() {
return toString();
}
public Integer failCount() {
return size();
}
public Integer passCount() {
return appliedCount - size();
}
}
就是这样!现在,实现你的检查器:
public class Checker1 implements Validator<Integer> {
/* Implementation */
}
public class CheckerN implements Validator<Integer> {
/* Implementation */
}
是时候进行验证了:
Validator<Integer> checkingRule = new Rule(new Checker1(), new CheckerN());
CompositeResult result = checkingRule.validate(yourParameter);
if (result.isOk())
System.out.println("All validations passed");
else
System.out.println(result.getFailedCount() + " validations failed");
简单明了。
我上传了example in my public repo给你玩。
答案 1 :(得分:0)
您可以使用Hibernate Validator(或JSR 303/349/380 Bean验证标准的其他实现)。
在使用无效参数实例化时抛出异常的示例用户类。您可以检查所有内置约束in the docs
public final class User {
@NotNull
private final String username;
@NotNull
private final String firstName;
@NotNull
private final String lastName;
public User(String username, String firstName, String lastName) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
MyValidator.validate(this);
}
// public getters omitted
}
验证器类:
import java.security.InvalidParameterException;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public final class MyValidator {
public static void validate(Object object) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Object>> constraintViolations = validator.validate( object );
if (!constraintViolations.isEmpty()) {
ConstraintViolation<Object> firstViolation = constraintViolations.iterator().next();
throw new InvalidParameterException("not valid "
+ object.getClass()
+ " failed property ' " + firstViolation.getPropertyPath() + " ' "
+ " failure message ' " + firstViolation.getMessage() + " ' ");
}
}
}
并留言:
java.security.InvalidParameterException:无效的类 com.foo.bar.User失败的属性'firstName'失败消息'可能不为空'
不要忘记在您的pom.xml中包含the dependency(如果您使用Maven)
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>