gpt4 book ai didi

java - 如何在java中链接检查

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:04:06 25 4
gpt4 key购买 nike

考虑如下类

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 语句。我想到了一些重构,但仍然不确定该怎么做。我在想类似链式模式的东西。然后我会实现几个检查器类,它们要么调用链中的下一个检查器,要么返回带有相应 errorMessageresult

但后来我有了一个更好的主意(至少我是这么认为的):为什么不像 java 8 那样运行呢?我想到了使用类似Try-Success-Failure-Pattern 的东西。但我不知道如何实现这一点。我在想类似的事情:

entrancePoint.check(firstChecker)
.check(secondChecker)
.check // and so on

想法是:当 check 失败时,它会表现得像 Optional.map() 并返回类似 Optional.EMPTY 的东西(或者在这种情况下类似于 Failure)。当 check 成功时,它应该继续进行下一次检查(返回 Success)。

你有过这样的经验吗?

最佳答案

当我们考虑验证时,它通常是复合模式。它大致被描述为:

如果这是有效的,那么做一些事情

当你强加时,你想将多个检查器链接成一个链条以在他们的区域执行验证,你可以实现一个责任链模式。

考虑一下:

你可以有一个Result对象,可以包含有关失败的消息以及简单的 true/false。

你可以有一个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> 的实现吗? ?是的,因为它是。

现在您可以实现您的 RuleCompositeResult作为:

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供您玩耍。

关于java - 如何在java中链接检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43226111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com