如何整合Java中的有效性检查和异常抛出?

我正在实现一个接口,该接口定义了一种方法,如果参数无效,该方法可以引发异常.有效参数的构成取决于实现类.该接口还定义了一个isValid()方法,该方法可用于检查参数,但返回布尔值而不是引发异常.我发现实现这两种方法将导致大量重复.考虑下面的示例:

public class Something implements SomeInterface {

    // Other class stuff

    public void doTheThing(SomeParameter sp) throws SpecificRuntimeException {
        if(sp == null) throw new ParameterCannotBeNullException();
        if(sp.getNonZeroInt() == 0) throw new ShouldBeNonZeroException();
        if(!sp.someOtherCondition()) throw new SomeConditionNotMetException();

        ...
    }

    public boolean isValid(SomeParameter sp) {
       if(sp == null) return false;
       if(sp.getNonZeroInt() == 0) return false;
       if(!sp.someOtherCondition()) return false;

       ...
       return true;
    }
}

问题在于两种方法中的检查必须一致,并且本质上是重复的逻辑.我一直在尝试合并检查,以便两种方法都使用相同的检查,但是行为仍然保留.我考虑过的一些事情:

>在doTheThing()中包含if(!isValid(sp)这一行引发new RuntimeException();
>将异常抛出部分分为一个单独的私有方法,例如checkParameter()并在isValid()中执行:try {checkParameter(sp);返回true; } catch(SpecificRunTimeException e){返回false; }

1.的问题是特定的异常丢失了,我想提供尽可能详细的异常. 2.使用异常机制的问题似乎…某种程度上是错误的.这部分代码可能对性能敏感,因此我不想依赖根本上慢于某些东西的东西(如果我必须这样做,并且性能分析没有显示问题,那是很公平的.) ,但是如果有问题怎么办?).还是这种使用例外的性能影响可以忽略不计?

重构此代码以使用相同的有效性检查逻辑的最佳方法是什么?

解决方法:

如果您创建一个isValidParam方法,该方法返回一个类似于以下内容的bean,该怎么办?

class ValidBean {
  private boolean isValid;
  private Exception exceptionOnInvalid;
  public ValidBean(boolean isValid, Exception exceptionOnInvalid) {...}
  // accessors
}

使用的方法是:

private ValidBean isValidParam(SomeParameter sp) {
       if (sp == null) {
          return new ValidBean(false, new ParameterCannotBeNullException());
       }
       if (sp.getNonZeroInt() == 0) { 
         return new ValidBean(false, new ShouldBeNonZeroException());
       }
       if (!sp.someOtherCondition()) {
          return new ValidBean(false, new SomeConditionNotMetException());
       }
       …
       return new ValidBean(true, null);
    }

然后您执行以下操作:

public boolean isValid(SomeParameter sp) {
 return isValidParam(sp).isValid();
}

public void doTheThing(SomeParameter sp) throws SpecificRuntimeException {
  ValidBean validbean = isValidParam(sp); 
  if (! validBean.isValid()) {
      throw validBean.getExceptionOnInvalid();
  }
  ...
}
上一篇:java-模式验证错误的自定义映射器


下一篇:php-Laravel 5.1-自定义验证语言文件