我需要在Java SDK中弃用API以使它们更通用.但我无法弄清楚如何做以下情况:
public class AdoptDog {
public interface OnDogAdoption {
public void onDogAdoption(String dogName);
}
public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
// Perform asynchronous tasks...
// Then call the callback:
callbackObj.onDogAdoption(dogName);
}
}
SDK的用户进行如下调用:
AdoptDog adoptDog = new AdoptDog();
adoptDog.adoptDog("Snowball", new OnDogAdoption {
@Override
public void onDogAdoption(String dogName) {
System.out.println("Welcome " + dogName);
}
};
我想概括从Dog到Pet,并弃用提到Dog的API.为了向后兼容,我在弃用API时不必更改上面采用Snowball的代码片段.
我是如何尝试弃用Dog API的:
// Introduce Pet API
public class AdoptPet {
public interface OnPetAdoption {
public void onPetAdoption(String petName);
}
public void adoptPet(final String petName, OnPetAdoption callbackObj) {
// Perform asynchronous tasks...
// Then call the callback:
if (callbackObj instanceof OnDogAdoption) {
((OnDogAdoption) callbackObj).onDogAdoption(petName);
}
else {
callbackObj.onPetAdoption(petName);
}
}
}
// Dog API now extends Pet API for backward compatibility
@Deprecated
public class AdoptDog extends AdoptPet {
@Deprecated
public interface OnDogAdoption extends AdoptPet.OnPetAdoption {
@Deprecated
public void onDogAdoption(String dogName);
}
@Deprecated
public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
super.adoptPet(dogName, callbackObj);
}
}
问题是它不完全向后兼容. SDK的用户必须实现AdoptPet.OnPetAdoption.onPetAdoption(),否则他们会收到编译器错误:
AdoptDog adoptDog = new AdoptDog();
adoptDog.adoptDog("Snowball", new OnDogAdoption {
@Override
public void onDogAdoption(String dogName) {
System.out.println("Welcome " + dogName);
}
// PROBLEM: How avoid customers having to implement this dummy method?
@Override
public void onPetAdoption(String petName) {
assert("This code should not be reached");
}
};
是否有其他方法可以弃用AdoptDog(特别是OnDogAdoption)并保持完全向后兼容性?
解决方法:
您没有扩展OnPetAdoption. OnDogAdoption不是OnPetAdoption. OnPetAdoption也可以处理猫的采用. OnDogAdoption不能.
我建议您将OnDogAdoption包装在OnDogAdoptionDispatcher中,并使用该调度程序调用new方法,例如:
@Deprecated
public class AdoptDog extends AdoptPet {
@Deprecated
public interface OnDogAdoption {
@Deprecated
public void onDogAdoption(String dogName);
}
private static class DogAdoptionDispatcher implements AdoptPet.OnPetAdoption {
final OnDogAdoption target;
public DogAdoptionDispatcher(OnDogAdoption target) {
this.target = target;
}
@Override
public void onPetAdoption(String petName) {
target.onDogAdoption(petName);
}
}
@Deprecated
public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
super.adoptPet(dogName, new DogAdoptionDispatcher(callbackObj));
}
}
这样,您仍然具有向后兼容性和干净的新界面.
如果您使用的是Java 8或更高版本,则不需要单独的类,您可以这样做
public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
super.adoptPet(dogName, callbackObj::onDogAdoption);
}