有没有更好的方法来处理向方法传递多个参数
我发现自己做了很多以下事情:
/**
* Redirect to a MVC controller&action
* @param controller
* @param action
*/
public void redirect(String controller, String action) {
redirect(controller, action, new HashMap<String, String>());
}
/**
* Redirect to a MVC controller&action with extra URL parameters
* @param controller
* @param action
* @param data
*/
public void redirect(String controller, String action, Map<String, String> data) {
String urlParameters = "";
for(String key : data.keySet()) {
urlParameters += "&" + key + "=" + data.get(key);
}
m_binder.putLocal("RedirectParams", "IdcService=MVC_FRONTCONTROLLER&controller="+controller+"&action="+action + urlParameters);
}
要调用第二种方法,我实际上需要创建一个哈希图来在其中添加数据,我想知道是否有更方便的方法来实现这一点?
如您所见,我需要知道键和值,因此可变参数不起作用(据我所知)。
我对所有想法持开放态度,包括使用反思。
I find myself doing the following a lot:
/**
* Redirect to a MVC controller&action
* @param controller
* @param action
*/
public void redirect(String controller, String action) {
redirect(controller, action, new HashMap<String, String>());
}
/**
* Redirect to a MVC controller&action with extra URL parameters
* @param controller
* @param action
* @param data
*/
public void redirect(String controller, String action, Map<String, String> data) {
String urlParameters = "";
for(String key : data.keySet()) {
urlParameters += "&" + key + "=" + data.get(key);
}
m_binder.putLocal("RedirectParams", "IdcService=MVC_FRONTCONTROLLER&controller="+controller+"&action="+action + urlParameters);
}
To call the second method I actually need to create a hashmap to add data in it and I was wondering if there is a more convenient way of achieving this?
As you can see I need to know both the key and the value, so varargs wouldn't work (as far as I can see).
I'm open to all ideas, including using reflection.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么必须创建新地图?我想您可以传入 null,然后在第二个
redirect
方法中检查 null 映射。无论如何,检查 null 可能是个好主意。Why do you have to create a new map? I imagine you could just pass in null and then check for a null map in your second
redirect
method. It's probably a good idea to check for null anyway.如果问题是创建新地图很尴尬,对于小地图,您可以使用 guava ImmutbleMap.of() 方法,
If the problem is that it is awkward to create a new map, for small maps you can use the guava ImmutbleMap.of() methods,
我不久前为构建地图编写了这个便捷方法。它需要可变参数并成对地生成一个映射。对于在测试代码中创建简单的地图来说,这非常方便。您需要确保参数的数量正确,但我喜欢它,因为它使代码量更小。
然后您可以使用以下方法创建一个映射:
然而,这并不是每个人都喜欢的(因为缺乏类型安全),因此您可以更改实现以采用对:
将pair定义为静态方法,该方法创建一个包含两个的简单对象值,然后是 mapOf 将这些对转换为映射中的条目。
I wrote this convenience method for build maps a while back. It takes varargs and makes a map out of them in pairs. For creating simple maps in test code, this is quite convenient. You need to make sure you get the numbers of parameters right but i like it because it makes the code volume smaller.
Then you can create a map using:
This is not everyone's cup of tea however (because of lack of type-safety) so you could change the implementation to take pairs:
Where you define pair as a static method that creates a simple object containing two values and then mapOf which converts those pairs into Entries in a Map.
你所做的事情是正常的、常见的。另一种方法是允许某些参数为空,并在第二种方法中进行一些空检查。您必须决定哪种方法您认为不那么难看。
我见过一些 API,其中相同的方法至少有 10 个这样的参数减少版本,显然没有任何原因。当它像这样膨胀时,你的设计就有问题了。在那之前,你所拥有的对我来说看起来不错,尽管我不太喜欢
Map
实例化。What you have done is normal and common. An alternative is to allow some of the parameters to be null and do some null-checking in the second method. You will have to decide which approach you feel is less ugly.
I have seen APIs where the same method has at least 10 of these parameter-reduced versions for apparently no reason at all. When it balloons up like that, there is something very wrong with your design. Until then, what you have looks fine to me, although I don't really like the
Map
instantiation much.我想说这是一种非常常见的方法。
我目前能想到的唯一其他(不太)方便的方法是:
then call
注意,这使用了可变参数,但使用起来不太安全(您可以传递长度为 1 甚至 0 的数组)。
I'd say that's a pretty common aproach.
The only other (not quite) convenient method I currently could think of would be:
then call
Note that this uses varargs but it's not quite safe to use (you could pass arrays of length 1 or even 0).