对于影响最小,我建议: 使ErrorActionResponse
作为StudentDTO
与setter和getter方法的私有成员。在服务中,当出现异常时,例化ErrorActionResponse
并在StudentDTO
的成员中设置相同。因此,客户必须首先检查getErrorActionResponse()
是否返回null
。如果是这样,则执行正常处理,处理异常情况。
CLASS StudentDTO:
public class StudentDTO {
...
private ErrorActionResponse errorActionResponse;
...
public ErrorActionResponse getErrorActionResponse() {
return errorActionResponse;
}
public void setErrorActionResponse(ErrorActionResponse errorActionResponse) {
this.errorActionResponse = errorActionResponse;
}
}
服务:
@Override
public StudentDTO getStudent(@WebParam(name = "name") String studentName) {
StudentDTO student = new StudentDTO();
try {
student = studentService.findStudentByName(studentName);
}
catch (Exception e) {
student.setErrorActionResponse(new ErrorActionResponse("student couldn't find by name"));
}
finally {
return student;
}
}
客户端代码:
if(student.getErrorActionResponse() == null) {
// do normal processing
}
else {
// handle exception case
}
在上述情况下,DTO有ErrorActionResponse
元件,其不涉及它的基本情况。所以,为了更清洁的方法,我建议你考虑Adapter pattern。
来源
2012-05-29 07:39:00
San
如果我尝试返回学生的名单,我将需要设置所有学生的errorActionResponse在列表 – kamaci
我同意。我的第一个建议是针对您的OP要求 - 返回学生而不是列表的方法(您现在要说明)。 – San