利用Java的格局,其中方法采用单个参数的对象作为参数,而不是参数、格局、而不是、对象

2023-09-11 11:13:24 作者:Plateau 高原

我一直在使用亚马逊的冰川已经通过亚马逊的Java SDK。

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.

例如,要检索的作业,其中的参数保管库,的JobId,范围的输出,以下技术用于:

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));

相反的:

client.getJobOutput(Vault, JobId, range);

什么是两种方法的优点和缺点是什么?

What are the pros and cons of the two approaches?

推荐答案

优点:

如果你的方法有很多参数,使用参数对象将方法签名理智。 如果你想利用额外的参数的方法后,使用参数对象,意味着你只需要添加一个字段在参数对象和方法签名不需要改变。 如果你想方法的批量版本,只是通过参数对象的列表。

缺点:

附加冗长 间接的另一个层面