How to pass the various input parameters to a method in a class efficiently?

3.5k views Asked by At

Below is my Interface -

public interface IClient {
    public String executeSync(ClientInput input);
}

This is my Implementation of the Interface -

public class TestingClient implements IClient {

    @Override
    public String executeSync(ClientInput input) {

    }
}

Now I have a factory which gets the instance of TestingClient like this -

IClient client = TestingClientFactory.getInstance();

Now customer is going to make a call to executeSync method of my TestingClient which accepts the ClientInput parameter and below is the class for the ClientInput.

public final class ClientInput {

    private Long userid;
    private Long clientid;
    private Long timeout = 20L;
    private boolean debug;
    private Map<String, String> parameterMap;

    public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout, boolean debug) {
        this.userid = userid;
        this.clientid = clientid;
        this.parameterMap = parameterMap;
        this.timeout = timeout;
        this.debug = debug;
    }

 ... //getters here
}    

So when customer make a call to executeSync method of our TestingClient, they will create the ClientInput parameter like this and then use the factory to get the Instance of TestingClient and then call the executeSync method accordingly.

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");

ClientInput input = new ClientInput(109739281L, 20L, paramMap, 1000L, true);

IClient client = TestingClientFactory.getInstance();
client.executeSync(input);

Problem Statement:-

  1. Is this the right way to make ClientInput parameters and pass to executeSync method as shown above?
  2. There are already three arguments of the Long type in my ClientInput it may not be clear to other developers which position is for which field (especially during those long nights...). Any thoughts how to avoid this?
  3. If more inputs are required, it will make the constructor declaration longer. How can I overcome this situation?
4

There are 4 answers

2
Shiva Kumar On BEST ANSWER

Use builder pattern if you have more parameters. That makes code clean. (See this example)

For instance, you can have

clientinput.userid("userid)
           .clientid("clientid")

Some parameters if not specified can be optional. Like if timeout and debug are not set, they can take default values

2
sunleo On

For this Case Better Go with Setters and Getters all these fields instead of Constructor

private Long userid;
private Long clientid;
private Long timeout = 20L;
private boolean debug;
private Map<String, String> parameterMap;
0
Shoaib Chikate On

I suggest you to go with setters and getters Because in future your field(state) of Object may increase which will always demand you to make changes in your constructor.

0
wxyz On

1 Sounds ok like you did it. Method take as parameter an wrapped object which contains all informations -> good design as if the informations in the object changes, the method doesn't need to change. Clearly is better than having a method taking 5 different parameters.

2 I think the usual way to call the method will be something like:

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");

ClientInput input = new ClientInput(user.getUserId(), client.getClientId(), 20L, paramMap, 1000L, true);

IClient client = TestingClientFactory.getInstance();
client.executeSync(input);

so it should be clear enough and mistakes can be avoided

3 . If more parameters likely to appear, try to group them in some other wrapping classes which then will be used to create you calling object (composition).