I have a Factory
class that gives out instances of ResponseImpl
class. It accepts one Destination
class and up to four Source
classes. At least one of the four Source
classes should be not null
.
So, instead of asking calling program to pass null
s in the arguments to Factory
class, I have overloaded the method to accept at least one up to four Source
classes. But, something tells me that this way of doing it is not a good idea. Although this works, I feel like this can be handled better.
Could you let me know how to make the following piece of code better?
public static Response initResponse(final Destination destination, final Source1 source1) {
return initResponse(destination, source1, null, null, null);
}
public static Response initResponse(final Destination destination, final Source1 source1, final Source2 source2) {
return initResponse(destination, source1, source2, null, null);
}
public static Response initResponse(final Destination destination, final Source1 source1, final Source2 source2, final Source3 source3) {
return initResponse(destination, source1, source2, source3, null);
}
public static Response initResponse(final Destination destination, final Source1 source1, final Source2 source2, final Source3 source3, final Source4 source4) {
try {
if(source1 == null && source2 == null && source3 == null && source4 == null) {
throw new ABODataException("Atleast one source has to be not null");
}
return ResponseImplManager.getInstance(destination, source1, source2, source3, source4);
} catch (Exception e) {
throw new ABODataException("Unable to instantiate Response:"+e.getMessage());
}
}