Advantage of Java Pattern where method takes Object as a parameter instead of individual parameters

旧城冷巷雨未停 提交于 2019-12-07 09:07:18

问题


I've been using Amazon Glacier via the Amazon Java SDK.

I'm struck that parameters are passed around via an object, rather than as individual parameters.

For example, to retrieve the output of a job, where the parameters are Vault, JobId,range, the following technique is used:

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

Instead of:

client.getJobOutput(Vault, JobId, range);

What are the pros and cons of the two approaches?


回答1:


Pros:

  1. If your method takes many parameters, using a parameter object makes the method signature sane.
  2. If you want to take additional parameters for the method later, using a parameter object means that you just have to add another field in the param object and the method signature need not change.
  3. If you want a batch version of the method, just pass a list of param objects.

Cons:

  1. Extra verbosity
  2. Another level of indirection


来源:https://stackoverflow.com/questions/12189236/advantage-of-java-pattern-where-method-takes-object-as-a-parameter-instead-of-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!