Reputation:
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:-
ClientInput
parameters and pass to executeSync
method as shown above?Upvotes: 2
Views: 3581
Reputation: 707
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).
Upvotes: 0
Reputation: 3161
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
Upvotes: 1
Reputation: 8973
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.
Upvotes: 0
Reputation: 10953
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;
Upvotes: 1