How to handle changing method signature in java [on hold]

问题: I'm writing a Library which has a method M() taking 3 parameters and method signature be like M(param 1, param 2, param 3) which might change during the development cycle....

问题:

I'm writing a Library which has a method M() taking 3 parameters and method signature be like M(param 1, param 2, param 3) which might change during the development cycle. since this library will be used three apps A, B and C, whenever i change the method signature( mostly there will change in number of parameter), Application A, B and C's existing code should not break. which is best way to handle this kind of scenario .


回答1:

If the signature is one way, and in use, then changing it will break the places it's used unless the changes are compatible with how it was used (changing int to Integer, for instance, is often compatible thanks to autoboxing).

The usual thing is to add an overload with the new signature, leaving the original method in place (although you might change its implementation). For instance, if the original was:

void m(Something a, SomethingElse b, YetAnotherThing c) {
    // ...
}

adding an overload / some overloads:

void m(Something a, SomethingElse b, YetAnotherThing c, ANewThing d) {
    // ...
}
// or
void m(Something a, SomethingElse b) {
    // ...
}
// or
void m(ANewThing d) {
    // ...
}

won't break clients using that original method (assuming the implementation continues to do what it did before, even if it does it differently).

  • 发表于 2019-01-17 17:42
  • 阅读 ( 92 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除