The advantage of a Java template where a method takes an object as a parameter instead of individual parameters

I use the Amazon Glacier through the Amazon Java SDK.

I am amazed that the parameters are passed through the object, and not as separate parameters.

For example, to obtain the result of a job, where the parameters are Vault, JobId, range, the following method is used:

client.getJobOutput(new GetJobOutputRequest(Vault, JobId, range)); 

Instead:

 client.getJobOutput(Vault, JobId, range); 

What are the pros and cons of the two approaches?

+4
java amazon-web-services amazon-glacier
source share
1 answer

Pros:

  • If your method accepts many parameters, using a parameter object makes the method signature reasonable.
  • If you want to use additional parameters for the method later, using the parameter object means that you just need to add another field to the param object, and the signature of the method should not be changed.
  • If you want a batch version of the method, just pass a list of param objects.

Minuses:

  • Additional verbosity
  • Another level of indirection
+9
source share

All Articles