java – 使用函数的返回是一个好习惯,返回值是作为参数提供的对象吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 使用函数的返回是一个好习惯,返回值是作为参数提供的对象吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2955字,纯文字阅读大概需要5分钟。
内容图文
![java – 使用函数的返回是一个好习惯,返回值是作为参数提供的对象吗?](/upload/InfoBanner/zyjiaocheng/725/9bc14aba7c5a4237b767bcd04df5ea04.jpg)
我只是想问一下这是一个很好的java练习还是有更好的方式(官方方式)来做同样的事情.
首先,我需要更新一些hashmap信息:
Map<Date, Object> rows = new HashMap<Date, Object>();
这是excel行的对象,对于每个日期(即10月1日,10月2日等)和包含该行信息的对象.
所以,为了获得这些信息,我有一些方法,如:
rows = performQuery1(rows, foo, bar, initDate, endDate);
rows = performQuery2(rows, someDAO, foo);
和…
private HashMap<Date, Object> performQuery1(rows, foo, bar, Date, Date) {
// Some code that adds or removes elements from the hashmap "rows"
rows.put(date1, o1);
//Then return the same object
return rows;
}
所以我的问题是:这是一个很好的java练习吗?
rows = performQuery1(rows, foo, bar, initDate, endDate);
rows = performQuery2(rows, someDAO, foo);
或不?
解决方法:
问题确实非常广泛,或者集中在“最佳实践”部分,可能基于意见 – 但主要不是基于意见,因为对于这样的模式存在有效论据.
通常,您有一个方法可以在某处获取数据,并且应该将其放入目标数据结构(可能是集合或您的案例中的映射).
这种方法的签名有几种选择(大致在你的例子中,但这种模式可以推广).
第一个可能是
/**
* Computes ... something, and returns the result as a map
*/
Map<Date, Result> execute(Date d0, Date d1) { ... }
第二个可能是
/**
* Computes ... something, and places the results into the
* given map
*/
void execute(Date d0, Date d1, Map<Date, Result> results) { ... }
但是,为了获得最大的灵活性,我经常会参考第三个选项(这是您实际询问的选项):
/**
* Computes ... something, and places the results into the
* given map, which is then returned. If the given map is
* null, then a new map will be created and returned.
*/
Map<Date, Result> execute(
Date d0, Date d1, Map<Date, Result> results) { ... }
这有几个好处:
>您可以方便地让呼叫创建新地图:
Map<Date, Result> results = execute(d0, d1, null);
>您可以确定目标数据结构的实现.如果您总是返回一个新地图,那么就没有办法在HashMap或LinkedHashMap之间进行选择.将目标数据结构传递给方法允许您调用
Map<Date, Result> results =
execute(d0, d1, new HashMap<Date, Result>());
要么
Map<Date, Result> results =
execute(d0, d1, new LinkedHashMap<Date, Result>());
分别
>您不必为每个呼叫创建新地图.例如,您可以创建一系列调用
Map<Date, Result> results = new HashMap<Date, Result>();
execute(d0, d1, results);
execute(d2, d3, results);
在给定的地图中累积结果
考虑到它可以简单地模拟两种选择,这种方法的力量可能变得更加明显:
class DB {
// The private method that can emulate both public methods:
private Map<Date, Result> executeImpl(
Date d0, Date d1, Map<Date, Result> results);
// The implementation that returns a new map
public Map<Date, Result> execute(Date d0, Date d1) {
return executeImpl(d0, d1, null);
}
// The implementation that fills a given map
public void execute(Date d0, Date d1, Map<Date, Result> results) {
executeImpl(d0, d1, results);
}
}
旁白:Java SDK的某些地方也使用了类似的模式.例如,在不同的应用案例中:BufferedImageOp#filter:
BufferedImage filter(BufferedImage src, BufferedImage dest)
… If the destination image is null, a BufferedImage with an appropriate ColorModel is created.
Returns: The filtered BufferedImage
内容总结
以上是互联网集市为您收集整理的java – 使用函数的返回是一个好习惯,返回值是作为参数提供的对象吗?全部内容,希望文章能够帮你解决java – 使用函数的返回是一个好习惯,返回值是作为参数提供的对象吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。