3 回答

TA贡献1936条经验 获得超6个赞
我建议用额外的参数覆盖该方法,并使用现有的方法来获取arrayTable,并且只在被覆盖的方法中做额外的工作(计算行数)。
ArrayTable foo(... args) {} //existing method
Integer foo(... args, fetchRows) {
arrayTable = foo(args);
// do the rest here
}
通过这种方式,您可以降低添加任何回归的风险,并且您为此必须进行的代码更改将是最小的。

TA贡献1810条经验 获得超4个赞
不,这是不可取的。
您可以创建一个FooResult包含标志的新类,并且可以包含 rowCount 或输出:
class FooResult {
private boolean outputAvailable;
private Integer rowCount;
private ArrayTable<> output;
public FooResult(Integer rowCount) {
this.outputAvailable = false;
this.rowCount = rowCount;
}
public FooResult(ArrayTable output) {
this.outputAvailable = true;
this.output = output;
}
// getters
}
然后你的foo方法应该有FooResult它的返回类型,并返回如下:
if (/* some condition */) {
return new FooResult(rowCount);
} else {
return new FooResult(output);
}
最后,调用它的进程应该检查标志,并根据标志的值从结果对象中获取 rowCount 或输出。
if (result.isOutputAvailable()) {
// do stuff with result.getOutput()
} else {
// do stuff with result.getRowCount()
}
不过,创建两个单独的方法可能更简单。
添加回答
举报