jeudi 23 janvier 2020

Return two different data types based on condition from the same method

Looking for the best strategy to handle a method returns two different data types based on condition

public Response find(int id) {

Student std = studentService.findOne(empId):

if(std == null)
    return new ErrorResponse(errorCode);
else
   return new SuccessResponse(std);  

}

One condition is like I cannot implement an Interface for ErrorResponse or SuccessResponse.

Another condition is like I cannot have a parent class for this.

These classes are dynamically generated classes by other Clients

Aucun commentaire:

Enregistrer un commentaire