所以我正在开发一个代码库,其中有一个实用程序类可以为用户生成 excel 文档。
它有一个名为 putDataInRowColumn(row, column, data)
的方法
它有很多方法,比如 putObjectsIntoExcel(myBigClass blah)
和 putObjectsIntoSpecialExcelType(myBigClass blah)
它调用了很多方法,例如 putObjectIntoSpecialRowType(blah.foo(), rowIndex, specialConditions)
和 putObjectIntoTotallydifferentRowType(blah.bar(), rowIndex, specialConditions)
所有这一切的要点是方法 putDataInRowColumn(row, column, data)
从许多不同的地方被称为公制 buttload。像100+。
现在,鉴于此遗留代码,我需要修改该方法以获取其他参数 - 样式信息。 99% 的方法现在将“null”作为第四个参数,1% 将接收一个包含样式信息的对象。
我修改了方法签名以接收附加参数,但我发现自己必须编写一个正则表达式来查找/替换所有方法调用。它奏效了,但这感觉是错误的方法。
我应该怎么做?
最佳答案
您创建一个接受第四个参数的新重载方法,并让旧方法以 null
作为第四个参数调用新方法。
之前:
public void putDataInRowColumn(int row, int column, int data) {
// implementation
}
之后:
// 99% calls this
public void putDataInRowColumn(int row, int column, int data) {
// Delegates to new method with null as "default" argument
putDataInRowColumn(row, column, data, null);
}
// Called by new code
public void putDataInRowColumn(int row, int column, int data, Style style) {
// implementation
}
相关:
关于java - 向项目中 100 个不同位置调用的方法添加参数 - 正确的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28740200/