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).