0

I am not sure if this is a correct way to use method overloading or there is a nicer/good other logic. I have those 4 cases which can be happened. Is there a limit for the methods count?

private ResponseEntity<ServiceResponse> handleResponse(ServiceResponse response, ServiceRequest request) {
    return handleResponse(response, request, null, null);
}

private ResponseEntity<ServiceResponse> handleResponse(ServiceResponse response, ServiceRequest request, Exception e) {
    return handleResponse(response, request, e, null);
}

private ResponseEntity<ServiceResponse> handleResponse(ServiceResponse response, ServiceRequest request, Header header) {
    return handleResponse(response, request, null, correlation);
}

private ResponseEntity<ServiceResponse> handleResponse(ServiceResponse response, ServiceRequest request, Exception e, Header header) {
 //logic//
}
0

3 Answers 3

1

Is there a limit for the methods count

There is no practical limit; but you double the number of overloads each time another "optional" parameter is added, so there is an increasing burden (both cognitively and in terms of maintenance) in terms of the amount of code.

For any more than 2 parameters, you could maybe consider a using "builder pattern" (I say "builder" because you're not really building something, just invoking a method).

handler()
    .withException(e) // omit if not needed
    .withHeader(h) // omit if not needed
    .call();
Sign up to request clarification or add additional context in comments.

6 Comments

Shouldn't I need to create the Builder object? I am not sure how to use the builder here
@userit1985 handler() could invoke new Builder ()
Thank you.Is there any reference I can read? I'm not sure how to implement it.
@userit1985 to be clear, I am not advocating it for the case you've shown. Overloads are fine.
So you are suggesting to use reflection?
|
0

You are totally right. There's no limit on method counts as long as they share the same return type and differ in the number or type of parameters. At the end of the day, this is why method overloading exists.

Comments

0

There is no limit on the overloading of the methods. However, the current design is brittle. What if you want to accommodate one more parameter in the future? Whenever you will add extra param, you will end up modifying all these methods.

A better design would be, create a class that contains the params and use that. For example

class ResponseContext {
    private ServiceResponse response;
    private ServiceRequest request;
    private Exception excp;
    private Header header;
}

Use the builder pattern to construct the above object so that you won't end up creating endless constructors with different combinations of parameters.

Finally have only one method as

private ResponseEntity<ServiceResponse> handleResponse(ResponseContext responseContext) {
 //logic//
}

With the above design, you can easily accommodate extra params you might need in the future. Now, you don't need to overload the method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.