在 java 中处理验证的最佳做法是什么
what is a good practice to handle validation in java
我有 3 种方法在大多数内部方法中进行验证。我想知道验证是否失败以及验证失败时的错误消息。我知道我可以使用异常传递错误消息来执行如下操作..
String methodA(parms){
try{
return methodB(params);
}
catch(UserDefinedRuntimeException ure){
return ure.getMessage();
}
}
String methodB(params){
try{
return methodC(params);
}
catch(UserDefinedRuntimeException ure){
throw ure;
}
catch(Exception otherException){
//handle Exception
otherException.printStackTrace();
}
}
String methodC(params){
if(params.equals("A")||params.equals("B")||params.equals("C")){
return dosomething();
}
else{
throw UserDefinedRuntimeException("invalid input :params should be in [A,B,C]");
}
}
但很多人都说创建异常的成本很高
所以我通过谷歌搜索找到了
中建议的另一种方法
Best way to return status flag and message from a method in Java
喜欢下面..
class Response{
int status;
String errMsg;
}
String methodA(params){
Response response = methodB(params);
if(response.status == 1){
return "success";
}
else{
return response.errMsg;
}
}
Response methodB(params){
Response response = methodC(params);
if(response.status == 0){
return response;
}
else{
//do processing
response = new Response();
response.status =1;
return response;
}
}
Response methodC(params){
if(valid(params)){
//dosomething
Response response = new Response();
response.status =1;
return response;
}
else{
Response response = new Response();
response.status = 0;
response.errMsg = "invalid data";
return response;
}
}
但问题是不必要的POJO class.
请提出一些处理这种情况的方法。
提前致谢。
the problem with that is many say Exceptions are expensive to create
以可读性换取性能总是一个糟糕的交易。 CPU 周期每年都变得更便宜,使得性能效果的价值降低。同时,人类对程序的推理能力根本没有得到 "upgraded",使得可读性变化的负面影响永久存在。因此,您购买的是快速贬值的资产。
当你的程序逻辑要求你报告异常时,最好的办法就是抛出异常。只有当你真正抛出异常时才会有成本,这种情况应该很少发生。
与此同时,不应将异常用于非异常情况(即您预计会经常发生的情况)。最终用户在字段中输入错误数据就是这样一种非例外情况。当你实现这样的东西时,你展示的第二种方法是正确的方法。
But the problem is unnecessary POJO class.
另一种选择是从 "pull model" 切换到 "push model",当方法用不同类型的响应回调您时,而不是您查询响应的状态。这是此方法的基本实现:
public interface ResponseHandler {
void validDataEntered(String data);
void invalidDataEntered(String data);
}
public class Processor {
void methodA(String param1, String param2, ResponseHandler h) {
if (!param1.valid()) {
h.invalidDataEntered(param1);
return;
}
if (!param2.valid()) {
h.invalidDataEntered(param2);
return;
}
validDataEntered(param1+":"+param2);
}
}
public class MainClass implements ResponseHandler {
Processor p = new Processor();
public void validDataEntered(String data) {
System.out.println("Got valid data: "+data);
}
public void invalidDataEntered(String data) {
System.err.println("Got invalid data: "+data);
}
public void doSomething() {
p.methodA("one", "two", this);
}
}
我有 3 种方法在大多数内部方法中进行验证。我想知道验证是否失败以及验证失败时的错误消息。我知道我可以使用异常传递错误消息来执行如下操作..
String methodA(parms){
try{
return methodB(params);
}
catch(UserDefinedRuntimeException ure){
return ure.getMessage();
}
}
String methodB(params){
try{
return methodC(params);
}
catch(UserDefinedRuntimeException ure){
throw ure;
}
catch(Exception otherException){
//handle Exception
otherException.printStackTrace();
}
}
String methodC(params){
if(params.equals("A")||params.equals("B")||params.equals("C")){
return dosomething();
}
else{
throw UserDefinedRuntimeException("invalid input :params should be in [A,B,C]");
}
}
但很多人都说创建异常的成本很高
所以我通过谷歌搜索找到了
中建议的另一种方法Best way to return status flag and message from a method in Java
喜欢下面..
class Response{
int status;
String errMsg;
}
String methodA(params){
Response response = methodB(params);
if(response.status == 1){
return "success";
}
else{
return response.errMsg;
}
}
Response methodB(params){
Response response = methodC(params);
if(response.status == 0){
return response;
}
else{
//do processing
response = new Response();
response.status =1;
return response;
}
}
Response methodC(params){
if(valid(params)){
//dosomething
Response response = new Response();
response.status =1;
return response;
}
else{
Response response = new Response();
response.status = 0;
response.errMsg = "invalid data";
return response;
}
}
但问题是不必要的POJO class.
请提出一些处理这种情况的方法。
提前致谢。
the problem with that is many say Exceptions are expensive to create
以可读性换取性能总是一个糟糕的交易。 CPU 周期每年都变得更便宜,使得性能效果的价值降低。同时,人类对程序的推理能力根本没有得到 "upgraded",使得可读性变化的负面影响永久存在。因此,您购买的是快速贬值的资产。
当你的程序逻辑要求你报告异常时,最好的办法就是抛出异常。只有当你真正抛出异常时才会有成本,这种情况应该很少发生。
与此同时,不应将异常用于非异常情况(即您预计会经常发生的情况)。最终用户在字段中输入错误数据就是这样一种非例外情况。当你实现这样的东西时,你展示的第二种方法是正确的方法。
But the problem is unnecessary POJO class.
另一种选择是从 "pull model" 切换到 "push model",当方法用不同类型的响应回调您时,而不是您查询响应的状态。这是此方法的基本实现:
public interface ResponseHandler {
void validDataEntered(String data);
void invalidDataEntered(String data);
}
public class Processor {
void methodA(String param1, String param2, ResponseHandler h) {
if (!param1.valid()) {
h.invalidDataEntered(param1);
return;
}
if (!param2.valid()) {
h.invalidDataEntered(param2);
return;
}
validDataEntered(param1+":"+param2);
}
}
public class MainClass implements ResponseHandler {
Processor p = new Processor();
public void validDataEntered(String data) {
System.out.println("Got valid data: "+data);
}
public void invalidDataEntered(String data) {
System.err.println("Got invalid data: "+data);
}
public void doSomething() {
p.methodA("one", "two", this);
}
}